<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="80px">
              <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="24" class="btn">
                  <el-button type="primary" size="mini" @click="search()">查询</el-button>
                  <el-button type="primary" size="mini" @click="toAdd()">添加</el-button>
                  <download-excel :data="selected" :fields="excel_fields.fields" :name="excel_fields.name" style="display: inline; margin: 0 0 0 10px">
                    <el-button type="primary" size="mini">批量导出</el-button>
                  </download-excel>
                </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"
            @edit="toEdit"
            @view="toView"
            @check="toCheck"
            @del="toDel"
            :select="true"
            :selected="selected"
            @handleSelect="handleSelect"
          >
          </data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const { exam_grade, baon_base } = require('@common/src/layout/deploy/dict');
import { mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('system_account');
const { mapActions: security_guard_base } = createNamespacedHelpers('security_guard_base');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      // 用户信息
      user: {},
      // 查询表单
      searchFrom: {},
      list: [],
      total: 0,
      fields: [
        { label: '姓名', model: 'name' },
        { label: '身份证号', model: 'card' },
        { label: '性别', model: 'gender' },
        { label: '保安员等级', model: 'grade' },
        { label: '出生日期', model: 'birth' },
        { label: '采集照片', model: 'collect_photo' },
        { label: '采集指纹', model: 'collect_fingerprint' },
        { label: '审核结果', model: 'examine_status' },
        { label: '审批结果', model: 'approve_status' },
        { label: '受理时间', model: 'acceptance_date' },
        { label: '受理公安机关', model: 'acceptance_police' },
      ],
      // 导出数据表头配置文件
      excel_fields: baon_base,
      opera: [
        { label: '详情', method: 'view', type: 'info' },
        { label: '修改', method: 'edit', type: 'primary', display: (i) => i.status == null },
        { label: '确认报名', method: 'check', type: 'primary', display: (i) => i.status == null },
        { label: '报名注销', method: 'del', type: 'danger', display: (i) => i.status == '1' || i.status == '2' || i.status == '3' || i.status == '4' },
      ],
      // 保安员等级
      exam_gradeList: exam_grade,
      // 批量导出列表
      selected: [],
    };
  },
  async created() {},
  mounted() {
    this.init();
  },
  methods: {
    ...mapActions(['fetch']),
    ...security_guard_base(['query', 'update']),
    async init() {
      if (!_.get(this.tokenInfo, 'id')) return;
      const res = await this.fetch({ id: this.tokenInfo.id });
      if (this.$checkRes(res)) {
        let data = _.cloneDeep(res.data);
        this.$set(this, `user`, data);
        this.search();
      }
    },
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      let res = await this.query({ skip, limit, house_police: this.user.department_name, status: 'null', ...condition, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 添加
    toAdd() {
      this.$router.push({ path: '/adminPolice/security/baybm/add' });
    },
    // 修改
    toEdit({ data }) {
      this.$router.push({ path: '/adminPolice/security/baybm/add', query: { id: data.id } });
    },
    // 详情
    toView({ data }) {
      this.$router.push({ path: '/adminPolice/security/baybm/info', query: { id: data.id } });
    },
    // 确认报名
    async toCheck({ data }) {
      this.$confirm(`您确认提交报名吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          data.status = '0';
          data.acceptance_personal = this.user.name;
          data.acceptance_police = this.user.department_name;
          data.acceptance_date = moment(new Date()).format('YYYY-MM-DD');
          let res = await this.update(data);
          if (this.$checkRes(res)) {
            this.$message({ type: `success`, message: `操作成功` });
            this.search();
          } else {
            this.$message({ type: `fail`, message: `${res.errmsg}` });
          }
        })
        .catch(() => {});
    },
    // 报名注销
    async toDel({ data }) {
      this.$confirm(`您确认提交报名考试注销吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          data.status = '13';
          data.cancel_personal = this.user.name;
          data.cancel_date = moment(new Date()).format('YYYY-MM-DD');
          let res = await this.update(data);
          if (this.$checkRes(res)) {
            this.$message({ type: `success`, message: `操作成功` });
            this.search();
          } else {
            this.$message({ type: `fail`, message: `${res.errmsg}` });
          }
        })
        .catch(() => {});
    },
    // 批量选择打印数
    handleSelect(data) {
      this.$set(this, `selected`, data);
    },
  },
  computed: {
    ...mapGetters(['tokenInfo']),
  },
  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>
