<template>
    <div>
        <lj-win-index-common :title="title" api-name="GetL1EmpInfoList"
                             :data-reset-callback="handleDataReset"
                             :data-loaded-callback="handleDataLoaded"
                             ref="baseWin"
                             :addable="false"
                             @add-click="addemp"
                             :query-hard-parms="queryHardParms"
                             :query-fields="queryFields"
                             :binding-parms="bindingParms"
                             @query-field-click="handleQueryFieldClick">
            <div style="text-align: center;" slot="header" @click="handleTitle">
                {{title}}<i class="iconfont icon-down"></i>
            </div>
            <lj-searchbar slot="header_sub" v-model="bindingParms.keyword" hint="输入员工名称/编号进行查找" @search="handleSearchbar" style="width: 100%"/>
            <div slot="body_list">
                <mu-list class="lj-group">
                    <template v-for="item in allData">
                        <emp-item :item="item" @click.native="handleCellClick(item)"></emp-item>
                    </template>
                </mu-list>
                <div v-show="isSelectModeMultiple" class="flowButton" style="background-color: #e0e0e0;position: fixed;bottom: 75px;right: 10px;border-radius: 50%;">
                    <mu-button color="#888" icon @click="handleClickMultiple">
                        <i class="iconfont icon-check"></i>
                    </mu-button>
                </div>
            </div>
        </lj-win-index-common>
        <l1-department-choose ref="deptDialog" @dept-chosen="handleDeptChosen"/>
    </div>
</template>

<script>
    import LjWinIndexCommon from "components/lj-win-index-common";
    import LjSearchbar from "components/lj-searchbar";
    import empItem from 'components/L1APP/public/emp-item';
    import l1DepartmentChoose from "components/L1APP/public/l1-department-choose";

    export default {
        components: {
            LjSearchbar,
            LjWinIndexCommon,
            empItem,
            l1DepartmentChoose
        },
        data() {
            return {
                winMode: "",
                allData: [],
                title:"员工选择",
                queryHardParms: {},
                bindingParms: {keyword: ""},
                queryFields: [
                    {
                        title: "部门",
                        property: "departmentid",
                        selectOptions: {
                            optionsApi: "GetL1Department",
                            valueMap: ["departmentid", "departmentname"],
                            extraOptions: [{departmentid: -1, departmentname: "全部", areaname: "全部", sonflag: 0, parentid: 0}]
                        },
                    },
                    {title: "员工名称/编号", property: "keyword"}
                ]
            }
        },
        created() {
            let pageParam = $lj.getPageParam();
            if (pageParam) {
                this.winMode = pageParam.winMode;
                this.queryHardParms = pageParam.queryParams || {};
            }
        },
        methods: {
            handleDataReset(response) {
                this.allData.length = 0;
                this.addList(response.empList);
            },
            handleDataLoaded(response) {
                let moreItems = response.empList;
                if (moreItems.length > 0) {
                    this.addList(moreItems);
                    return true;
                }
                return false;
            },
            addList(list) {
                this.allData = this.allData.concat(list);
            },
            handleSearchbar(text) {
                this.$refs.baseWin.refresh(Object.assign(this.bindingParms, this.queryHardParms));
            },
            handleCellClick(mainObject) {
                if(this.isSelectModeMultiple){
                    this.$set(mainObject,"isCheck",!mainObject.isCheck)
                }else if (this.isSelectMode) {
                    $lj.sendEvent("empap_select", mainObject);
                    $lj.closeWin();
                } else {
                    // $lj.openWin("cust-detail", {mainObject: mainObject});
                }
            },
            handleClickMultiple(){
                var item = [];
                for(var i = 0 ;i<this.allData.length ;i++){
                    if(this.allData[i].isCheck){
                      item.push(this.allData[i])
                    }
                }
                $lj.sendEvent("empap_select", item);
                $lj.closeWin();
            },
            addemp() {
                // $lj.openWin("cust-detail");
            },
            handleQueryFieldClick({property}) {
                if (property === 'departmentid') {
                    this.$refs.deptDialog.open();
                }
            },
            handleTitle(){
                this.$refs.deptDialog.open();
            },
            handleDeptChosen(item) {
                if (item && item.departmentid) {
                    this.$refs.baseWin.setQueryParam("departmentid", item.departmentid);
                    this.$refs.baseWin.refresh({departmentid: item.departmentid});
                    this.$refs.baseWin.closeRightDrawer();
                    this.title = '员工选择-'+item.departmentname
                }
            },
        },
        computed: {
            isSelectMode() {
                return this.winMode === "single-select" || this.winMode === "multiple-select";
            },
            isSelectModeMultiple(){
                return this.winMode === "multiple-select";
            }
            // title() {
            //     if (this.winMode === 'single-select') {
            //         return "员工选择";
            //     } else {
            //         return "员工管理";
            //     }
            // }
        }
    }
</script>