<template>
  <div id="companyServiceArea">
    <el-row>
      <el-col :span="24" class="main">
        <el-col :span="24" class="one">
          <el-col :span="24" class="title">{{ title }}</el-col>
          <el-col :span="24" class="form">
            <el-form ref="searchForm" :model="searchForm" label-width="140px">
              <el-col :span="8" class="one_1">
                <el-form-item label="区域名称">
                  <el-input v-model="searchForm.service_name"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8" class="one_1">
                <el-form-item label="区域详址">
                  <el-input v-model="searchForm.address_detailed"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8" class="one_1">
                <el-form-item label="区域省市县">
                  <el-input v-model="searchForm.onhouse_city"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8" class="one_1">
                <el-form-item label="区域负责人姓名">
                  <el-input v-model="searchForm.name"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8" class="one_1">
                <el-form-item label="区域负责人联系电话">
                  <el-input v-model="searchForm.phone"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8" class="one_1">
                <el-form-item label="区域负责人身份证号">
                  <el-input v-model="searchForm.card"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24" class="btn">
                <el-button type="primary" size="mini" @click="search()">查询</el-button>
              </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" @view="toView" @material="toMaterial"> </data-table>
        </el-col>
      </el-col>
    </el-row>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialog" center :destroy-on-close="true" @closed="toClose">
      <el-form ref="info" :model="info" label-width="auto">
        <el-col :span="12">
          <el-form-item label="区域名称" prop="service_name">
            <el-input v-model="info.service_name" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="区域详址" prop="address_detailed">
            <el-input v-model="info.address_detailed" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="区域省市县" prop="onhouse_city">
            <el-input v-model="info.onhouse_city" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人姓名" prop="name">
            <el-input v-model="info.name" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人身份证号" prop="card">
            <el-input v-model="info.card" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="info.phone" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="区域负责人职务" prop="zw">
            <el-input v-model="info.zw" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="保安服务类型" prop="baoan_sevicetype">
            <el-input v-model="info.baoan_sevicetype" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-dialog title="电子资料信息" :close-on-click-modal="false" :visible.sync="mdialog" center :destroy-on-close="true" @closed="toClose">
      <data-table :fields="filefields" :opera="fileopera" :data="filelist" :usePage="false" @download="toDownload"> </data-table>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('company_service_area');
const { mapActions: mapfiles } = createNamespacedHelpers('digital_files');
const _ = require('lodash');
export default {
  name: 'companyServiceArea',
  props: {
    title: { type: String },
  },
  components: {},
  data: function () {
    return {
      // 查询
      searchForm: {},
      list: [],
      total: 0,
      fields: [
        { label: '区域名称', model: 'service_name' },
        { label: '区域详址', model: 'address_detailed' },
        { label: '负责人姓名', model: 'name' },
        { label: '负责人身份证号', model: 'card' },
        { label: '联系电话', model: 'phone' },
      ],
      opera: [
        { label: '详情', method: 'view' },
        { label: '资料管理', method: 'material' },
      ],
      // 弹框
      dialog: false,
      // 详情
      info: {},
      // 电子资料信息
      mdialog: false,
      filefields: [
        { label: '电子材料类型', model: 'type' },
        { label: '电子材料名称', model: 'name' },
        { label: '文件地址', model: 'el_img' },
      ],
      fileopera: [{ label: '下载', method: 'download' }],
      filelist: [],
    };
  },
  async created() {
    if (this.id) await this.search();
  },
  methods: {
    ...mapActions(['query', 'fetch', 'create', 'update']),
    ...mapfiles({ filesQuery: 'query' }),
    async search({ skip, limit } = {}) {
      const condition = _.cloneDeep(this.searchForm);
      const res = await this.query({ skip, limit, ...condition, company_id: this.id });
      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, `info`, res.data);
        this.$set(this, `dialog`, true);
      }
    },
    // 查看电子资料
    toMaterial({ data }) {
      this.$set(this, `info`, data);
      this.searchFiles();
      this.$set(this, `mdialog`, true);
    },
    async searchFiles() {
      let res = await this.filesQuery({ main_id: this.info.id });
      if (this.$checkRes(res)) {
        this.$set(this, `filelist`, res.data);
      }
    },
    // 下载
    toDownload({ data }) {
      window.open(`${process.env.VUE_APP_HOST}${data.el_img}`, '_blank');
    },
    // 关闭弹框
    toClose() {
      this.$set(this, `dialog`, false);
      this.$set(this, `mdialog`, false);
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.one {
  .title {
    font-weight: bold;
    font-size: 18px;
    padding: 10px 0;
    border-bottom: 1px solid #cccccc;
    margin: 0 0 10px 0;
  }
  .form {
    .one_1 {
      padding: 0 10px;
      /deep/.el-form-item {
        margin: 0;
      }
    }
    .btn {
      text-align: right;
      margin: 10px 0 0 0;
      padding: 0 10px;
    }
  }
}
/deep/.el-dialog__body {
  max-height: 430px;
  overflow: auto;
}
.dialog {
  .btn {
    text-align: center;
  }
}
</style>
