<template>
  <div>
    <el-card class="el-card">
      <el-form size="mini" :model="filterQuery" 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" style="width: 90%" placeholder="请选择第三方系统"
                         @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-delete-solid" @click="clearAll"-->
          <!--                     v-if="!this.thirdSysDetail.enabled">清空全部-->
          <!--          </el-button>-->

          <el-button type="primary" icon="el-icon-download" @click="downloadCorps"
                     v-if="this.thirdSysDetail.enabled && this.thirdSysDetail.fromType==3">
            下载生产企业
          </el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="生产企业ID" prop="id" 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-column label="操作" v-if="thirdSysDetail.fromType!=0" width="60">
          <template slot-scope="scope">
            <el-button type="text" @click.native.stop="deleteDialog(scope.row)" :disabled="!delFalg"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="getList"
      ></pagination>
    </el-card>


    <el-dialog
      title="下载生产企业信息信息"
      :visible.sync="thrCorpSelectVisible"
      width="80%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      v-if="thrCorpSelectVisible"
    >
      <thrCorpSelect v-on:cancelDialog="cancelDialog" :isDownThrSys="true"
      ></thrCorpSelect>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCorps, delCorps
} from "@/api/thrsys/thrManus";
import {getBasicThirdSys, filterDetailByKey} from "@/api/basic/basicThirdSys";
import {selectIp} from "@/api/param/systemParamConfig";
import {findConfig} from "@/api/sync/spsSyncStatus";
import thrCorpSelect from "./thrManuSelect";

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

  methods: {
    onReset() {
      // this.$router.push({
      //     path: "",
      // });
      this.filterQuery = {
        unitId: null,
        name: null,
        thirdSys: this.mainThirdSys,
        page: 1,
        limit: 20,
      };
      this.getList();
    },

    getList() {
      if (this.filterQuery.thirdSys == null) {
        this.$message.warning("请先选择第三方系统！")
        return;
      }

      this.loading = true;
      getCorps(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    deleteDialog(row) {
      this.$confirm("此操作将永久删除该生产企业信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {

          let dQuery = {
            id: row.id,
          };
          delCorps(dQuery)
            .then((response) => {
              this.loading = false;
              if (response.code == 20000) {
                this.$message.success("删除成功");
                this.getList();
              } else {
                this.$message.error(response.message);
              }
            })
            .catch(() => {
              this.loading = false;
            });

        })
        .catch(() => {
        });
    },

    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 = [];
        });

    },
    getConFig() {
      findConfig().then((response) => {
        if (response.code == 20000) {
          this.delFalg = response.data.basicThirdCorp;
        } else {
          this.$message.error(response.message);
        }
      });
    },
    downloadDatas() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSys
      };
      downloadAll(query).then((response) => {
        if (response.code == 20000) {
          this.$message.success(response.data);
        } else {
          this.$message.error(response.message);
        }
      });
    },


    downloadCorps() {
      this.selectType = 3;
      this.thrCorpSelectVisible = true;
    },

    cancelDialog(val) {
      this.thrCorpSelectVisible = false;
      if (val) {
        this.getList();
      }
    },
  },
  components: {thrCorpSelect},
  mounted() {
  },
  created() {
    this.getConFig();
    this.getBasicThirdSys();
  },
};


</script>

<style scoped>

</style>
