<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="140px">
              <el-col :span="24" class="one_2txt">
                <el-col :span="8">
                  <el-form-item label="企业名称">
                    <el-input v-model="searchFrom.company"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="管辖单位">
                    <el-select v-model="searchFrom.tube_company" placeholder="请选择" clearable filterable style="width: 100%">
                      <el-option-group v-for="(item, index) in tube_companyList" :key="index" :label="item.label">
                        <el-option v-for="(ci, cindex) in item.children" :key="`child-${cindex}`" :label="ci.name" :value="ci.name"></el-option>
                      </el-option-group>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="法人姓名">
                    <el-input v-model="searchFrom.legal_name"></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-input v-model="searchFrom.legal_card"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="提交时间">
                    <el-date-picker
                      v-model="searchFrom['start@submit_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@submit_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>
              <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="two">
          <data-table @query="search" :fields="fields" :opera="opera" :data="list" :total="total" @accept="toAccept" @view="toView"> </data-table>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('company_base');
const { mapActions: dept } = createNamespacedHelpers('police_department');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      // 查询
      searchFrom: {},
      // 列表
      fields: [
        { label: '企业名称', model: 'company' },
        { label: '企业类别', model: 'purpose' },
        { label: '法定负责人', model: 'legal_personal' },
        { label: '联系电话', model: 'phone' },
        { label: '提交时间', model: 'submit_date' },
        { label: '管辖单位', model: 'tube_company' },
      ],
      opera: [
        { label: '详情', method: 'view', type: 'info' },
        { label: '受理', method: 'accept' },
      ],
      list: [],
      total: 0,
      tube_companyList: [],
    };
  },
  created() {
    this.initDept();
    this.search();
  },
  methods: {
    ...dept({ getDept: 'query' }),
    ...mapActions(['query']),
    async search({ skip, limit } = {}) {
      const condition = _.cloneDeep(this.searchFrom);
      const res = await this.query({ status: '1', skip, limit, ...condition, '^purpose_id': 'purpose_one,purpose_six' });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 审批
    toAccept({ data }) {
      this.$router.push({ path: '/adminPolice/company/accept', query: { id: data.id } });
    },
    // 详情
    toView({ data }) {
      this.$router.push({ path: '/adminPolice/company/info', query: { id: data.id } });
    },
    /**
     * 初始化管辖单位列表,分组
     */
    async initDept() {
      const res = await this.getDept();
      if (this.$checkRes(res)) {
        // 按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 });
        }
        console.log(data);
        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;
    }
    .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>
