<template>
  <Dialog v-model="dialogVisible" title="导入历史工资条" style="width: 600px">
    <el-form :model="formData" ref="formRef" :rules="rules" label-width="80px">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-form-item label="年月" prop="period">
            <el-date-picker
              v-model="formData.period"
              type="month"
              placeholder="选择年月"
              format="YYYY-MM"
              value-format="YYYY-MM"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-form-item label="导入工资条">
            <UploadFile
              v-model="formData.rewardSalaryFile"
              :limit="1"
              :fileType="['xls', 'xlsx']"
              accept=".xls,.xlsx"
              :isShowTip="false"
            >
              <template #tip>
                <div>
                  <el-link type="primary" :underline="false" @click="downloadTemplateFile(3)">
                    点击下载模板文件
                  </el-link>
                </div>
              </template>
            </UploadFile>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="formLoading" @click="handleSave"> 确认生成 </el-button>
      </span>
    </template>
  </Dialog>
</template>

<script setup name="DialogSalaryImport">
import * as SalaryApi from '@/api/home/salary.js'
import { downloadFile } from '@/utils/download'

const dialogVisible = ref(false)

const formData = ref({})

const formLoading = ref(false)

const rules = {
  period: { required: true, message: '年月不可为空', trigger: 'blur,change' },
  attendanceFile: { required: true, message: '考勤文件不可为空', trigger: 'blur,change' }
}

function open() {
  dialogVisible.value = true
  resetForm()
}

function resetForm() {
  formData.value = {
    period: `${new Date().getFullYear()}-${new Date().getMonth() + 1}`,
    rewardSalaryFile: '',
    attendanceFile: ''
  }
}

defineExpose({ open })

function handleSave() {}

async function downloadTemplateFile(type) {
  let data
  if (type == 3) {
    data = await SalaryApi.getLinkHistorySalary()
    downloadFile(data, '工资条模板.xls')
  }
}
</script>

<style lang="scss" scoped></style>
