<template>
  <div id="index">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-col :span="24" class="one_1"> 保安员证备案（绑定）审核 </el-col>
          <el-col :span="24" class="one_2">
            <el-form ref="searchFrom" :model="searchFrom" label-width="100px">
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="姓名">
                    <el-input v-model="searchFrom.name"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="公民身份号码">
                    <el-input v-model="searchFrom.card"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="考试等级">
                    <el-select v-model="searchFrom.grade" clearable placeholder="请选择">
                      <el-option v-for="(item, index) in exam_gradeList" :key="index" :label="item" :value="item"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="出生日期">
                    <el-date-picker
                      v-model="searchFrom['start@birth']"
                      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="8">
                  <el-form-item label="至">
                    <el-date-picker
                      v-model="searchFrom['end@birth']"
                      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="8">
                  <el-form-item label="审核结果">
                    <el-select v-model="searchFrom.examine_status" clearable placeholder="请选择">
                      <el-option label="未审核" value="未审核"></el-option>
                      <el-option label="通过" value="通过"></el-option>
                      <el-option label="未通过" value="未通过"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="24" class="one_2txt">
                <el-col :span="6">
                  <el-form-item label="受理时间">
                    <el-date-picker
                      v-model="searchFrom['start@acceptance_date']"
                      type="daterange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="至">
                    <el-date-picker
                      v-model="searchFrom['end@acceptance_date']"
                      type="daterange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="审核时间">
                    <el-date-picker
                      v-model="searchFrom['start@examine_date']"
                      type="daterange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="至">
                    <el-date-picker
                      v-model="searchFrom['end@examine_date']"
                      type="daterange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="报名企业">
                    <el-select v-model="searchFrom.sign_company" filterable clearable placeholder="请选择" style="width: 100%">
                      <el-option v-for="(item, index) in sign_companyList" :key="index" :label="item.name" :value="item.name"> </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :span="24" class="one_2txt">
                <el-col :span="24" class="btn">
                  <el-button type="primary" size="mini" @click="seCheck()">临时人员批量通过备案</el-button>
                  <el-button type="primary" size="mini" @click="search()">查询</el-button>
                </el-col>
              </el-col>
            </el-form>
          </el-col>
        </el-col>
        <el-col :span="24" class="two">
          <data-table
            @query="search"
            :fields="fields"
            :opera="opera"
            :data="list"
            :total="total"
            @view="toView"
            @check="toCheck"
            :select="true"
            :selected="selected"
            @handleSelect="handleSelect"
          >
          </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog :title="dialog.title" :visible.sync="dialog.show" width="30%" :before-close="toClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="保安员编号" prop="baoan_num">
          <el-input v-model="form.baoan_num" placeholder="请输入保安员编号"></el-input>
        </el-form-item>
        <el-form-item label="发证公安机关" prop="cert_police">
          <el-input v-model="form.cert_police" placeholder="请输入发证公安机关"></el-input>
        </el-form-item>
        <el-form-item label="发证日期" prop="cert_date">
          <el-date-picker v-model="form.cert_date" placeholder="请选择" value-format="yyyy-MM-dd" format="yyyy-MM-dd" type="date" style="width: 100%">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="11">通过</el-radio>
            <el-radio label="-11">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="onSubmit('form')">提交备案</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
