<!-- 委托方意见反馈表-编辑 -->
<script lang="ts" setup name="ClientOpinionFormEdit">
import type { FormInstance, FormRules, UploadUserFile } from 'element-plus'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import dayjs from 'dayjs'
import useUserStore from '@/store/modules/user'
import { getUserDept } from '@/api/system/user'
import { addOpinion } from '@/api/eqpt/MeasurementBusiness/opinion'
const $route = useRoute()
const $router = useRouter()
const userStore = useUserStore()
const ruleFormRef = ref<FormInstance>() // from组件
const tableRef = ref()
// 显示标题
const textMap: { [key: string]: string } = {
  update: '编辑',
  create: '新增',
  detail: '详情',
}
// 对话框类型：create,update
const dialogStatus = ref('create')
const ruleForm = ref({
  appealUserId: '',
  appealUserName: '',
  approvalStatus: '',
  approvalStatusName: '',
  createTime: '',
  createUserId: '',
  createUserName: '',
  customerDeptId: '',
  customerDeptName: '',
  customerId: '',
  customerName: '',
  decisionItem: '',
  formName: '',
  formNo: '',
  id: '',
  labCode: '',
  processId: '',
  remark: '',
  suggestContent: '',
  taskId: '',
  updateTime: '',
}) // 表单
const rules = ref<FormRules>({
  appealUserName: [{ required: true, message: '申诉人', trigger: ['blur', 'change'] }],
}) // 表单验证规则
// 弹窗初始化
const initDialog = () => {
  dialogStatus.value = $route.params.type as string
  ruleFormRef.value?.resetFields()
  if ($route.params.type !== 'create') {
    const data = JSON.parse($route.query.row as string)
    ruleForm.value = data
  }
  if ($route.params.type === 'update' || $route.params.type === 'create') {
    ruleForm.value.formName = '委托方意见登记表'
    ruleForm.value.createTime = dayjs().format('YYYY-MM-DD HH:mm:ss') // 创建时间
    ruleForm.value.createUserId = userStore.id
    ruleForm.value.createUserName = userStore.name
    ruleForm.value.customerDeptId = userStore.deptId
    ruleForm.value.customerDeptName = userStore.deptName
    // 获取当前用户所在单位
    getUserDept().then((res) => {
      ruleForm.value.customerId = res.data.id
      ruleForm.value.customerName = res.data.fullName
    })
  }
}
onMounted(() => {
  initDialog()
})

// 关闭弹窗
const close = () => {
  $router.back()
}
// 编辑
const update = () => {
  addOpinion(ruleForm.value).then((res) => {
    ElMessage.success('添加成功')
    close()
  })
}
// 保存
const saveForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) { return }
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (!ruleForm.value.suggestContent) {
        ElMessage.warning('请先填写意见内容')
        return
      }
      ElMessageBox.confirm(
        '确认保存吗？',
        '提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        },
      ).then((res) => {
        update()
      })
    }
  })
}
// 取消
const resetForm = (formEl: FormInstance | undefined) => {
  formEl?.resetFields()
  close()
}
</script>

<template>
  <app-container style="overflow: hidden;">
    <detail-page :title="`委托方意见登记表-${textMap[dialogStatus]}`">
      <template #btns>
        <el-button v-if="!$route.path.includes('detail')" type="primary" @click="saveForm(ruleFormRef)">
          保存
        </el-button>
        <el-button type="info" @click="resetForm(ruleFormRef)">
          关闭
        </el-button>
      </template>
    </detail-page>
    <detail-block-com>
      <el-form ref="ruleFormRef" :model="ruleForm" :class="$route.path.includes('detail') ? 'isDetail' : ''" :rules="rules" label-position="right" label-width="120px" class="form" :disabled="$route.path.includes('detail')">
        <el-row :gutter="24" class="marg">
          <el-col :span="6">
            <el-form-item label="表单编号" prop="formNo">
              <el-input v-model.trim="ruleForm.formNo" placeholder="系统自动生成" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="表单名称" prop="formName">
              <el-input v-model.trim="ruleForm.formName" placeholder="表单名称" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单位" props="senderName">
              <el-input v-model.trim="ruleForm.customerName" placeholder="单位" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门">
              <el-input v-model.trim="ruleForm.customerDeptName" placeholder="部门" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="marg">
          <el-col :span="6">
            <el-form-item label="申诉人" prop="appealUserName">
              <el-input v-model.trim="ruleForm.appealUserName" placeholder="申诉人" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建人" prop="createUserName">
              <el-input v-model.trim="ruleForm.createUserName" placeholder="创建人" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间" prop="createTime">
              <el-input v-model.trim="ruleForm.createTime" placeholder="创建时间" disabled />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </detail-block-com>
    <detail-block-com>
      <detail-block-switch title="意见内容">
        <el-row :gutter="24" class="marg">
          <el-col :span="1" />
          <el-col :span="22">
            <el-input
              v-model.trim="ruleForm.suggestContent" :rows="4" :disabled="$route.path.includes('detail')"
              type="textarea" placeholder="意见内容"
            />
          </el-col>
          <el-col :span="1" />
        </el-row>
      </detail-block-switch>
    </detail-block-com>
  </app-container>
</template>

<style lang="scss" scoped>
// 详情页面隐藏小红点
.isDetail {
  ::v-deep {
    .el-form-item.is-required:not(.is-no-asterisk) .el-form-item__label-wrap > .el-form-item__label::before,
    .el-form-item.is-required:not(.is-no-asterisk) > .el-form-item__label::before {
      content: "";
      display: none;
    }
  }
}
</style>
