<template>
  <div>
    <el-card class="el-card">
      <el-form :model="unitQuery" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="往来单位编码:">
              <el-input v-model="unitQuery.unitId" style="width: 90%" placeholder="请输入往来单位编码" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="请设置主系统:">
              <el-select v-model="thirdSysFk" style="width: 90%" placeholder="请设置主系统" disabled>
                <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="search">查询</el-button>
          <el-button type="primary" size="mini" icon="el-icon-bottom-right" @click="combine">选入</el-button>
          <el-button type="primary" size="mini" icon="el-icon-bottom-right" @click="combineAll">结果全部选入</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table :data="unitlList" style="width: 100%" @selection-change="handleSelectionUdiChange" border
                highlight-current-row>
        <el-table-column type="selection" width="55" :selectable="checkSelectable"></el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="往来单位编码" prop="unitId"></el-table-column>
        <el-table-column label="往来单位名称" prop="name" show-overflow-tooltip></el-table-column>
        <el-table-column label="往来单位简写" show-overflow-tooltip prop="spell"></el-table-column>
        <el-table-column label="地址" prop="addr" show-overflow-tooltip></el-table-column>
      </el-table>
    </el-card>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="unitQuery.page"
      :limit.sync="unitQuery.limit"
      @pagination="handlepageChange"

    ></pagination>
  </div>
</template>

<script>
import {getBasicThirdSys} from "@/api/basic/basicThirdSys";
import {getCorps} from "@/api/thrsys/thrManus";
import {combine, combineAll} from "@/api/basic/basicManuMaintain";

export default {
  data() {
    return {
      showSearch: true,
      unitQuery: {
        page: 1,
        limit: 10,
        key: "",
        thirdSys: null,
      },
      combineQuery: {
        thirdSys: "",
        keys: [],
        custmandocResponses: [],
      },
      total: 0,
      erpTotal: 0,
      checked: false,
      unitlList: [],
      erpList: [],
      currentRow: null,
      multipleUdiSelection: [],
      thirdSys: [],
      thirdSysFk: null,
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.unitQuery = {
        key: "",
        page: 1,
        limit: 10,
        thirdSys: null,
      };
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    handlepageChange(val) {
      this.unitQuery.page = val.page;
      this.getList();
    },
    handleErppageChange(val) {
      this.erpQuery.page = val;
      this.getErpList();
    },
    checkSelectable(row) {
      return !row.checked;
    },
    search() {
      this.unitQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.unitQuery.thirdSys = this.thirdSysFk;
      getCorps(this.unitQuery)
        .then((response) => {
          this.loading = false;

          if (response.code == 20000) {
            this.unitlList = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
            this.unitlList = [];
            this.total = 0;
          }

        })
        .catch(() => {
          this.loading = false;
          this.unitlList = [];
          this.total = 0;
        });
    },

    intentBack() {
      this.$router.go(-1);
    },
    getBasicThirdSys() {
      let query = {
        enabled: true,
        mainSys: true,
      };
      getBasicThirdSys(query)
        .then((response) => {
          this.thirdSys = response.data.list || [];
          this.thirdSysFk = this.thirdSys[0].thirdId;
          this.getList();
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },
    combine() {
      let selectData = this.multipleUdiSelection;
      var ids = [];
      selectData.forEach((obj) => {
        ids.push(obj);
      });
      if (ids.length == 0) {
        this.$message.warning("请选择往来单位信息！");
        return;
      }
      this.combineQuery.thirdSys = this.thirdSysFk;
      this.combineQuery.custmandocResponses = ids;
      combine(this.combineQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.$emit("closeMain", true);
          } else {
            this.$emit("closeMain", false);
          }
        })
        .catch(() => {
          this.$emit("closeMain", false);
        });
    },
    combineAll() {

      this.$confirm("此操作将在后台自动选入所有查询结果, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.unitQuery.thirdSys = this.thirdSysFk;
          combineAll(this.unitQuery)
            .then((response) => {
              if (response.code == 20000) {
                this.$emit("closeMain", true);
              } else {
                this.$emit("closeMain", false);
              }
            })
            .catch(() => {
              this.$emit("closeMain", false);
            });
        })
        .catch(() => {
        });


    },
    handleCurrentChange(val) {
      this.currentRow = val;
      this.unitQuery.key = this.currentRow.name;
    },

    handleSelectionUdiChange(val) {
      this.multipleUdiSelection = val;
    },
  },
  created() {
    this.getBasicThirdSys();
  },
};
</script>
