<!-- 计量人员新增 -->
<script lang="ts" setup name="addNotice">
import type { FormInstance, FormRules, UploadProps, UploadUserFile } from 'element-plus'
import { ElMessage, ElMessageBox, ElTable } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { technologyJobType } from '../person-interface'
import certificateAddDialog from './certificateAddDialog.vue'
import userListDialog from './userListDialog.vue'
import { getUserList } from '@/api/system/user'
import { getDeptTreeList } from '@/api/system/dept'
import { UploadFile } from '@/api/measure/file'
import { toTreeList } from '@/utils/structure'
import type { userType } from '@/views/system/user/user-interface'
import { getDictByCode } from '@/api/system/dict'
import comTreeSelect from '@/views/system/user/selecTree.vue'
import { getStaffAdd, getStaffDetail, getStaffupdate } from '@/api/measure/person'
import { getPhotoUrl } from '@/api/system/tool'
// import showPhoto from '@/views/system/tool/showPhoto.vue'
const emits = defineEmits(['resetData'])
const ruleFormRef = ref<FormInstance>() // from组件
const ruleForm = ref({
  account: '',
  name: '', // 姓名
  minioFileName: '', // 照片
  idCard: '', // 省份证号
  birthday: '', // 出生年月
  technologyJob: '', // 技术职务
  administrationJob: '', // 行政职务
  staffNo: '', // 人员编号
  sex: '', // 性别
  education: '', // 文化程度
  deptId: '', // 工作部门   科室
  major: '', // 计量专业
  workDate: '', // 从事日期
  mainExaminer: '', // 是否主考人
  specialOperator: '', // 是否特操人
  technologyExam: '', // 技术考核
  remark: '', // 备注
  certificateList: [], // 证书列表

}) // 表单
const multipleTableRef = ref<InstanceType<typeof ElTable>>()
const title = ref('')
// 身份证号码验证规则
const validateIDcard = (rule: any, value: any, callback: any) => {
  const rr = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[X])$)$/
  if (rr.test(value)) {
    callback()
  }
  else {
    callback(new Error('验证失败'))
  }
}
const rules = ref({
  name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
  minioFileName: [{ required: true, message: '头像不能为空', trigger: 'blur' }],
  idCard: [{ required: true, message: '身份证号码不符合规范', trigger: 'blur', validator: validateIDcard }],
  sex: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
  mainExaminer: [{ required: true, message: '请先选择', trigger: 'blur' }],
  specialOperator: [{ required: true, message: '请先选择', trigger: 'blur' }],
  technologyExam: [{ required: true, message: '请先选择', trigger: 'blur' }],
  deptId: [{ required: true, message: '请先选择', trigger: 'blur' }],
  major: [{ required: true, message: '请先输入', trigger: 'blur' }],
}) // 表单验证规则
// 根据身份证得到生日
const getBirthday = () => {
  ruleFormRef.value?.validateField('idCard').then((res) => {
    // 获取生日
    const IdCard = ruleForm.value.idCard
    const birthday = `${IdCard.substring(6, 10)}-${IdCard.substring(10, 12)}-${IdCard.substring(12, 14)}`
    ruleForm.value.birthday = birthday
    console.log(ruleForm.value)
  })
}
const dialogVisible = ref<boolean>(false) // 弹窗显示
const userList = ref<userType[]>([]) // 可使用人列表
const getuser = () => {
  getUserList({ offset: 1, limit: 99999 }).then((res) => {
    userList.value = res.data.rows
  })
} // 获取创建人列表
const selectWhether = ref([
  { name: '是', value: '0' },
  { name: '否', value: '1' },
])
const deptProps = reactive({
  parent: 'pid', value: 'id', label: 'name', children: 'children',
})
const PubList = ref([])
// 获取组织列表
const getPubList = () => {
  getDeptTreeList().then((res) => {
    // 转成树结构
    PubList.value = toTreeList(res.data, '0', true)
  })
}
const sexList = ref<{ id: string; value: string; name: string }[]>()
// 获取性别
const getSexList = () => {
  getDictByCode('sex').then((response) => {
    sexList.value = response.data
  })
}
const educationList = ref<technologyJobType[]>([])
// 获取文化程度列表
const geteducationList = () => {
  getDictByCode('education').then((response) => {
    educationList.value = response.data
  })
}
// 重置表单内容
const resetFormData = () => {
  ruleForm.value = {
    name: '', // 姓名
    minioFileName: '', // 照片
    idCard: '', // 省份证号
    birthday: '', // 出生年月
    technologyJob: '', // 技术职务
    administrationJob: '', // 行政职务
    staffNo: '', // 人员编号
    sex: '', // 性别
    education: '', // 文化程度
    deptId: '', // 工作部门   科室
    major: '', // 计量专业
    workDate: '', // 从事日期
    mainExaminer: '', // 是否主考人
    specialOperator: '', // 是否特操人
    technologyExam: '', // 技术考核
    remark: '', // 备注
    certificateList: [], // 证书列表

  }
}
const technologyJobList = ref<technologyJobType[]>([])
// 获取技术职务列表
const getTechnologyJobList = () => {
  getDictByCode('technologyJob').then((response) => {
    technologyJobList.value = response.data
  })
}
const administrationJobList = ref<technologyJobType[]>([])
// 获取行政职务列表
const getAdministrationJob = () => {
  getDictByCode('administrationJob').then((response) => {
    administrationJobList.value = response.data
  })
}
const userListRef = ref()
// 选择人员
const selectPerson = () => {
  userListRef.value.initDialog()
}
// 保存选择人员信息
const confirmPerson = (row: userType) => {
  ruleForm.value.name = row.name
  ruleForm.value.account = row.account
  ruleFormRef.value?.clearValidate('name')
}
// 上传请求
const uploadQuarterlyEvaluateFile: any = (file: any) => {
  const fd = new FormData()
  fd.append('multipartFile', file.file)
  UploadFile(fd).then((res) => {
    if (res.code === 200) {
      ruleFormRef.value?.clearValidate('minioFileName')
      ElMessage.success('上传成功')
      ruleForm.value.minioFileName = res.data[0]
    }
  })
}
// 上传之前的验证
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png' && rawFile.type !== 'image/jpg') {
    ElMessage.error('只能上传png/jpeg/jpg格式的图片')
    return false
  }
  else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('图片不能大于2MB')
    return false
  }
  return true
}
const changeList = ref([
  {
    name: '证书基本信息',
    data: [] as any[],
    column: [
      {
        label: '发证单位',
        data: 'certificateCompany',
        id: 'zs01',
      },
      {
        label: '证书号',
        data: 'certificateNo',
        id: 'zs02',
      },
      {
        label: '证书名称',
        data: 'certificateName',
        id: 'zs03',
      },
      {
        label: '发证日期',
        data: 'certificateDate',
        id: 'zs04',
      },
      {
        label: '有效日期',
        data: 'validDate',
        id: 'zs05',
      },
    ],
  },
  {
    name: '计量工作培训记录',
    data: [],
    column: [],
  },
  {
    name: '业务记录',
    data: [],
    column: [],
  },
])
const current = ref('证书基本信息')
const currentShow = computed(() => {
  return changeList.value.filter(item => item.name === current.value)[0]
})
const certificateRef = ref() // 添加证书信息弹窗
const currenSelect = ref<number>(-1) // 当前选中行
// 添加行
const addRow = () => {
  certificateRef.value.initDialog({ title: '添加' })
}
// 编辑行
const editRow = () => {
  if (currenSelect.value >= 0) {
    const row: object = changeList.value[0].data.filter((item, index) => index === currenSelect.value)[0]
    certificateRef.value.initDialog({ ...row, title: '编辑' })
  }
  else {
    ElMessage.warning('请先选择需要编辑的数据')
  }
}
// 证书信息行内添加数据
const addRowcertificate = (row: any, _: string) => {
  if (_ === 'update') {
    changeList.value[0].data = changeList.value[0].data.map((item, index) => {
      if (index === currenSelect.value) {
        return {
          ...item,
          ...row,
        }
      }
      else {
        return item
      }
    })
  }
  else {
    changeList.value[0].data.push(row)
  }
}

