<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="800px">
    <el-form ref="formRef" :model="formData" label-width="130px">
      <el-form-item label="每日跟进指标数">
        <el-input-number v-model="formData.targetNum" :controls="false" style="width: 150px" />
      </el-form-item>
      <el-form-item label="跟进指标生效日期">
        <el-date-picker
          v-model="formData.startDate"
          type="date"
          placeholder="选择日期时间"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          style="width: 150px"
        />
      </el-form-item>
    </el-form>
    <el-divider direction="horizontal" content-position="left">成交额指标</el-divider>
    <el-button class="mb-10px" type="primary" @click="handleAddPrice"> 添加年份 </el-button>
    <el-table :data="formData.deptSignPriceTargetVOList" border>
      <el-table-column label="年份" width="120">
        <template #default="{ row }">
          <el-date-picker
            v-model="row.year"
            type="year"
            placeholder="选择年份"
            size="small"
            format="YYYY"
            value-format="YYYY"
            style="width: 100%"
          />
        </template>
      </el-table-column>
      <el-table-column v-for="col in 12" :key="col" :label="`${col}月`" width="100px">
        <template #default="{ row }">
          <el-input-number
            v-model="row.monthTargetVOList[col - 1].targetPrice"
            size="small"
            :controls="false"
            style="width: 100%"
          />
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup name="DialogTarget">
import * as DeptApi from '@/api/system/dept'

const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  remark: undefined
})

/** 打开弹窗 */
const open = async (deptId) => {
  dialogVisible.value = true
  dialogTitle.value = '业绩指标'
  resetForm()
  // 修改时，设置数据
  if (deptId) {
    formLoading.value = true
    try {
      formData.value = await DeptApi.getDeptTarget(deptId)
      formData.value.deptId = deptId
      if (formData.value?.deptSignPriceTargetVOList) {
        formData.value.deptSignPriceTargetVOList.forEach((it) => {
          it.year = it.year + ''
        })
      }
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const formRef = ref() // 表单 Ref
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    await DeptApi.updateDeptTarget(formData.value)
    message.success('修改成功')
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    targetNum: undefined,
    startDate: undefined,
    deptSignPriceTargetVOList: []
  }
  formRef.value?.resetFields()
}

function handleAddPrice() {
  const obj = {
    year: undefined,
    monthTargetVOList: []
  }
  for (let i = 1; i <= 12; i++) {
    obj.monthTargetVOList.push({
      month: i,
      targetPrice: undefined
    })
  }
  if (!formData.value.deptSignPriceTargetVOList) {
    formData.value.deptSignPriceTargetVOList = []
  }
  formData.value.deptSignPriceTargetVOList.push(obj)
}
</script>

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