<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="单位编码:">
              <el-input v-model="filterQuery.unitId" placeholder="请输入往来单位编码" style="width: 90%" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="往来单位:">
              <el-input v-model="filterQuery.name" placeholder="请输入往来单位" style="width: 90%" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="外部系统:">
              <el-select v-model="filterQuery.thirdSys" placeholder="请选择第三方系统" style="width: 90%"
                         @change="thirdSysChange">
                <el-option
                  v-for="item in thirdSys"
                  :key="item.value"
                  :label="item.thirdName"
                  :value="item.thirdId">
                  <span style="float: left">{{ item.thirdName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.thirdId }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
          <el-button type="primary" icon="el-icon-upload2" @click="selectExport" v-if="selectType==0">选中导出</el-button>
          <el-button type="primary" icon="el-icon-upload2" @click="allExport" v-if="selectType==0">结果全部导出</el-button>
          <el-button type="primary" icon="el-icon-top-upload" @click="selectUpload" v-if="selectType==1">选中上传</el-button>
          <el-button type="primary" icon="el-icon-top-upload" @click="allUpload" v-if="selectType==1">一键上传</el-button>
          <el-button type="primary" icon="el-icon-download" @click="downloadErp" v-if="selectType==3">选中下载</el-button>
          <el-button type="primary" icon="el-icon-download" @click="downloadAllErp" v-if="selectType==3">结果全部下载</el-button>
        </el-button-group>
      </div>

      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                border highlight-current-row>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="往来单位ID" prop="unitId" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="往来单位" prop="name" show-overflow-tooltip></el-table-column>
        <el-table-column label="拼音简写" prop="spell" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="地址" prop="addr" show-overflow-tooltip></el-table-column>
        <el-table-column label="社会信用号" prop="creditNo" width="140" show-overflow-tooltip></el-table-column>
        <el-table-column label="联系人" prop="contact" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column label="联系电话" prop="mobile" show-overflow-tooltip></el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import {corpsDlAll} from "@/api/thrsys/thrCorps";
import {getBasicThirdSys, filterDetailByKey} from "@/api/basic/basicThirdSys";
import {ucloudUnit} from "@/api/basic/basicUnitMaintain";
import {selectIp} from "@/api/param/systemParamConfig";
import {exportExcel, uploadSmp} from "@/api/thrsys/thrCorpsExport"

export default {

  name: "ThrCorpSelect",
  props: {
    selectType: {
      type: Object,
      required: true,
    },
    isDownThrSys: {
      type: Object,
      required: true,
    },
  },

  data() {
    return {
      showSearch: true,
      filterQuery: {
        thrCorpEntities: [],
        unitId: null,
        name: null,
        thirdSys: null,
        page: 1,
        limit: 20,
        isDownThrSys: null,
      },
      mainThirdSys:'',
      total: 0,
      list: [],
      thirdSys: [],
      thirdSysDetail: null,
      uploadFileUrl: null,
      fileList: [],
      uploadData: {
        thirdSys: "thirdId",
      },
      loading: false,
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        thrCorpEntities: [],
        unitId: null,
        name: null,
        thirdSys: this.mainThirdSys,
        page: 1,
        limit: 20,
        isDownThrSys: null,
      };
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    getList() {
      this.loading = true;
      this.filterQuery.isDownThrSys = this.isDownThrSys;
      ucloudUnit(this.filterQuery)
        .then((response) => {
          this.loading = false;
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }

        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getBasicThirdSys() {
      let query = {
        enabled: true,
      };
      getBasicThirdSys(query)
        .then((response) => {
          this.thirdSys = response.data.list || [];
          for(var i=0;i<this.thirdSys.length;i++){
            if(this.thirdSys[i].mainSys){
              this.filterQuery.thirdSys = this.thirdSys[i].thirdId;
              this.mainThirdSys = this.thirdSys[i].thirdId;
            }
          }
          this.uploadData.thirdSys = this.filterQuery.thirdSys;
          this.getThirdSysDetail();
          this.selectSysParam();
          this.getList();
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },
    handleChange(response, files, fileList) {
      console.log(response);
      if (response.code != 20000) {
        this.$message.error(response.message);
      } else {
        console.log(files[0] + "\n" + this.fileList[0] + "\n" + fileList[0]);
        this.$message.success("文件上传成功,请稍后刷新查看！");
      }
    },
    selectSysParam() {
      let query = {
        key: "thirdIpUrl",
        thirdSysFk: this.filterQuery.thirdSys
      };
      selectIp(query).then((response) => {
        if (response.code == 20000) {
          this.uploadFileUrl = response.data.thridUrl + "/udiwms/erp/corp/upload";
        }
      });
    },
    thirdSysChange() {
      this.uploadData.thirdSys = this.filterQuery.thirdSys;
      // this.getThirdSysDetail();
    },
    getThirdSysDetail() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSys,
        key: "corpUrl",
      };
      filterDetailByKey(query)
        .then((response) => {
          this.thirdSysDetail = response.data;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },

    downloadErp() {
      var selectData = this.multipleSelection;
      selectData.forEach((obj) => {
        this.filterQuery.thrCorpEntities.push(obj);
      });
      this.downloadAllErp();
    },

    downloadAllErp() {
      corpsDlAll(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message.success("提交成功，后台正在下载！")
          } else {
            this.$message.error(response.message)
          }
          this.$emit("cancelDialog", true);
    })
        .catch(() => {
          this.$message.error("下载失败！");
        });
    },

    selectExport() {
      var selectData = this.multipleSelection;
      selectData.forEach((obj) => {
        this.filterQuery.thrCorpEntities.push(obj);
      });
      this.allExport();
    },
    allExport() {
      exportExcel(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message({
              type: "success",
              message: "导出成功，后台正在生成Json文件，请稍后刷新查看！",
            });
          } else {
            this.$message({
              type: "error",
              message: response.message,
            });
          }
          this.$emit("cancelDialog", true);
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "上传失败",
          });
        });
    },

    selectUpload() {
      var selectData = this.multipleSelection;
      selectData.forEach((obj) => {
        this.filterQuery.thrCorpEntities.push(obj);
      });
      this.allUpload();
    },
    allUpload() {
      uploadSmp(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$message({
              type: "success",
              message: "上传成功！！",
            });
          } else {
            this.$message({
              type: "error",
              message: response.message,
            });
          }
          this.$emit("cancelDialog", true);
        })
        .catch(() => {
          this.$message({
            type: "error",
            message: "上传失败",
          });
        });
    },
  },
  components: {},
  mounted() {
  },
  created() {
    // this.getList();
    this.getBasicThirdSys();
  },
};


</script>

<style scoped>

</style>