const { exam_grade } = require('@common/src/layout/deploy/dict');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: security_guard_base } = createNamespacedHelpers('security_guard_base');
const { mapActions: certificates_base } = createNamespacedHelpers('certificates_base');
const { mapActions: company_base } = createNamespacedHelpers('company_base');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '姓名', model: 'name' },
        { label: '公民身份号码', model: 'card' },
        { label: '性别', model: 'gender' },
        { label: '保安员等级', model: 'grade' },
        { label: '采集指纹', model: 'collect_fingerprint' },
        { label: '采集照片', model: 'collect_photo' },
        { label: '审核结果', model: 'examine_status' },
        { label: '审批结果', model: 'approve_status' },
        { label: '受理时间', model: 'acceptance_date' },
        { label: '受理公安机关', model: 'acceptance_police' },
      ],
      opera: [
        { label: '详情', method: 'view' },
        { label: '备案', method: 'check' },
      ],
      list: [],
      total: 0,
      // 考试等级
      exam_gradeList: exam_grade,
      sign_companyList: [],
      // 备案审核
      dialog: { title: '证件信息', show: false },
      form: {},
      rules: {
        baoan_num: [{ required: true, message: '请输入保安员编号', trigger: 'blur' }],
        cert_police: [{ required: true, message: '请输入发证公安机关', trigger: 'blur' }],
        cert_date: [{ required: true, message: '请选择发证日期', trigger: 'change' }],
        status: [{ required: true, message: '审核状态', trigger: 'change' }],
      },
      // 多选
      selected: [],
    };
  },
  created() {
    this.searchOther();
    this.search();
  },
  methods: {
    ...security_guard_base(['query', 'update']),
    ...certificates_base(['create']),
    ...company_base({ companyQuery: 'query' }),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, status: '10', ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 详情
    toView({ data }) {
      this.$router.push({ path: '/adminPolice/security/baoanbash/info', query: { id: data.id } });
    },
    // 审核备案
    async toCheck({ data }) {
      let arr = {
        name: data.name,
        phone: data.phone,
        gender: data.gender,
        card: data.card,
        birth: data.birth,
        house_onaddress: data.house_onaddress,
        exam_grade: data.grade,
        cert_status: '0',
        print_date: moment(new Date()).format('YYYY-MM-DD'),
        is_cert: '0',
        is_receive: '0',
        security_guard_id: data.id,
      };
      this.$set(this, `form`, arr);
      this.dialog = { title: '证件信息', show: true };
    },
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let data = this.form;
          this.updateBase(data);
          if (data.status == '11') {
            data.status = undefined;
            let res = await this.create(data);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `操作成功` });
            }
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    async updateBase(data) {
      let res = await this.update({ id: data.security_guard_id, status: data.status == '11' ? data.status : '' });
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `操作成功` });
        this.search();
        this.toClose();
      }
    },
    // 取消
    toClose() {
      this.dialog = { title: '证件信息', show: false };
    },
    // 批量选择打印数
    handleSelect(data) {
      this.$set(this, `selected`, data);
    },
    // 临时人员批量审核
    async seCheck() {
      let data = this.selected;
      if (data) {
        this.$confirm(`您确定要批量备案保安员吗`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            const loading = this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)',
            });
            for (const [index, val] of data.entries()) {
              if (val && val.id) {
                this.updateBase({ security_guard_id: val.id, status: '11' });
                let arr = {
                  name: val.name,
                  phone: val.phone,
                  gender: val.gender,
                  card: val.card,
                  birth: val.birth,
                  house_onaddress: val.house_onaddress,
                  exam_grade: val.grade,
                  cert_status: '0',
                  print_date: moment(new Date()).format('YYYY-MM-DD'),
                  cert_date: moment(new Date()).format('YYYY-MM-DD'),
                  is_cert: '0',
                  is_receive: '0',
                  security_guard_id: val.id,
                  cert_police: this.user.department_name,
                  baoan_num: 'LS' + moment(new Date(), 'YYYY-MM-DD HH:mm:ss').valueOf() + index,
                };
                let p1 = await this.create(arr);
                if (this.$checkRes(p1)) {
                  this.$message({ type: `success`, message: `操作成功` });
                  this.search();
                }
              }
            }
            loading.close();
          })
          .catch(() => {});
      } else {
        this.$message({ type: `warning`, message: `请选择数据` });
      }
    },
    async searchOther() {
      // 报名企业
      let res = await this.companyQuery({ status: '3' });
      if (this.$checkRes(res)) {
        res.data.push({ name: '无', id: '无' });
        this.$set(this, `sign_companyList`, res.data);
      }
    },
  },
  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 {
      height: 40px;
      line-height: 40px;
      border-bottom: 1px solid #ccc;
      margin: 0 0 10px 0;
      font-size: 18px;
      font-weight: bold;
    }
    .one_2 {
      .one_2txt {
        border: 1px solid #ccc;
        /deep/.el-form-item {
          margin: 0;
        }
        /deep/.el-date-editor.el-input,
        .el-date-editor.el-input__inner {
          width: 100%;
        }
        /deep/.el-select {
          width: 100%;
        }
        .other {
          padding: 10px;
        }
        .btn {
          text-align: right;
          padding: 5px 5px;
        }
      }
    }
  }
}
</style>