// 删除行
const removeRow = () => {
  console.log(currenSelect.value)
  if (currenSelect.value >= 0) {
    ElMessageBox.confirm(
      '确认删除选中的数据吗？',
      '提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      },
    ).then(() => {
      changeList.value[0].data = changeList.value[0].data.filter((item, index) => index !== currenSelect.value)
      currenSelect.value = -1
      ElMessage.success('删除成功')
    })
  }
  else {
    ElMessage.warning('请先选择需要删除的数据')
  }
}
// 提交
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) { return }
  await formEl.validate((valid, fields) => {
    if (valid) {
      ElMessageBox.confirm(
        '确认提交吗？',
        '提示',
        {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        },
      ).then(() => {
        if (changeList.value[0].data.length) {
          for (const key in ruleForm.value) {
            if (ruleForm.value[key] === '') {
              ruleForm.value[key] = null
            }
          }
          (title.value === '新建'
            ? getStaffAdd
            : getStaffupdate) ({ ...ruleForm.value, certificateList: changeList.value[0].data }).then((res) => {
            if (res.code == 200) {
              formEl?.resetFields()
              ElMessage.success('提交成功')
              emits('resetData')
              resetFormData()
            }
          })
        }
        else {
          ElMessage.warning('请先填写证书信息')
        }
      })
    }
  })
}
// 取消
const resetForm = (formEl: FormInstance | undefined) => {
  formEl?.resetFields()
  changeList.value[0].data = []
  emits('resetData')
  resetFormData()
}
// 弹窗初始化
const initDialog = (row: any) => {
  changeList.value[0].data = []
  dialogVisible.value = true
  title.value = row.title
  resetFormData()
  getuser()
  getPubList()
  getSexList()
  geteducationList()
  getTechnologyJobList()
  getAdministrationJob()
  if (row.title === '新建') {
    console.log()
  }
  else {
    getStaffDetail({ ...row }).then((res) => {
      if (res.code === 200) {
        ruleForm.value = res.data
        changeList.value[0].data = res.data.certificateList
      }
    })
  }
}
defineExpose({ initDialog })
// 图片预览地址
const photoUrl = ref('')
watch(() => ruleForm.value.minioFileName, (newVal) => {
  getPhotoUrl(newVal).then((res) => {
    if (res.code === 200) {
      photoUrl.value = res.data
    }
  })
})
</script>

