<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="baoan_examine">
                  <el-input v-model="form.baoan_examine" type="textarea" placeholder="请输入保安员报名审批意见"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="发证审批公安机关" prop="issue_office">
                  <el-input v-model="form.issue_office" placeholder="请输入发证审批公安机关"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="发证审核人" prop="issue_examinepersonal">
                  <el-input v-model="form.issue_examinepersonal" placeholder="请输入发证审核人"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="发证审批时间" prop="issue_date">
                  <el-date-picker v-model="form.issue_date" 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="issue_result">
                  <el-radio-group v-model="form.issue_result">
                    <el-radio label="4">通过</el-radio>
                    <el-radio label="-4">未通过</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 { mapActions: examination_examinee } = createNamespacedHelpers('examination_examinee');
const { mapActions: sendTemplate } = createNamespacedHelpers('sendTemplate');
const moment = require('moment');
export default {
  name: 'datash-1',
  props: {
    basic_id: { type: String },
  },
  components: {},
  data: function () {
    return {
      form: {},
      rules: {
        baoan_examine: [{ required: true, message: '请输入保安员报名审批意见', trigger: 'blur' }],
        issue_office: [{ required: true, message: '请输入发证审批公安机关', trigger: 'blur' }],
        issue_examinepersonal: [{ required: true, message: '请输入发证审核人', trigger: 'blur' }],
        issue_date: [{ required: true, message: '请选择', trigger: 'change' }],
        issue_result: [{ 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', baseFetch: 'fetch' }),
    ...examination_examinee(['create']),
    ...sendTemplate(['send']),
    async search() {
      let res = await this.fetch({ security_guard_id: this.basic_id });
      if (this.$checkRes(res)) {
        res.data.issue_examinepersonal = this.user.name;
        res.data.issue_office = this.user.department_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);
              // 审核通过， 创建报名缴费表信息
              if (data.issue_result == '4') this.createExam(data);
              // 发送通知
              this.sendTem(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) {
      console.log(data);
      let p1 = {
        id: this.basic_id,
        status: data.issue_result,
        approve_status: (data.status = '4' ? '通过' : '未通过'),
        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));
    },
    // 创建报名缴费表信息
    async createExam() {
      let res = await this.baseFetch({ id: this.basic_id });
      if (this.$checkRes(res)) {
        let p1 = {
          name: res.data.name,
          gender: res.data.gender,
          card: res.data.card,
          phone: res.data.phone,
          exam_grade: res.data.grade,
          security_guard_id: this.basic_id,
          exam_type: '第一次考试',
        };
        let p2 = await this.create(p1);
        if (this.$checkRes(p2)) {
          this.$message({ type: `success`, message: `操作成功` });
          this.$emit('back');
        } else {
          this.$message({ type: `success`, message: `${p2.errmsg}` });
        }
      }
    },
    // 发送通知
    async sendTem(data) {
      let res = await this.baseFetch({ id: this.basic_id });
      if (this.$checkRes(res)) {
        let p1 = {
          openid: res.data.gopenid,
          unionid: res.data.unionid,
          templateId: process.env.VUE_APP_templateId,
          url: '',
          topColor: '',
          data: {
            first: { value: '保安员报名考试资格审批' },
            keyword1: { value: '资格审批' },
            keyword2: { value: data.issue_result == '4' ? '通知' : '未通过' },
            keyword3: { value: moment(new Date()).format('YYYY-MM-DD') },
            remark: {
              value:
                data.issue_result == '4' ? '报名考试资料已通过资格审批，请及时联系相关人员进行缴费' : '报名考试资料未通过资格审批，请您登录系统修改相应信息',
            },
          },
        };
        let p2 = await this.send(p1);
        if (p2.errcode == '0' && p2.wxOpenid) {
          let p3 = await this.baseUpdate({ id: this.basic_id, gopenid: p2.wxOpenid });
          if (this.$checkRes(p3));
        }
      }
    },
  },
  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>
