<template>
    <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.unitId"
                            placeholder="往来单位ID"
                            clearable
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="erpQuery.name"
                            placeholder="往来单位"
                            clearable
                        ></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="序号" 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>

                <el-pagination
                    :page-size="erpQuery.limit"
                    @current-change="handleErppageChange"
                    layout="prev, pager, next"
                    :total="erpTotal"
                    :current-page="erpQuery.page"
                ></el-pagination>
            </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.unitId"-->
                    <!--                            placeholder="往来单位编码"-->
                    <!--                            clearable-->
                    <!--                        ></el-input>-->
                    <!--                    </el-form-item>-->
                    <el-form-item class="query-form-item">
                        <el-input
                            v-model="unitQuery.name"
                            placeholder="往来单位"
                            clearable
                        ></el-input>
                    </el-form-item>
                    <el-form-item class="query-form-item">
                        <el-select v-model="thirdSysFk" 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-form-item style="display: flex">
                        <el-button-group>
                            <el-button type="primary" icon="search" @click="getList">查询</el-button>
                            <el-button type="primary" size="mini" icon="search" @click="combine"
                            >选入
                            </el-button>
                            <el-button type="primary" size="mini" icon="search" @click="combineAll"
                            >结果全部选入
                            </el-button>
                        </el-button-group>
                    </el-form-item>
                </el-form>

                <el-table
                    :data="unitlList"
                    style="width: 100%"
                    @selection-change="handleSelectionUdiChange"
                >
                    <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="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"
                        show-overflow-tooltip
                    ></el-table-column>
                </el-table>
            </div>
        </el-card>
        <el-pagination
            :page-size="unitQuery.limit"
            @current-change="handlepageChange"
            layout="prev, pager, next"
            :total="total"
            :current-page="unitQuery.page"
        ></el-pagination>
    </div>
</template>

<script>
import {combine, ucloudUnit, combineAll} from "../../api/basic/basicUnitMaintain";
import {getBasicThirdSys} from "../../api/basic/basicThirdSys";
import {addAllErp} from "@/api/basic/udiRelevance";

export default {
    data() {
        return {
            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: {
        handlepageChange(val) {
            this.unitQuery.page = val;
            this.getList();
        },
        handleErppageChange(val) {
            this.erpQuery.page = val;
            this.getErpList();
        },
        checkSelectable(row) {
            return !row.checked;
        },
        getList() {
            this.loading = true;
            this.unitQuery.thirdSys = this.thirdSysFk;
            ucloudUnit(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;
                })
                .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>
