<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"></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'">
        <info-1 :info="form" :fileList="fileList"></info-1>
      </template>
      <template v-else-if="dialog.type == '2'">
        <form-1 :form="form" @toReset="toClose" @onSubmit="onSubmit"></form-1>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import form1 from './parts/form-1.vue';
import info1 from './parts/info-1.vue';
import search1 from './parts/search-1.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('company_legal_change');
const { mapActions: digital_files } = createNamespacedHelpers('digital_files');
const { mapActions: company_base } = createNamespacedHelpers('company_base');
export default {
  name: 'index',
  props: {},
  components: { search1, info1, form1 },
  data: function () {
    return {
      // 查询
      searchForm: {},
      // 列表
      list: [],
      total: 0,
      fields: [
        { label: '企业名称', model: 'company' },
        { label: '企业类别', model: 'purpose' },
        { label: '法人姓名', model: 'legal_personal' },
        { label: '法人身份证号', model: 'legal_card' },
        { label: '法人联系电话', model: 'legal_phone' },
        { label: '法人代表职务', model: 'legal_zw' },
        { label: '管辖单位名称', model: 'tube_company' },
        { label: '审核结果', model: 'status', format: (i) => (i == '0' ? '待审中' : i == '1' ? '通过' : '拒绝') },
      ],
      opera: [
        { label: '详情', method: 'view' },
        { label: '审核', method: 'check', type: 'danger', display: (i) => i.status == '0' },
      ],
      // 弹框
      dialog: { title: '详细信息', show: false, type: '1' },
      // 详细信息
      form: {},
      // 电子资料
      fileList: [],
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...mapActions(['query', 'fetch', 'update']),
    ...digital_files({ fileQuery: 'query' }),
    ...company_base({ baseUpdate: 'update' }),
    async search({ skip = 0, limit = 10, ...info } = {}) {
      const condition = _.cloneDeep(this.searchForm);
      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);
      }
    },
    // 查看详情
    async toView({ data }) {
      // 详细信息
      let res = await this.fetch({ id: data.id });
      if (this.$checkRes(res)) this.$set(this, `form`, res.data);
      // 电子资料
      res = await this.fileQuery({ main_id: data.id });
      if (this.$checkRes(res)) this.$set(this, `fileList`, res.data);
      this.dialog = { title: '详细信息', show: true, type: '1' };
    },
    // 审核
    async toCheck({ data }) {
      let res = await this.fetch({ id: data.id });
      if (this.$checkRes(res)) this.$set(this, `form`, res.data);
      this.dialog = { title: '信息审核', show: true, type: '2' };
    },
    // 提交审核,当审核通过时，修改企业信息数据
    async onSubmit({ data }) {
      let res = await this.update(data);
      if (res.errcode == '0') {
        if (data.status == '1') this.updateUnit(data);
        this.$message({ type: `success`, message: `审核信息成功` });
        this.toClose();
      } else {
        this.$message({ type: `error`, message: `${res.errmsg}` });
      }
    },
    // 审核通过时，修改企业信息
    async updateUnit(data) {
      let arr = {
        id: data.company_id,
        legal_personal: data.legal_personal,
        legal_card: data.legal_card,
        legal_phone: data.legal_phone,
        legal_zw: data.legal_zw,
      };
      let res = await this.baseUpdate(arr);
      if (res.errcode == '0') {
        this.$message({ type: `success`, message: `企业信息修改成功` });
      } else {
        this.$message({ type: `error`, message: `${res.errmsg}` });
      }
    },
    // 关闭弹框
    toClose() {
      this.info = {};
      this.form = {};
      this.dialog = { title: '详细信息', show: false, type: '1' };
      this.search();
    },
  },
  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>
