<template>
  <el-dialog
    :title="`${id ? '编辑' : '新增'} - 编制岗位数据`"
    :visible.sync="dialogVisible"
    custom-class="new-edition-dialog"
    width="60%"
    @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="8">
          <el-form-item label="单位名称" prop="companyName">
            <el-input
              v-model="form.companyName"
              clearable
              placeholder="请输入单位名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="编制岗位情况" prop="postInfo">
            <el-input
              v-model="form.postInfo"
              clearable
              placeholder="请输入编制岗位情况"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="军官数量" prop="officerNum">
            <el-input
              v-model="form.officerNum"
              clearable
              placeholder="请输入军官数量"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="军士数量" prop="ncoNum">
            <el-input
              v-model="form.ncoNum"
              clearable
              placeholder="请输入军士数量"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="义务兵数量" prop="dutyNum">
            <el-input
              v-model="form.dutyNum"
              clearable
              placeholder="请输入义务兵数量"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="文职人员数量" prop="civilianNum">
            <el-input
              v-model="form.civilianNum"
              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/postDataMenu'

export default {
  name: 'AddEditDialog',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    rowInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      rules: {
        companyName: [
          {required: true, message: '请输入单位名称', trigger: 'blur'}
        ]
      },
      form: {},
      saveLoading: false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.value
      },
      set(val) {
        this.$emit('input', val)
      }
    },
    id() {
      return this.rowInfo?.id || ''
    }
  },
  created() {},
  methods: {
    // 打开弹层
    open() {
      if (this.id) {
        this.saveLoading = true
        this.getDetail(this.id)
      }
    },

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

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

    // 获取详情
    async getDetail(id) {
      try {
        const {startTime = '', endTime = '', ...form} = await api.tableDetail(
            id
          ),
          startEndTime = startTime && endTime ? [startTime, endTime] : ''
        this.form = {...form, startEndTime}
      } 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 {
            startEndTime: [startTime = '', endTime = ''] = [],
            ...addEditForm
          } = this.form

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

            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>
