<template>
    <lj-win-common title="工作中心-选择">
        <div slot="header_sub" style="display: flex;">
            <mu-checkbox style="padding: 7px 0 5px 4px;white-space:nowrap;" v-model="ifshowall" label="显示全部"></mu-checkbox>
            <lj-searchbar style="width: auto;" hint="输入名称进行查找" @search="handleSearchbar" v-model="keyword" />
        </div>
        <div slot="body">
            <div style="margin-top:10px">
                <lj-grid :table-data="columns" :columns="columnProperties" :editing="true"
                         @cell-click="handleGridCellClick"/>
            </div>
        </div>
        <template slot="footer">
            <mu-button large color="primary" @click="chooseCommit" style="width:100%">选择</mu-button>
        </template>
    </lj-win-common>
</template>

<script>
    import ljWinCommon from 'components/lj-win-common'
    import LjGrid from "components/lj-grid";
    import LjSearchbar from "components/lj-searchbar";

    export default {
        components: {
            ljWinCommon,
            LjGrid,
            LjSearchbar,
        },
        data() {
            return {
                columnsAll: [],
                columns: [],
                columnProperties: [
                    {
                        property: "checked",
                        width: screen.width * 0.1,
                        type: "selection",
                        editable: true,
                        headerType: "selection"
                    },
                    {property: "workgroupcode", label: "编号", width: screen.width * 0.2,},
                    {property: "workgroupname", label: "名称", width: screen.width * 0.3,},
                    {property: "wrkgrpname", label: "所属工段", width: screen.width * 0.4,},
                ],
                workgroupSelect: [],
                keyword: '',
                item: {},
                ifshowall: false,
            }
        },
        created() {
            var pageParm = $lj.getPageParam();
            this.item = pageParm.item;
            this.GetL1WorkgroupSelection(this.item.wrkgrpid)
        },
        methods: {
            GetL1WorkgroupSelection(wrkgrpid) {
                let self = this;
                this.columns = [];
                $lj.postLJRequest("GetWorkgroupList", {
                    token: $lj.getStorage("token"),
                    wrkgrpid: wrkgrpid,
                    inuse: 1,
                }, function (ret) {
                    self.columns = ret.workgroupList;
                    self.columnsAll = ret.workgroupList;
                })
            },
            handleGridCellClick({column, row, index}) {
                if (!column) {
                    return;
                }
                let self = this;
                if (column.property === "checked") {
                    for (let i = 0; i < self.columns.length; i++) {
                        if (i === index) {
                            self.$set(self.columns[i], column.property, true);
                            self.workgroupSelect = self.columns[i];
                        }else{
                            self.$set(self.columns[i], column.property, false);
                        }
                    }
                }
            },
            handleSearchbar() {
                this.columns = [];
                if (this.keyword !== '') {
                    this.columnsAll.forEach((item) => {
                        if (item.workgroupname.indexOf(this.keyword) >= 0) {
                            this.columns.push(item);
                        }
                    })
                }
                else {
                    this.columns = this.columnsAll;
                }
            },
            chooseCommit() {
                if (this.workgroupSelect.length <= 0) {
                    $lj.toast('请选择工作中心');
                    return;
                }
                $lj.sendEvent("workgroup_select", this.workgroupSelect);
                $lj.closeWin();
            }
        },
        watch: {
            ifshowall: function(newVal, oldVal) {
                if (newVal !== oldVal) {
                    if (newVal) {
                        this.GetL1WorkgroupSelection();
                    }
                    else {
                        this.GetL1WorkgroupSelection(this.item.wrkgrpid);
                    }
                }
            }
        }
    }
</script>

<style>

</style>