<template>
  <el-dialog
    :title="`${id ? '编辑' : '新增'} - 人员基本信息`"
    :visible.sync="dialogVisible"
    custom-class="new-edition-dialog"
    width="80%"
    @closed="closed"
    @open="open"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-position="top"
      label-width="auto"
    >
      <el-row :gutter="24" style="flex-wrap:wrap" type="flex">
        <el-col :span="6">
          <el-form-item label="姓名" prop="userName">
            <el-input
              v-model="form.userName"
              clearable
              placeholder="请输入姓名"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="性别" prop="sex">
            <el-select
              v-model="form.sex"
              clearable
              placeholder="请选择性别"
              style="width:100%"
            >
              <el-option
                v-for="item in getDictionaryByCode('xb')"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="身高" prop="height">
            <el-input
              v-model="form.height"
              clearable
              placeholder="请输入身高"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="体重" prop="weight">
            <el-input
              v-model="form.weight"
              clearable
              placeholder="请输入体重"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="血型" prop="bloodType">
            <el-select
              v-model="form.bloodType"
              clearable
              placeholder="请选择血型"
              style="width:100%"
            >
              <el-option
                v-for="item in getDictionaryByCode('xx')"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="民族" prop="nation">
            <el-input
              v-model="form.nation"
              clearable
              placeholder="请输入民族"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="籍贯" prop="nativePlace">
            <el-input
              v-model="form.nativePlace"
              clearable
              placeholder="请输入籍贯"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="文化程度" prop="degreeEducation">
            <el-input
              v-model="form.degreeEducation"
              clearable
              placeholder="请输入文化程度"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="军衔" prop="militaryRank">
            <el-input
              v-model="form.militaryRank"
              clearable
              placeholder="请输入军衔"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="职务" prop="post">
            <el-input
              v-model="form.post"
              clearable
              placeholder="请输入职务"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="岗位" prop="position">
            <el-select
              v-model="form.position"
              filterable
              placeholder="请选择岗位"
              style="width: 100%;"
            >
              <el-option
                v-for="(item, index) in positonData"
                :key="index"
                :label="item.positionName"
                :value="item.positionId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="出生日期" prop="birthday">
            <el-date-picker
              v-model="form.birthday"
              clearable
              placeholder="请选择日期"
              style="width: 100%;"
              type="date"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="入伍年月" prop="enlistmentTime">
            <el-date-picker
              v-model="form.enlistmentTime"
              clearable
              placeholder="请选择入伍年月"
              style="width: 100%;"
              type="date"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="政治面貌" prop="politicalStatus">
            <el-input
              v-model="form.politicalStatus"
              clearable
              placeholder="请输入政治面貌"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="党团时间" prop="partyTime">
            <el-date-picker
              v-model="form.partyTime"
              clearable
              placeholder="请选择党团时间"
              style="width: 100%;"
              type="date"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="身份证号" prop="idCard">
            <el-input
              v-model="form.idCard"
              clearable
              placeholder="请输入身份证号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="军官（军士、义务兵）证号" prop="officerCard">
            <el-input
              v-model="form.officerCard"
              clearable
              placeholder="请输入军官（军士、义务兵）证号"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="所属单位" prop="caompanyName">
            <el-input
              v-model="form.caompanyName"
              clearable
              disabled
              placeholder="请输入所属单位"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="照片" prop="photo">
            <el-input
              v-model="form.photo"
              clearable
              placeholder="请输入照片"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="户籍类型" prop="householdType">
            <el-input
              v-model="form.householdType"
              clearable
              placeholder="请输入户籍类型"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="军体等级" prop="sportsGrade">
            <el-input
              v-model="form.sportsGrade"
              clearable
              placeholder="请输入军体等级"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="专业能力" prop="competence">
            <el-input
              v-model="form.competence"
              clearable
              placeholder="请输入专业能力"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="认证专业" prop="certificationMajor">
            <el-input
              v-model="form.certificationMajor"
              clearable
              placeholder="请输入认证专业"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="人员类别" prop="userType">
            <el-input
              v-model="form.userType"
              clearable
              placeholder="请输入人员类别"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="生长类别" prop="efficiencyImpacT">
            <el-input
              v-model="form.growthType"
              clearable
              placeholder="请输入生长类别"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="saveLoading" type="primary" @click="handleSave">
        确 认
      </el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import * as api from '@/api/examDataMenu/examObjectData/userDataMenu'
import {dictionaryEscape, getDictionaryByCode} from '@/utils/dictionary'
import userTypeMenu from '@/api/examination/userTypeMenu'
import {refineRequestBody} from '@/utils/requestWrappers'

export default {
  name: 'AddEditDialog',
  components: {},
  props: {
    value: {
      type: Boolean,
      default: false
    },
    rowInfo: {
      type: Object,
      default: () => ({})
    },
    activeTreeItem: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      rules: {
        userName: [{required: true, message: '请输入姓名', trigger: 'blur'}],
        caompanyName: [
          {required: true, message: '请选择所属单位', trigger: 'blur'}
        ]
      },
      form: {
        caompanyName: '',
        orgId: '',
        position: []
      },
      saveLoading: false,
      positonData: []
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    id() {
      return this.rowInfo?.id || ''
    }
  },
  created() {
    this.getPositonData()
  },
  methods: {
    getDictionaryByCode,
    dictionaryEscape,

    // 打开弹层
    open() {
      if (this.id) {
        this.saveLoading = true
        this.getDetail(this.id)
      } else {
        const {id: orgId = '', name: caompanyName = ''} = this.activeTreeItem
        this.form = {...this.form, orgId, caompanyName}
      }
    },

    // 关闭弹层后
    closed() {
      const {rules, positonData} = this
      // 重置组件数据
      Object.assign(this.$data, this.$options.data.call(this), {
        rules,
        positonData
      })

      this.$nextTick(_ => {
        this.$refs.form.$el.scrollTop = 0
        this.$refs.form.clearValidate()
      })
    },

    // 获取岗位数据
    getPositonData() {
      userTypeMenu.getOrgPostPage(refineRequestBody(), res => {
        const {rows = []} = res
        this.positonData = rows.map(i => ({
          ...i,
          positionName: i.name,
          positionId: i.id
        }))
      })
    },

    // 获取详情
    async getDetail(id) {
      try {
        const {position, ...form} = await api.tableDetail(id)

        this.form = {
          ...form,
          position: position.map(i => i.positionId)
        }
      } finally {
        this.saveLoading = false
      }
    },

    // 保存
    handleSave() {
      this.$refs.form.validate(async (valid, object) => {
        if (!valid) {
          this.$message.warning(
            `有${
              Object.keys(object).length
            }个字段未通过校验，请正确填写表单内容。`
          )
        } else {
          this.saveLoading = true
          const myInterFace = this.id ? api.tableEdit : api.tableAdd

          const {...addEditForm} = this.form

          try {
            let {state, message} = await myInterFace({
              ...addEditForm
            })

            if (state) {
              this.$emit('submit', this.id ? 'edit' : 'add')
              message = this.id ? '编辑' : '新增'
              this.$message.success(`人员基本信息${message}成功`)
              this.dialogVisible = false
            }
          } finally {
            this.saveLoading = false
          }
        }
      })
    }
  }
}
</script>

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