<template>
    <div>
        <el-form :inline="true" :model="unitquery" class="query-form" size="mini">
            <el-form-item class="query-form-item">
                <el-input v-model="unitquery.key" placeholder="搜索"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="search"
                    @click.native.stop="getUnitList()"
                >查询
                </el-button
                >
<!--                <el-button-->
                <!--                    type="primary"-->
                <!--                    icon="search"-->
                <!--                    @click.native.stop="selectUnit()"-->
                <!--                >确定-->
                <!--                </el-button-->
                <!--                >-->
            </el-form-item>
        </el-form>

        <el-table
            v-loading="loading"
            :data="unitlist"
            style="width: 100%"
            highlight-current-row
            @current-change="handleDetail"
        >
            <el-table-column
                label="往来单位ID"
                prop="erpId"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="往来单位名称"
                prop="name"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="往来单位简写"
                prop="spell"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column
                label="地址"
                prop="addr"
                show-overflow-tooltip
            ></el-table-column>
            <el-table-column label="状态" prop="status"></el-table-column>
            <el-table-column label="类型" prop="type"></el-table-column>
            <el-table-column width="200" label="操作" fixed="right">
                <template slot-scope="scope">

                    <el-button
                        type="text"
                        size="small"
                        @click.native.stop="selectUnit(scope.row)"
                    >选入绑定
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import {updateUnit} from "@/api/warehouse/order";
import {getBasicUnitMaintains, getCorpFilter} from "@/api/basic/basicUnitMaintain";

export default {
    name: "DialogSelectUnit",
    props: {
        codeId: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            unitquery: {key: "", page: 1, limit: 20},
            unitlist: [],
            unitUpdateQuery: {
                id: "",
                fromCorpId: "",
                fromCorp: "",
            },
        }
    },
    methods: {
        getUnitList() {
            if(!this.unitquery.key){
                this.getUnitListByCode(this.codeId)
            }else{
            this.loading = true;
            getBasicUnitMaintains(this.unitquery)
                .then((response) => {
                    this.loading = false;
                    this.unitlist = response.data.page.list || [];
                })
                .catch(() => {
                    this.loading = false;
                });
            }
        },
        getUnitListByCode(row) {
            let query = {
                udiRlIdFk: row.relId,
            }
            getCorpFilter(query)
                .then((response) => {
                    this.loading = false;
                    if (response.code == 20000) {
                        this.unitlist = response.data || [];
                    } else {
                        this.$message.warning(response.message);
                    }

                })
                .catch(() => {
                    this.loading = false;
                });
        },

        handleDetail(row) {
            this.curUnitRow = row;
        },
        selectUnit(row) {
            this.$emit("selectSupUnit", row);
            this.$emit("closeBindDialog", true);
        },
    },
    created() {

    },
    mounted() {
        console.log("this.codeId = " + this.codeId)
        if (this.codeId != null) {
            this.getUnitListByCode(this.codeId)
        }
    }

}
</script>

<style scoped>

</style>
