<template>
  <div id="datazs-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_1">审查情况</el-col>
            <el-col :span="24" class="one_2">
              <el-col :span="12" class="one_2info">
                <el-form-item label="是否受到过收容教育，强制隔离戒毒，劳动教养者3次以上行政拘留" prop="check_xzjl">
                  <el-radio-group v-model="form.check_xzjl">
                    <el-radio label="是"></el-radio>
                    <el-radio label="否"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="是否因故意犯罪被刑事处罚" prop="check_xscf">
                  <el-radio-group v-model="form.check_xscf">
                    <el-radio label="是"></el-radio>
                    <el-radio label="否"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="是否吊销保安员证未满3年" prop="check_certthr">
                  <el-radio-group v-model="form.check_certthr">
                    <el-radio label="是"></el-radio>
                    <el-radio label="否"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="是否两次被吊销保安员证" prop="check_certtwo">
                  <el-radio-group v-model="form.check_certtwo">
                    <el-radio label="是"></el-radio>
                    <el-radio label="否"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="疑似精神病人，风险评估不能担任保安员" prop="check_fxpg">
                  <el-radio-group v-model="form.check_fxpg">
                    <el-radio label="是"></el-radio>
                    <el-radio label="否"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12" class="one_2info">
                <el-form-item label="是否有其他不合适担任保安的综合评定" prop="check_zhpd">
                  <el-radio-group v-model="form.check_zhpd">
                    <el-radio label="是"></el-radio>
                    <el-radio label="否"></el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="24" class="one_2info">
                <el-form-item label="审核状态" prop="status">
                  <el-radio-group v-model="form.status">
                    <el-radio label="1">通过</el-radio>
                    <el-radio label="-1">未通过</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: sendTemplate } = createNamespacedHelpers('sendTemplate');
const moment = require('moment');
export default {
  name: 'datazs-1',
  props: {
    basic_id: { type: String },
  },
  components: {},
  data: function () {
    return {
      form: {
        check_xzjl: '否',
        check_xscf: '否',
        check_certthr: '否',
        check_certtwo: '否',
        check_fxpg: '否',
        check_zhpd: '否',
      },
      rules: {
        check_xzjl: [{ required: true, message: '请选择', trigger: 'change' }],
        check_xscf: [{ required: true, message: '请选择', trigger: 'change' }],
        check_certthr: [{ required: true, message: '请选择', trigger: 'change' }],
        check_certtwo: [{ required: true, message: '请选择', trigger: 'change' }],
        check_fxpg: [{ required: true, message: '请选择', trigger: 'change' }],
        check_zhpd: [{ required: true, message: '请选择', trigger: 'change' }],
        status: [{ required: true, message: '请选择', trigger: 'change' }],
      },
    };
  },
  created() {},
  methods: {
    ...security_guard_qualifications(['create']),
    ...security_guard_base(['fetch', 'update']),
    ...sendTemplate(['send']),
    submitBtn(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.$confirm(`您确认提交资料政审吗？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(async () => {
              let data = this.form;
              // 创建政审记录
              this.createQuali(data);
              // 修改基本信息表信息
              this.updateBase(data);
              // 发送通知
              this.sendTem(data);
            })
            .catch(() => {});
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 创建政审
    async createQuali(data) {
      let p1 = {
        check_xzjl: data.check_xzjl,
        check_xscf: data.check_xscf,
        check_certthr: data.check_certthr,
        check_certtwo: data.check_certtwo,
        check_fxpg: data.check_fxpg,
        check_zhpd: data.check_zhpd,
        security_guard_id: this.basic_id,
      };
      let p2 = await this.create(p1);
      if (this.$checkRes(p2));
    },
    // 修改信息表信息
    async updateBase(data) {
      let p1 = {
        status: data.status == '1' ? data.status : '',
        id: this.basic_id,
        acceptance_personal: this.user.name,
        acceptance_police: this.user.department_name,
        acceptance_date: moment(new Date()).format('YYYY-MM-DD'),
      };
      let p2 = await this.update(p1);
      if (this.$checkRes(p2)) {
        this.$message({ type: `success`, message: `操作成功` });
        this.$emit('back');
      } else {
        this.$message({ type: `fail`, message: `${p2.errmsg}` });
      }
    },
    // 发送通知
    async sendTem(data) {
      let res = await this.fetch({ 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.status == '1' ? '通知' : '未通过' },
            keyword3: { value: moment(new Date()).format('YYYY-MM-DD') },
            remark: { value: data.status == '1' ? '报名考试资料已通过政审，您可登录系统进行信息采集预约' : '报名考试资料未通过政审，请您登录系统修改相应信息' },
          },
        };
        let p2 = await this.send(p1);
        if (p2.errcode == '0' && p2.wxOpenid) {
          let p3 = await this.update({ 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_1 {
      text-align: center;
      font-size: 20px;
      font-weight: bold;
    }
    .one_2 {
      .one_2info {
        margin: 10px 0;
        border: 1px solid #ccc;
        /deep/.el-form-item {
          margin: 0;
        }
      }
    }
    .btn {
      text-align: center;
    }
  }
}
</style>
