<template>
    <lj-win-common title="工段-选择">
        <div slot="body">
            <div style="margin-top:10px">
                <div style="padding: 6px;">
                    <mu-checkbox v-model="ifshowall" label="显示全部"></mu-checkbox>
                </div>
                <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";

    export default {
        components: {
            ljWinCommon,
            LjGrid,
        },
        data() {
            return {
                columns: [],
                columnProperties: [
                    {
                        property: "checked",
                        width: screen.width * 0.1,
                        type: "selection",
                        editable: true,
                        headerType: "selection"
                    },
                    {property: "wrkgrpcode", label: "工段编号", width: screen.width * 0.2,},
                    {property: "wrkgrpname", label: "工段名称", width: screen.width * 0.7,},
                ],
                wrkgrpSelect: [],
                item: {},
                ifshowall: false,
            }
        },
        created() {
            var pageParm = $lj.getPageParam();
            this.item = pageParm.item;
            this.GetL1WorkgroupSelection(this.item.wrkgrps)
        },
        methods: {
            GetL1WorkgroupSelection(wrkgrps) {
                let self = this;
                this.columns = [];
                $lj.postLJRequest("GetSCWorkgroups", {
                    token: $lj.getStorage("token"),
                    wrkgrps: wrkgrps,
                }, function (ret) {
                    self.columns = ret.wrkgrpList;
                })
            },
            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.wrkgrpSelect = self.columns[i];
                        }else{
                            self.$set(self.columns[i], column.property, false);
                        }
                    }
                }
            },
            chooseCommit() {
                if (this.wrkgrpSelect.length <= 0) {
                    $lj.toast('请选择工段');
                    return;
                }
                $lj.sendEvent("wrkgrp_select", this.wrkgrpSelect);
                $lj.closeWin();
            }
        },
        watch: {
            ifshowall: function(newVal, oldVal) {
                if (newVal !== oldVal) {
                    if (newVal) {
                        let wrkgrps = []
                        this.GetL1WorkgroupSelection(wrkgrps);
                    }
                    else {
                        this.GetL1WorkgroupSelection(this.item.wrkgrps);
                    }
                }
            }
        }
    }
</script>

<style>

</style>