<template>
    <div>
        <el-card class="el-card" v-if="checked">
                <el-form :inline="true" :model="erpQuery"  size="mini" label-width="100px" v-show="showSearch">
                  <el-row>
                    <el-col :span="6">
                     <el-form-item label="单位编码:">
                        <el-input v-model="erpQuery.customname" placeholder="搜索" style="width: 90%"></el-input>
                     </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-search" @click="getErpList">查询</el-button>
            </el-button-group>
          </div>
          <el-divider style="margin: 15px"></el-divider>

                <el-table :data="erpList" style="width: 100%" border highlight-current-row @current-change="handleCurrentChange">
                    <el-table-column label="序号" type="index"></el-table-column>
                    <el-table-column label="第三方往来单位ID" prop="customid" width="200"></el-table-column>
                    <el-table-column label="往来单位名称" prop="customname" width="400"></el-table-column>
                </el-table>

          <pagination
            v-show="erpTotal>0"
            :total="erpTotal"
            :page.sync="erpQuery.page"
            :limit.sync="erpQuery.limit"
            @pagination="handleErppageChange"
          ></pagination>
        </el-card>

        <el-card class="el-card">
                <el-form :model="unitQuery" label-width="100px" v-show="showSearch" size="mini">
                  <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-input v-model="unitQuery.name" 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="true">
                            <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="searchList">查询</el-button>
              <el-button type="primary" size="mini" icon="el-icon-bottom-right" @click="combine" :loading="combineLoading">选入</el-button>
            </el-button-group>
          </div>

          <el-divider style="margin: 15px"></el-divider>
                <el-table border highlight-current-row @current-change="handleCurrentChange" :data="unitlList" :row-class-name="tableRowClassName" style="width: 100%">
                    <el-table-column label="序号" type="index"></el-table-column>
                    <el-table-column label="往来单位编码" prop="id"></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"></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 {combineSingle, ucloudUnit, removeRl} from "@/api/basic/basicUnitMaintain";
import {getBasicThirdSys} from "@/api/basic/basicThirdSys";

export default {
    name: "combinType",
    props: {
        corpData: {
            type: Object,
            required: true,
        },
        defaultSys: {
            type: Object,
            required: true,
        }
    },
    data() {
        return {
            showSearch: true,
            unitQuery: {
                page: 1,
                limit: 10,
                key: "",
                thirdSys: null,
            },
            combineQuery: {
                thirdSys: "",
                key: null,
                erpUnitsResponse: [],
            },
            total: 0,
            erpTotal: 0,
            checked: false,
            unitlList: [],
            erpList: [],
            currentRow: null,
            multipleUdiSelection: [],
            thirdSys: [],
            thirdSysFk: null,
            combineLoading: false,
        };
    },
    methods: {
        handlepageChange(val) {
            this.unitQuery.page = val;
            this.getList();
        },
        handleErppageChange(val) {
            this.erpQuery.page = val;
            this.getErpList();
        },
        searchList() {
            this.unitQuery.page = 1;
            this.getList();
        },
        hideSearch() {
          this.showSearch = !this.showSearch;
        },
        onReset() {
            this.unitQuery = {
                page: 1,
                limit: 10,
                key: "",
                thirdSys: null,
            }
            this.total = 0;
            this.getList();
        },
        getList() {
            this.loading = true;
            this.unitQuery.thirdSys = this.thirdSysFk;
            ucloudUnit(this.unitQuery)
                .then((response) => {
                    console.log(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,
            };
            getBasicThirdSys(query)
                .then((response) => {
                    this.thirdSys = response.data.list || [];
                    this.thirdSysFk = this.thirdSys[0].thirdId;
                    if (this.defaultSys != null) {
                        this.thirdSysFk = this.defaultSys;
                    }
                })
                .catch(() => {
                    this.loading = false;
                    this.list = [];
                });
        },

        handleCurrentChange(val) {
            console.log(val);
            this.combineQuery.thirdSys = this.thirdSysFk;
            this.combineQuery.key = this.corpData.id;
            this.combineQuery.erpUnitsResponse = val;
        },
        combine() {

            if (this.combineQuery.key == null) {
                this.$message.warning("未选择往来单位！");
                return;
            }
            if (this.combineQuery.erpUnitsResponse.checked == true) {
                this.$message.warning("该往来单位已被关联，不能重复选入！");
                return;
            }

            this.combineLoading = true;
            combineSingle(this.combineQuery)
                .then((response) => {
                    this.combineLoading = false;
                    this.$emit("cancelDialog", true);
                    if (response.code == 20000) {
                        this.$message.success("关联成功！");
                    } else {
                        this.$message.error(response.message);
                    }
                })
                .catch(() => {
                    this.combineLoading = false;
                    this.$emit("cancelDialog", false);
                });
        },
        tableRowClassName({row}) {
            if (row.checked) return "warning-row";
            return "";
        },
        handleSelectionUdiChange(val) {
            console.log("----" + val[0].id);
            this.multipleUdiSelection = val;
        },

    },
    created() {
        this.getBasicThirdSys();
    },
};
</script>

<style scoped>

div /deep/ .el-table .warning-row {
    background: #bebebe;
}

div /deep/ .el-table .success-row {
    background: #ffffff;
}
</style>
