<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.exam_addr"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="公安机关">
                    <el-input v-model="searchFrom.police_office"></el-input>
                  </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-button type="primary" size="mini" @click="toAdd()">添加</el-button>
                  <!-- <el-button type="primary" size="mini" @click="toExport">导入文件样本下载</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"
            @edit="toEdit"
            @view="toView"
            @autolayout="autolayout"
            @handlayout="handlayout"
            @againlayout="againlayout"
            @check="toCheck"
          >
          </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog class="showinfo" title="信息详情" :visible.sync="showinfo" width="70%" :before-close="close" :close-on-click-modal="false">
      <info-1 :info="info" :list="infoList"></info-1>
    </el-dialog>
    <el-dialog class="showlayout" title="手动编排" :visible.sync="showlayout" width="50%" :before-close="twoClose" :close-on-click-modal="false">
      <layout-1 :romminfo="romminfo" :list="bmjfList" @layoutSubmit="layoutSubmit"></layout-1>
    </el-dialog>
  </div>
</template>

<script>
import info1 from './parts/info-1.vue';
import layout1 from './parts/layout-1.vue';
const { exam_grade } = require('@common/src/layout/deploy/dict');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: examination_room } = createNamespacedHelpers('examination_room');
const { mapActions: examination_examinee } = createNamespacedHelpers('examination_examinee');
const { mapActions: security_guard_base } = createNamespacedHelpers('security_guard_base');
const { mapActions: sendTemplate } = createNamespacedHelpers('sendTemplate');
const moment = require('moment');
export default {
  name: 'index',
  props: {},
  components: { info1, layout1 },
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '考场编码', model: 'testsite_num' },
        { label: '考试日期', model: 'exam_date' },
        { label: '考试时间', model: 'exam_time' },
        { label: '考试等级', model: 'exam_grade' },
        { label: '考试地点', model: 'exam_addr' },
        { label: '计划参考人数', model: 'test_number' },
        { label: '编排时间', model: 'room_date' },
      ],
      opera: [
        { label: '修改', method: 'edit', type: 'primary', display: (i) => i.status === '0' },
        { label: '详情', method: 'view', type: 'info' },
        { label: '自动编排', method: 'autolayout', type: 'primary', display: (i) => i.status === '0' },
        { label: '手动编排', method: 'handlayout', type: 'primary', display: (i) => i.status === '0' },
        { label: '重新编排', method: 'againlayout', type: 'primary', display: (i) => i.status === '1' },
        { label: '确认编排', method: 'check', type: 'success', display: (i) => i.status === '1' },
      ],
      list: [],
      total: 0,
      // 考试等级
      exam_gradeList: exam_grade,
      // 考场信息详情
      showinfo: false,
      info: {},
      // 考场考生信息
      infoList: [],
      // 手动编排
      showlayout: false,
      romminfo: {},
      bmjfList: [],
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...examination_room(['query', 'update']),
    ...examination_examinee({ bmjfQuery: 'query', bmjfUpdate: 'update' }),
    ...security_guard_base({ baseFetch: 'fetch', baseQuery: 'query', baseUpdate: 'update' }),
    ...sendTemplate(['send']),
    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);
      }
    },
    // 添加
    toAdd() {
      this.$router.push({ path: '/adminPolice/security/kcbp/add' });
    },
    // 修改
    toEdit({ data }) {
      this.$router.push({ path: '/adminPolice/security/kcbp/add', query: { id: data.id } });
    },
    // 详情
    async toView({ data }) {
      let res = await this.bmjfQuery({ room_id: data.id });
      if (this.$checkRes(res)) {
        this.$set(this, `infoList`, res.data);
        this.$set(this, `info`, data);
        this.showinfo = true;
      }
    },
    // 关闭详情
    close() {
      this.showinfo = false;
    },
    // 自动编排
    async autolayout({ data }) {
      // 查询已缴费和未编排的保安员
      let status = '0';
      let res = await this.bmjfQuery({ is_money: '已缴费', status, limit: data.test_number, exam_grade: data.exam_grade });
      if (this.$checkRes(res)) {
        if (res.total > 0) {
          for (const val of res.data) {
            val.status = '1';
            val.seat_num = this.searchNum(res.data, val);
            val.room_id = data.id;
            val.police_office = data.police_office;
            val.exam_date = data.exam_date;
            val.exam_time = data.exam_time;
            val.testsite_num = data.testsite_num;
            val.exam_addr = data.exam_addr;
            val.exam_num = data.testsite_num + val.seat_num;
            let arr = await this.bmjfUpdate(val);
            if (this.$checkRes(arr)) {
              // 编排成功，改变考点状态
              data.status = '1';
              let room = await this.update(data);
              if (this.$checkRes(room)) {
                this.$message({ type: `success`, message: `操作成功` });
              } else {
                this.$message({ type: `fail`, message: `${arr.errmsg}` });
              }
            } else {
              this.$message({ type: `fail`, message: `${arr.errmsg}` });
            }
          }
        } else {
          this.$message({ type: `success`, message: `暂无保安员进行考场编排` });
        }
      }
    },
    // 计算座位号
    searchNum(data, val) {
      let num = data.findIndex((i) => i.card == val.card) + 1;
      if (num.toString().length == 1) num = '00' + num;
      else if (num.toString().length == 2) num = '0' + num;
      else num;
      return num;
    },
    // 手动编排
    async handlayout({ data }) {
      let status = '0';
      let res = await this.bmjfQuery({ is_money: '已缴费', status, limit: data.test_number, exam_grade: data.exam_grade });
      if (this.$checkRes(res)) {
        this.$set(this, `bmjfList`, res.data);
        this.$set(this, `romminfo`, data);
        this.showlayout = true;
      }
    },
    // 手动编排提交
    async layoutSubmit(data) {
      let room = this.romminfo;
      for (const val of data) {
        val.status = '1';
        val.seat_num = this.searchNum(data, val);
        val.room_id = room.id;
        val.police_office = room.police_office;
        val.exam_date = room.exam_date;
        val.exam_time = room.exam_time;
        val.testsite_num = room.testsite_num;
        val.exam_addr = room.exam_addr;
        val.exam_num = room.testsite_num + val.seat_num;
        let arr = await this.bmjfUpdate(val);
        if (this.$checkRes(arr)) {
          room.status = '1';
          let p1 = await this.update(room);
          if (this.$checkRes(p1)) {
            this.$message({ type: `success`, message: `操作成功` });
            this.twoClose();
          } else {
            this.$message({ type: `fail`, message: `${p1.errmsg}` });
          }
        } else {
          this.$message({ type: `fail`, message: `${arr.errmsg}` });
        }
      }
    },
    // 关闭手动编排
    twoClose() {
      this.showlayout = false;
    },
    // 重新编排
    async againlayout({ data }) {
      let res = await this.bmjfQuery({ limit: data.test_number, room_id: data.id });
      if (this.$checkRes(res)) {
        for (const val of res.data) {
          let p1 = {
            exam_num: '',
            police_office: '',
            exam_date: '',
            exam_time: '',
            testsite_num: '',
            exam_addr: '',
            seat_num: '',
            room_id: '',
            status: '0',
            id: val.id,
          };
          let arr = await this.bmjfUpdate(p1);
          if (this.$checkRes(arr)) {
            data.status = '0';
            let room = await this.update(data);
            if (this.$checkRes(room)) {
              this.$message({ type: `success`, message: `操作成功` });
              this.twoClose();
            } else {
              this.$message({ type: `fail`, message: `${arr.errmsg}` });
            }
          }
        }
      }
    },
    // 确认编排
    async toCheck({ data }) {
      let res = await this.bmjfQuery({ is_money: '已缴费', status: '1', room_id: data.id });
      if (this.$checkRes(res)) {
        for (const val of res.data) {
          // 修改报名缴费
          this.updateBmjf({ id: val.id, status: '2' });
          // 修改房间表状态
          this.updateRoom(data);
          // 给考生发送消息
          this.sendTem(data);
        }
      }
    },
    // 修改报名缴费
    async updateBmjf(data) {
      let res = await this.bmjfUpdate(data);
      if (this.$checkRes(res));
    },
    // 修改房间表状态
    async updateRoom(data) {
      data.status = '2';
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `操作完成` });
      } else {
        this.$message({ type: `fail`, message: `${res.errmsg}` });
      }
    },
    // 给考生发放考试通知
    async sendTem(data) {
      let res = await this.bmjfQuery({ room_id: data.id });
      if (this.$checkRes(res)) {
        for (const val of res.data) {
          let p1 = await this.baseFetch({ id: val.security_guard_id });
          if (this.$checkRes(p1)) {
            let p2 = {
              openid: p1.data.gopenid,
              unionid: p1.data.unionid,
              templateId: process.env.VUE_APP_templateId,
              url: '',
              topColor: '',
              data: {
                first: { value: '保安员报名考试考场信息' },
                keyword1: { value: '考场信息' },
                keyword2: { value: '已编排' },
                keyword3: { value: moment(new Date()).format('YYYY-MM-DD') },
                remark: { value: '您的考试信息已发送，请您登录信息查看考试信息！' },
              },
            };
            let p3 = await this.send(p2);
            if (p3.errcode == '0' && p3.wxOpenid) {
              let P4 = await this.baseUpdate({ id: this.basic_id, gopenid: p3.wxOpenid });
              if (this.$checkRes(P4));
            }
          }
        }
      }
    },
    // 导入样本下载
    toExport() {},
  },
  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;
        }
      }
    }
  }
}
.showinfo {
  /deep/.el-dialog__body {
    height: 650px;
    overflow-y: auto;
  }
}
/deep/.el-dialog__body {
  padding: 10px;
}
</style>
