<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.customname"
                            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="序号" 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="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-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"
                                       :loading="combineLoading"
                            >选入
                            </el-button>
                        </el-button-group>
                    </el-form-item>
                </el-form>

                <el-table
                    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"
                        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 {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 {
            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();
        },
        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,
            };
            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) {
            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) {
            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>
