<template>
    <lj-win-common :title="title">
        <div slot="header_sub" style="display: flex;">
            <lj-searchbar style="width: 100%;" 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 {
                title:'',
                columns: [],
                allcolumns:[],
                seachCode:'',
                columnProperties: [
                    {
                        property: "checked",
                        width: screen.width * 0.1,
                        type: "selection",
                        editable: true,
                        headerType: "selection"
                    },
                ],
                groupSelect: [],
                keyword: '',
                item: {},
                ifshowall: false,
            }
        },
        created() {
            var pageParm = $lj.getPageParam();
            this.title = pageParm.title;
            this.columnProperties = this.columnProperties.concat(pageParm.gridColumns);
            this.columns = pageParm.columns;
            this.allcolumns = pageParm.columns;
            this.seachCode = pageParm.seachCode;
        },
        methods: {           
            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.groupSelect = self.columns[i];
                        }else{
                            self.$set(self.columns[i], column.property, false);
                        }
                    }
                }
            },
            handleSearchbar() {
                this.columns = [];
                if (this.keyword != '') {
                    this.allcolumns.forEach(element => {
                        if(element[this.seachCode].indexOf(this.keyword) >= 0){
                            this.columns.push(element);
                        }
                    });
                }
                else {
                    this.columns = this.allcolumns;
                }
            },
            chooseCommit() {
                if (this.groupSelect.length <= 0) {
                    $lj.toast('请选择');
                    return;
                }

                $lj.sendEvent("currcncy_select", this.groupSelect);
                $lj.closeWin();
            }
        }
    }
</script>

<style>

</style>