<template>
  <app-container style="overflow: hidden;">
    <div class="body-container">
      <div class="header">
        <div class="title">
          {{ title }}计量人员
        </div>
        <span class="btns">
          <el-button v-if="title !== '详情'" type="primary" @click="submitForm(ruleFormRef)">
            完成
          </el-button>
          <el-button type="info" @click="resetForm(ruleFormRef)">
            关闭
          </el-button>
        </span>
      </div>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-position="right" label-width="110px" class="form" :class="[title === '详情' ? 'isDetail' : '']" border stripe>
        <el-row :gutter="24" class="marg">
          <el-col :span="12">
            <el-form-item label="用户头像" prop="minioFileName">
              <el-upload
                class="avatar-uploader"
                :show-file-list="false"
                :http-request="uploadQuarterlyEvaluateFile"
                :before-upload="beforeAvatarUpload"
                style="margin-left: 50px;"
                :disabled="title === '详情'"
              >
                <!-- <show-photo v-if="ruleForm.minioFileName" :minio-file-name="ruleForm.minioFileName" width="178px" height="178px" /> -->
                <img v-if="ruleForm.minioFileName" :src="photoUrl" class="avatar">
                <el-icon v-else class="avatar-uploader-icon">
                  <plus />
                </el-icon>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="姓名" prop="name">
              <el-input v-model.trim="ruleForm.name" placeholder="名称" :disabled="title === '详情'" @focus="selectPerson" />
            </el-form-item>
            <el-form-item label="身份证号" prop="idCard">
              <el-input ref="cardRef" v-model.trim="ruleForm.idCard" placeholder="身份证号" :disabled="title === '详情'" @blur="getBirthday" />
            </el-form-item>
            <el-form-item label="出生年月" prop="birthday">
              <el-date-picker
                v-model="ruleForm.birthday" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD"
                placeholder="出生年月" :disabled="title === '详情'"
              />
            </el-form-item>
            <el-form-item label="技术职务" prop="technologyJob">
              <el-select v-model="ruleForm.technologyJob" placeholder="技术职务" :disabled="title === '详情'">
                <el-option v-for="item in technologyJobList" :key="item.id" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="人员编号" prop="staffNo">
              <el-input v-model.trim="ruleForm.staffNo" placeholder="系统自动生成" disabled />
            </el-form-item>
            <el-form-item label="性别" prop="sex">
              <el-select v-model="ruleForm.sex" placeholder="请选择性别" :disabled="title === '详情'">
                <el-option v-for="item in sexList" :key="item.id" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="文化程度" prop="education">
              <el-select v-model="ruleForm.education" placeholder="文化程度" :disabled="title === '详情'">
                <el-option v-for="item in educationList" :key="item.id" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="行政职务" prop="administrationJob">
              <el-select v-model="ruleForm.administrationJob" placeholder="行政职务" :disabled="title === '详情'">
                <el-option v-for="item in administrationJobList" :key="item.id" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="marg">
          <el-col :span="6">
            <el-form-item label="工作部门" prop="deptId">
              <!-- <el-select v-model="ruleForm.deptId" placeholder="工作部门" :disabled="title === '详情'">
                <el-option v-for="item in PubList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select> -->
              <el-input v-if="title === '详情'" v-model.trim="ruleForm.deptId" disabled />
              <com-tree-select
                v-else
                v-model="ruleForm.deptId" :options="PubList" placeholder="选择组织机构"
                :tree-props="deptProps"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="计量专业" prop="major">
              <el-input v-model.trim="ruleForm.major" placeholder="计量专业" :disabled="title === '详情'" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="从事日期" prop="workDate">
              <el-date-picker
                v-model="ruleForm.workDate" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD HH:mm:ss" :disabled="title === '详情'"
                placeholder="请选择从事日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="marg">
          <el-col :span="6">
            <el-form-item label="是否主考人" prop="mainExaminer">
              <el-select v-model="ruleForm.mainExaminer" placeholder="是否主考人" :disabled="title === '详情'">
                <el-option v-for="item in selectWhether" :key="item.value" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否特操人" prop="specialOperator">
              <el-select v-model="ruleForm.specialOperator" placeholder="是否特操人" :disabled="title === '详情'">
                <el-option v-for="item in selectWhether" :key="item.value" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="是否技术考核" prop="technologyExam">
              <el-select v-model="ruleForm.technologyExam" placeholder="技术考核" :disabled="title === '详情'">
                <el-option v-for="item in selectWhether" :key="item.value" :label="item.name" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="marg">
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model.trim="ruleForm.remark" placeholder="请输入备注" :disabled="title === '详情'" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24" class="marg" />
      </el-form>
      <!-- 证书基本信息 -->
      <!-- 切换 -->
      <!-- <el-radio-group v-model="current" style="margin-bottom: -15px;">
          <el-radio-button v-for="item in changeList" :key="item.name" :label="item.name">
            {{ item.name }}
          </el-radio-button>
        </el-radio-group> -->
      <div class="header" style="margin-top: 10px;">
        <div class="title">
          {{ current }}
        </div>
        <span class="btns">
          <el-button v-if="title !== '详情'" type="info" @click="removeRow">
            删除行
          </el-button>
          <el-button v-if="title !== '详情'" type="primary" @click="addRow">
            添加行
          </el-button>
          <el-button v-if="title !== '详情'" type="primary" @click="editRow">
            编辑行
          </el-button>
        </span>
      </div>
      <el-table ref="multipleTableRef" :data="currentShow.data" style="width: 100%;margin-top: 10px;">
        <el-table-column width="55" align="center">
          <template #default="scope">
            <el-radio-group v-model="currenSelect" class="ml-4">
              <el-radio :label="scope.$index" />
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column type="index" label="序号" width="55" />
        <el-table-column v-for="item in currentShow.column" :key="item.id" :prop="item.data" :label="item.label" align="center" />
      </el-table>
    </div>
    <!-- 添加证书信息组件 -->
    <certificate-add-dialog ref="certificateRef" @add="addRowcertificate" />
    <!-- 选择用户列表组件 -->
    <user-list-dialog ref="userListRef" @add="confirmPerson" />
  </app-container>
</template>

<style lang="scss" scoped>
.body-container {
  .form {
    background-color: #fff;
    border-radius: 8px;
    padding-top: 10px;
    margin-top: 10px;
  }

  .header {
    background-color: #fff;
    height: 40px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    flex-direction: column;
    justify-content: center;
    position: relative;

    .title {
      font-weight: 700;
    }

    .btns {
      position: absolute;
      right: 40px;
      top: 5px;
    }
  }

  :deep(.el-radio__label) {
    display: none;
  }

  .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 {
        display: none;
      }
    }
  }
}

.marg {
  margin-top: 20px;
}
</style>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
