<template>
  <div>
    <div style="display: flex; margin-top: 12px">
      <el-checkbox v-model="checked" class="checkitemTag"
        >是否关联第三方系统往来单位信息</el-checkbox
      >
      <el-button type="primary" size="mini" icon="search" @click="combine"
        >选入</el-button
      >
      <el-button
        type="primary"
        size="mini"
        icon="search"
        @click="intentBack"
        style="margin-right: 32px"
        >返回</el-button
      >
    </div>

    <el-card class="el-card" v-if="checked">
      <div>
        <el-form
          :inline="true"
          :model="erpQuery"
          style="display: flex"
          size="mini"
        >
          <el-form-item class="query-form-item">
            <el-input
              v-model="erpQuery.key"
              placeholder="搜索"
              style="width: 350px"
            ></el-input>
          </el-form-item>

          <el-form-item style="display: flex">
            <el-button type="primary" icon="search" @click="getErpList"
              >查询</el-button
            >
          </el-form-item>
        </el-form>

        <el-table
          :data="erpList"
          style="width: 100%"
          highlight-current-row="true"
          @current-change="handleCurrentChange"
        >
          <el-table-column
            label="第三方往来单位ID"
            prop="unitId"
          ></el-table-column>
          <el-table-column label="往来单位名称" prop="name"></el-table-column>
          <el-table-column label="类型" prop="type"></el-table-column>
          <el-table-column label="地址" prop="addr"></el-table-column>
        </el-table>
      </div>
    </el-card>

    <el-card class="el-card">
      <div>
        <el-form
          :inline="true"
          :model="unitQuery"
          style="display: flex"
          size="mini"
        >
          <el-form-item class="query-form-item">
            <el-input v-model="unitQuery.key" placeholder="搜索"></el-input>
          </el-form-item>

          <el-form-item style="display: flex">
            <el-button type="primary" icon="search" @click="getList"
              >查询</el-button
            >
          </el-form-item>
        </el-form>

        <el-table
          :data="unitlList"
          style="width: 100%"
          @selection-change="handleSelectionUdiChange"
        >
          <el-table-column type="selection" width="55"></el-table-column>

          <el-table-column
            label="往来单位ID"
            prop="unitId"
            fixed
          ></el-table-column>
          <el-table-column
            label="往来单位名称"
            prop="name"
            fixed
          ></el-table-column>
          <el-table-column
            label="往来单位简写"
            prop="spell"
            fixed
          ></el-table-column>
          <el-table-column label="地址" prop="addr" fixed></el-table-column>
          <el-table-column
            label="联系方式"
            prop="status"
            fixed
          ></el-table-column>
          <el-table-column label="类型" prop="type" fixed></el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-pagination
      :page-size="unitQuery.limit"
      @current-change="handlepageChange"
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

 <script>
import { unitListBykey } from "../../api/warehouse/unit";
import { getErpUnit } from "../../api/erp/udiRelevance";
import { combine } from "../../api/receipts/unitMaintain";
export default {
  data() {
    return {
      erpQuery: {
        key: "",
        page: 1,
        limit: 10,
      },
      unitQuery: {
        page: 1,
        limit: 20,
        key: "",
      },
      combineQuery: {
        thirdId: "",
        keys: [],
      },
      checked: false,
      unitlList: [],
      erpList: [],
      currentRow: null,
      multipleUdiSelection: [],
    };
  },
  methods: {
    handlepageChange(val) {
      this.unitQuery.page = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      unitListBykey(this.unitQuery)
        .then((response) => {
          this.loading = false;
          this.unitlList = response.data.list || [];
          this.total = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.unitlList = [];
          this.total = 0;
        });
    },

    getErpList() {
      this.loading = true;
      getErpUnit(this.erpQuery)
        .then((response) => {
          this.loading = false;
          this.erpList = response.data || [];
        })
        .catch(() => {
          this.loading = false;
          this.erpList = [];
        });
    },
    intentBack() {
      this.$router.go(-1);
    },
    combine() {
      let selectData = this.multipleUdiSelection;
      var ids = [];
      selectData.forEach((obj, index) => {
        ids.push(obj.id);
      });
      if (this.checked) {
        this.combineQuery.thirdId = this.currentRow.id;
      } else {
        this.combineQuery.thirdId = "";
      }
      this.combineQuery.keys = ids;

      combine(this.combineQuery)
        .then((response) => {
          if (response.code == 20000) this.$router.go(-1);
          else {
            this.$message.error("合并失败！");
          }
        })
        .catch(() => {});
    },

    handleCurrentChange(val) {
      console.log(val);
      this.currentRow = val;
    },

    handleSelectionUdiChange(val) {
      console.log("----" + val[0].id);
      this.multipleUdiSelection = val;
    },
  },
};
</script>