<template>
  <div id="datash-1">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-form :model="form" :rules="rules" ref="form" label-width="500px">
            <el-col :span="24" class="one_2">
              <el-col :span="24" class="one_2info">
                <el-form-item label="报名考试审核意见" prop="exam_examinereason">
                  <el-input v-model="form.exam_examinereason" type="textarea" placeholder="请输入报名考试审核意见"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="报名考试审核机关" prop="exam_office">
                  <el-input v-model="form.exam_office" placeholder="请输入报名考试审核机关"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="报名考试审核人" prop="exam__examine">
                  <el-input v-model="form.exam__examine" placeholder="请输入报名考试审核人"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="报名考试审核时间" prop="baoan_examinedate">
                  <el-date-picker
                    v-model="form.baoan_examinedate"
                    placeholder="请选择"
                    value-format="yyyy-MM-dd"
                    format="yyyy-MM-dd"
                    type="date"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="审核状态" prop="exam_examineresult">
                  <el-radio-group v-model="form.exam_examineresult">
                    <el-radio label="3">通过</el-radio>
                    <el-radio label="-3">未通过</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
            </el-col>
            <el-col :span="24" class="btn">
              <el-button type="primary" @click="submitBtn('form')">提交审核</el-button>
            </el-col>
          </el-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: security_guard_qualifications } = createNamespacedHelpers('security_guard_qualifications');
const { mapActions: security_guard_base } = createNamespacedHelpers('security_guard_base');
const moment = require('moment');
export default {
  name: 'datash-1',
  props: {
    basic_id: { type: String },
  },
  components: {},
  data: function () {
    return {
      form: {},
      rules: {
        exam_examinereason: [{ required: true, message: '请输入报名考试审核意见', trigger: 'blur' }],
        exam_office: [{ required: true, message: '请输入报名考试审核机关', trigger: 'blur' }],
        exam__examine: [{ required: true, message: '请输入报名考试审核人', trigger: 'blur' }],
        baoan_examinedate: [{ required: true, message: '请选择', trigger: 'change' }],
        exam_examineresult: [{ required: true, message: '请选择', trigger: 'change' }],
      },
    };
  },
  async created() {
    if (this.basic_id) await this.search();
  },
  methods: {
    ...security_guard_qualifications(['fetch', 'update']),
    ...security_guard_base({ baseUpdate: 'update' }),
    async search() {
      let res = await this.fetch({ security_guard_id: this.basic_id });
      if (this.$checkRes(res)) {
        res.data.exam_office = this.user.department_name;
        res.data.exam__examine = this.user.name;
        this.$set(this, `form`, res.data);
      }
    },
    submitBtn(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.$confirm(`您确认报名资格审核吗？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(async () => {
              let data = this.form;
              // 修改政审资料表
              this.updateQuali(data);
              // 修改基本信息表信息
              this.updateBase(data);
            })
            .catch(() => {});
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 修改政审资料表
    async updateQuali(data) {
      let p1 = await this.update(data);
      if (this.$checkRes(p1));
    },
    // 修改基本信息表信息
    async updateBase(data) {
      let p1 = {
        id: data.security_guard_id,
        status: data.exam_examineresult,
        examine_status: data.exam_examineresult == '3' ? '通过' : '未通过',
        acceptance_personal: this.user.name,
        acceptance_police: this.user.department_name,
        acceptance_date: moment(new Date()).format('YYYY-MM-DD'),
      };
      let p2 = await this.baseUpdate(p1);
      if (this.$checkRes(p2)) {
        this.$message({ type: `success`, message: `操作成功` });
        this.$emit('back');
      } else {
        this.$message({ type: `fail`, message: `${p2.errmsg}` });
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    .one_2 {
      .one_2info {
        margin: 10px 0;
        border: 1px solid #ccc;
        /deep/.el-form-item {
          margin: 0;
        }
      }
    }
    .btn {
      text-align: center;
    }
  }
}
</style>
