<template>
  <a-row>
    <a-col :span="4" ref="elw">
      <a-card :body-style="{'height':dxdcMainHeight + 'px','padding':'0', 'overflow': 'auto'}">
        <a-tree
          :tree-data="treeData"
          @select="handleCheckChange"
          :fieldNames="{ children: 'childrenList', title: 'pnm', key: 'id' }"
          :auto-expand-parent="true"
          :expandedKeys.sync="expandedKeys"
          ref="PTTab_OrgTree"
          style="height:100%;">
        </a-tree>
      </a-card>
    </a-col>
    <a-col :span="20">
      <a-card :body-style="{'height':dxdcMainHeight + 'px','padding':'0'}">
        <form ref="formMain" :target="index" :action="option.reportSrc" method="post">
          <input type="hidden" name="width" v-model="windowWidth">
          <input type="hidden" name="height" v-model="windowHeight">
          <input type="hidden" name="groupId" v-model="groupId">
          <input type="hidden" name="excelName" v-model="option.label">
        </form>
        <div style="width: 100%;height: 100%;" :loading="loading">
          <iframe
            :ref="index"
            :id="index"
            :name="index"
            src=""
            style="overflow: auto;border:0;width: 100%;height:100%;"></iframe>
        </div>
      </a-card>
    </a-col>
  </a-row>

</template>

<script>
import requset from '../../utils/gw_ajax_request';
export default {
  components: {},
  props: ['option', 'index'],
  data() {
    return {
      showExport: false,
      dxdcMainHeight: window.innerHeight - 220,
      dxdcAsideHeight: window.innerHeight - 160,
      windowHeight: window.innerHeight - 220,
      windowWidth: window.innerWidth - 350,
      loading: true,
      treeWidth: 300,
      groupId: '',
      treeData: [],
      expandedKeys: [],
    };
  },
  created() {
    this.getTreeAllNode();
  },
  mounted() {},
  methods: {
    // 树节点选中或取消事件
    treeCheckedDataChange(val) {
      let str = '';
      if (val == '360000') {
        str = '';
      } else {
        if (val.endsWith('00')) {
          // if (val.substr(0, 4).endsWith('0')) {
          //   str = val.substr(0, 3)
          // } else {
          str = val.substr(0, 4);
          // }
        } else {
          // if (val.endsWith('0')) {
          //   str = val.substr(0, 5)
          // } else {
          str = val;
          // }
        }
      }
      if (str.length == 0) {
        this.cityCode = '';
        this.countyCode = '';
        this.adCode = '';
      } else if (str.length && (str.length == 3 || str.length == 4)) {
        this.cityCode = str;
        this.countyCode = '';
        this.adCode = str;
      } else {
        this.cityCode = '';
        this.countyCode = str;
        this.adCode = str;
      }
      if (this.adCode == '3604') {
        // 九江
        this.notCode = '360482000000'; // 共青城
      } else if (this.adCode == '3611') {
        // 上饶
        this.notCode = '361128000000'; // 鄱阳
      } else if (this.adCode == '3607') {
        // 赣州
        this.notCode = '360781000000'; // 瑞金
      } else if (this.adCode == '3608') {
        // 吉安
        this.notCode = '360829000000'; // 安福
      } else if (this.adCode == '3610') {
        // 抚州
        this.notCode = '361021000000'; // 南城
      } else if (this.adCode == '3609') {
        // 宜春
        this.notCode = '360981000000'; // 丰城
      } else {
        this.notCode = 'not';
      }
      this.searchExcel();
    },
    // 获取Excel数据
    searchExcel() {
      this.$nextTick(() => {
        /**
         * 1. 开启 loading
         * 2. form 提交表单
         * 3. 当iframe在加载时（显示页面），取消loading
         */
        this.loading = true;
        // 使用form表单post提交，解决参数值过长的问题
        this.$refs.formMain.submit();
        // 使用_this
        const _this = this;
        this.$el.getElementsByTagName('iframe')[0].onload = () => {
          _this.loading = false;
        };
      });
    },
    // 导出Excel数据
    exportExcel() {
      this.$el
        .getElementsByTagName('iframe')[0]
        .contentWindow.report1_saveAsExcel();
    },
    getTreeAllNode() {
      requset
        .get(
          `/dc/organization/base/getTreeAllNode?userId=${this.userId}&orgType=117`,
        )
        .then((res) => {
          this.treeData = res.data;
          this.expandedKeys.push(res.data[0].id);
          this.groupId = res.data[0].id;
        });
    },
    handleCheckChange(data, checked) {
      this.groupId = data[0];
      this.searchExcel();
    },
  },
  computed: {
    userId: () =>
      localStorage.getItem('userid')
        ? localStorage.getItem('userid')
        : '1098101939614724096',
    orgid: () => localStorage.getItem('currentOrgId'),
  },
  activated() {
    this.searchExcel();
  },
};
</script>

<style scoped>
</style>
