<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" style="width: 1200px">
    <el-form :model="form" ref="formRef" :rules="rules" label-width="80px">
      <el-form-item label="方案名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入方案名称" style="width: 240px" />
      </el-form-item>
    </el-form>
    <div class="mt-10px mb-10px">
      <el-button type="primary" @click="handleAddYearPlan"> 添加年度考勤方案 </el-button>
      <span class="ml-20px font-bold">请配置每月计薪天数，计薪天数 = 应出勤天数 + 带薪假天数</span>
    </div>
    <el-table :data="form.planList" border stripe>
      <el-table-column label="年份" width="150px">
        <template #default="{ row }">
          <el-date-picker
            v-model="row.year"
            type="year"
            placeholder="选择年份"
            format="YYYY"
            value-format="YYYY"
            style="width: 120px"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-for="item in 12"
        :prop="item"
        :key="item"
        :label="item + '月'"
        width="140px"
      >
        <el-table-column label="应出勤">
          <template #default="{ row }">
            <el-input-number
              v-model="row[item].yingchuqin"
              size="small"
              :min="1"
              :max="31"
              :controls="false"
              style="width: 50px"
            />
          </template>
        </el-table-column>
        <el-table-column label="带薪假">
          <template #default="{ row }">
            <el-input-number
              v-model="row[item].daixinjia"
              size="small"
              :min="0"
              :max="31"
              :controls="false"
              style="width: 50px"
            />
          </template>
        </el-table-column>
      </el-table-column>
    </el-table>

    <template #footer>
      <span>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button :disabled="formLoading" type="primary" @click="submitForm">保存</el-button>
      </span>
    </template>
  </Dialog>
</template>

<script setup name="DialogAttendancePlan">
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('考勤方案设置')
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用

const formType = ref('create')
/** 打开弹窗 */
const open = async (type, row) => {
  dialogVisible.value = true
  dialogTitle.value = type == 'create' ? `新增方案` : '修改方案'
  formType.value = type
  resetForm()

  if (row?.id) {
    formLoading.value = true
    try {
      // comissionForm.value = await UserApi.getUser(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const form = ref({})
const rules = {
  name: { required: true, message: '方案名称不可为空', trigger: 'blur' }
}
function resetForm() {
  form.value = {
    name: undefined,
    planList: []
  }
}

function handleAddYearPlan() {
  form.value.planList.push({
    year: new Date().getFullYear(),
    1: { yingchuqin: undefined, daixinjia: undefined },
    2: { yingchuqin: undefined, daixinjia: undefined },
    3: { yingchuqin: undefined, daixinjia: undefined },
    4: { yingchuqin: undefined, daixinjia: undefined },
    5: { yingchuqin: undefined, daixinjia: undefined },
    6: { yingchuqin: undefined, daixinjia: undefined },
    7: { yingchuqin: undefined, daixinjia: undefined },
    8: { yingchuqin: undefined, daixinjia: undefined },
    9: { yingchuqin: undefined, daixinjia: undefined },
    10: { yingchuqin: undefined, daixinjia: undefined },
    11: { yingchuqin: undefined, daixinjia: undefined },
    12: { yingchuqin: undefined, daixinjia: undefined }
  })
}

const emit = defineEmits(['success'])
const formRef = ref()
const submitForm = async () => {
  // 校验表单
  if (!formRef.value) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    if (formType.value === 'create') {
      // await ClassApi.createClassType(data)
      message.success(t('common.createSuccess'))
    } else {
      // await ClassApi.updateClassType(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
</script>

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