<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-input v-model="searchFrom.exam_cardpolice"></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@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.cert_status" clearable placeholder="请选择">
                      <el-option label="正常" value="0"></el-option>
                      <el-option label="注销" value="1"></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@cert_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@cert_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" @view="toview" @del="toDel"> </data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const { exam_grade } = require('@common/src/layout/deploy/dict');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: certificates_base } = createNamespacedHelpers('certificates_base');
const { mapActions: security_guard_base } = createNamespacedHelpers('security_guard_base');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      // 查询表单
      searchFrom: {},
      list: [],
      total: 0,
      fields: [
        { label: '姓名', model: 'name' },
        { label: '身份证号', model: 'card' },
        { label: '出生日期', model: 'birth' },
        { label: '联系电话', model: 'phone' },
        { label: '职业等级', model: 'exam_grade' },
        { label: '发证日期', model: 'cert_date' },
        { label: '保安员编号', model: 'baoan_num' },
        { label: '证件状态', model: 'cert_status', format: (i) => (i == '0' ? '正常' : '注销') },
      ],
      opera: [
        { label: '详情', method: 'view' },
        { label: '保安员证注销', method: 'del', type: 'danger', display: (i) => i.cert_status == '0' },
      ],
      exam_gradeList: exam_grade,
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...certificates_base(['query', '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);
      }
    },
    // 详情
    toview({ data }) {
      this.$router.push({ path: '/adminPolice/security/bayxxcx/info', query: { id: data.security_guard_id } });
    },
    // 保安员证注销,修改证表证件状态，修改基本信息表状态为14，注销人，注销原因。
    async toDel({ data }) {
      this.$confirm(`您确认提交保安员证注销吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          this.updateCert(data);
          this.updateBase(data);
        })
        .catch(() => {});
    },
    // 证表注销
    async updateCert(data) {
      data.cert_status = '1';
      let res = await this.update(data);
      if (this.$checkRes(res));
    },
    // 注銷修改基本信息表数据
    async updateBase(data) {
      let p1 = {
        id: data.security_guard_id,
        status: '14',
        cancel_personal: this.user.name,
        cancel_date: moment(new Date()).format('YYYY-MM-DD'),
      };
      let res = await this.baseUpdate(p1);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `操作成功` });
        this.search();
      } else {
        this.$message({ type: `fail`, message: `${res.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_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>
