<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">
            <search-1 :form="searchForm" @onSubmit="search" :tube_companyList="tube_companyList"></search-1>
          </el-col>
        </el-col>
        <el-col :span="24" class="thr">
          <data-table @query="search" :fields="fields" :opera="opera" :data="list" :total="total" @view="toView" @check="toCheck"> </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog class="dialog" :title="dialog.title" :visible.sync="dialog.show" width="60%" :before-close="toClose" :close-on-click-modal="false">
      <template v-if="dialog.type == '1'">
        <form-1 :form="form" :info="info" @toReset="toClose" @onSubmit="onSubmit" :statusList="statusList"></form-1>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import form1 from '../one/parts/form-1.vue';
import search1 from '../one/parts/search-1.vue';
const _ = require('lodash');
const moment = require('moment');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('company_base');
const { mapActions: dept } = createNamespacedHelpers('police_department');
const { mapActions: examine } = createNamespacedHelpers('company_examine');
export default {
  name: 'index',
  props: {},
  components: { search1, form1 },
  data: function () {
    return {
      // 查询
      searchForm: {},
      // 管辖单位
      deptList: [],
      tube_companyList: [],
      list: [],
      total: 0,
      fields: [
        { label: '企业名称', model: 'company' },
        { label: '企业类别', model: 'purpose' },
        { label: '法定负责人', model: 'legal_personal' },
        { label: '联系电话', model: 'phone' },
        { label: '提交时间', model: 'submit_date' },
        {
          label: '管辖单位',
          model: 'tube_company',
          format: (i) => {
            let data = this.deptList.find((r) => r.id == i);
            if (data) return data.name;
          },
        },
      ],
      opera: [
        { label: '详情', method: 'view' },
        { label: '审核', method: 'check', type: 'danger', display: (i) => i.status == '4' },
      ],
      // 审核
      dialog: { title: '审核信息', show: false, type: '1' },
      info: {},
      form: {},
      // 审核状态
      statusList: [
        { label: '通过', value: '5' },
        { label: '拒绝', value: '2' },
      ],
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...mapActions(['query', 'fetch', 'update']),
    ...dept({ getDept: 'query' }),
    ...examine({ examineCreate: 'create' }),
    async search({ skip, limit } = {}) {
      // 状态为：1
      // todo:查询该管辖下的注册企业，关联派出所，分局，市局（查询全部）
      const condition = _.cloneDeep(this.searchForm);
      const res = await this.query({ skip, limit, ...condition, purpose_id: 'purpose_five', status: '4' });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 详细信息
    toView({ data }) {
      this.$router.push({ path: '/adminPolice/companyInfo', query: { id: data.id, purpose_id: data.purpose_id } });
    },
    // 审核信息
    async toCheck({ data }) {
      let res = await this.fetch({ id: data.id });
      if (this.$checkRes(res)) {
        this.$set(this, `info`, res.data);
        this.dialog = { title: '审核信息', show: true, type: '1' };
      }
    },
    // 提交审核
    async onSubmit({ data }) {
      data.company_id = this.info.id;
      let res = await this.examineCreate(data);
      if (res.errcode == '0') {
        // 修改企业信息
        this.updateUnit({ id: data.company_id, status: data.status, status_two: data.status_two });
        this.$message({ type: `success`, message: `审核信息创建成功` });
        this.toClose();
      } else {
        this.$message({ type: `error`, message: `${res.errmsg}` });
      }
    },
    // 修改企业信息
    async updateUnit(data) {
      let res = await this.update(data);
      if (res.errcode == '0') {
        this.$message({ type: `success`, message: `企业信息审核成功` });
      } else {
        this.$message({ type: `error`, message: `${res.errmsg}` });
      }
    },
    // 关闭弹框
    toClose() {
      this.dialog = { title: '审核信息', show: false, type: '1' };
      this.search();
    },
    // 查询其他信息
    async searchOther() {
      const res = await this.getDept();
      if (this.$checkRes(res)) {
        this.$set(this, `deptList`, res.data);
        // 按type分类,1:市局;2,分局;3,派出所
        let mid = _.cloneDeep(res.data);
        mid = _.groupBy(mid, 'type');
        let data = [];
        let keys = ['1', '2', '3'];
        for (const key of keys) {
          let label = '';
          let children;
          if (key === '1') {
            label = '市局';
            children = mid[key];
          } else if (key === '2') {
            label = '分局';
            children = mid[key];
          } else if (key === '3') {
            label = '派出所';
            children = mid[key];
          }
          if (children) data.push({ label, children });
        }
        this.$set(this, `tube_companyList`, 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;
    }
  }
}
</style>
