<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="120px">
              <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-input v-model="searchFrom.cert_police"></el-input>
                  </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@receive_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="8">
                  <el-form-item label="至">
                    <el-date-picker
                      v-model="searchFrom['end@receive_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="8">
                  <el-form-item label="打印人">
                    <el-input v-model="searchFrom.print_personal"></el-input>
                  </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@print_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="8">
                  <el-form-item label="至">
                    <el-date-picker
                      v-model="searchFrom['end@print_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="8">
                  <el-form-item label="保安员等级">
                    <el-select v-model="searchFrom.exam_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="24" class="btn">
                  <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="thr">
          <data-table @query="search" :fields="fields" :opera="opera" :data="list" :total="total" @check="toCheck" @view="toView"> </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog class="dialog" title="登记领取信息" :visible.sync="show" width="50%" :before-close="close" :close-on-click-modal="false">
      <check-1 :form="form" @close="close" @onSubmit="onSubmit"></check-1>
    </el-dialog>
    <el-dialog class="dialog" title="详细信息" :visible.sync="twoShow" width="50%" :before-close="twoClose" :close-on-click-modal="false">
      <info-1 :info="info"></info-1>
    </el-dialog>
  </div>
</template>

<script>
import check1 from './parts/check-1.vue';
import info1 from './parts/info-1.vue';
const { exam_grade } = require('@common/src/layout/deploy/dict');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: certificates_grant } = createNamespacedHelpers('certificates_grant');
const { mapActions: certificates_base } = createNamespacedHelpers('certificates_base');
const { mapActions: security_guard_base } = createNamespacedHelpers('security_guard_base');
export default {
  name: 'index',
  props: {},
  components: {
    check1,
    info1,
  },
  data: function () {
    return {
      // 查询表单
      searchFrom: {},
      list: [],
      total: 0,
      fields: [
        { label: '保安员证编码', model: 'baoan_num' },
        { label: '姓名', model: 'name' },
        { label: '身份证号', model: 'card' },
        { label: '保安员等级', model: 'exam_grade' },
        { label: '打印日期', model: 'print_date' },
        { label: '打印人', model: 'print_personal' },
        { label: '发证公安机关', model: 'cert_police' },
        { label: '领取时间', model: 'receive_date' },
      ],
      opera: [
        { label: '登记', method: 'check', display: (i) => i.receive_card == null },
        { label: '详情', method: 'view' },
      ],
      exam_gradeList: exam_grade,
      // 发放登记
      show: false,
      form: {},
      // 信息详情
      twoShow: false,
      info: {},
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...certificates_grant(['query', 'update']),
    ...certificates_base({ certUpdate: 'update' }),
    ...security_guard_base({ baseUpdate: 'update' }),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 登记
    toCheck({ data }) {
      this.$set(this, `form`, data);
      this.show = true;
    },
    // 提交登记,并修改证件表是否领取，修改基本信息表状态：11：领取成功
    async onSubmit({ data }) {
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        // 修改证件表
        this.updateCert(data);
        // 修改基本信息表
        this.updateBase(data);
      }
    },
    // 修改证件表
    async updateCert(data) {
      let arr = { id: data.certificates_id, is_receive: '1', cert_date: data.receive_date };
      let p1 = await this.certUpdate(arr);
      if (this.$checkRes(p1)) {
      } else {
        this.$message({ type: `success`, message: `${p1.errmsg}` });
      }
    },
    // 修改基本信息表
    async updateBase(data) {
      let arr = { id: data.security_guard_id, status: '11' };
      let p2 = await this.baseUpdate(arr);
      if (this.$checkRes(p2)) {
        this.$message({ type: `success`, message: `操作成功` });
        this.show = false;
      } else {
        this.$message({ type: `success`, message: `${p2.errmsg}` });
      }
    },
    // 关闭登记
    close() {
      this.show = false;
    },
    // 查看详情
    toView({ data }) {
      this.$set(this, `info`, data);
      this.twoShow = true;
    },
    // 关闭详情
    twoClose() {
      this.twoShow = false;
    },
  },
  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%;
        }
        .btn {
          text-align: right;
          padding: 5px 5px;
        }
      }
    }
  }
}
</style>
