/**
 * Created by Zhong on 2017/8/25.
 */
/*
   弹出组成物窗口 组成物表
 * */
let componentOprObj = {
    treeObj:null,
    rootNode: null,//分类树根节点
    radiosSelected: null,//allGljs, stdGljs, complementaryGljs
    workBook: null,
    selectedList: [],//选中的组成物
    setting: {
        owner: "components",
        header: [
            {headerName:"选择", headerWidth: 40, dataCode: "select", hAlign: "center", vAlign: "center"},
            {headerName:"编码",headerWidth:80,dataCode:"code", dataType: "String", formatter: "@", hAlign: "left", vAlign: "center"},
            {headerName:"名称",headerWidth:120,dataCode:"name", dataType: "String", hAlign: "left", vAlign: "center"},
            {headerName:"规格型号",headerWidth:80,dataCode:"specs", dataType: "String", hAlign: "center", vAlign: "center"},
            {headerName:"计量单位",headerWidth:80,dataCode:"unit", dataType: "String", hAlign: "center", vAlign: "center"},
            {headerName:"单价",headerWidth:80,dataCode:"basePrice", dataType: "Number", formatter: "0.00", hAlign: "right", vAlign: "center"},
            {headerName:"类型",headerWidth:80,dataCode:"gljType", dataType: "String",  hAlign: "center", vAlign: "center"}
        ],
        view: {
            lockedCells: [1, 2, 3]
        }
    },
    buildSheet: function (container) {
        let me = componentOprObj;
        me.workBook = sheetOpr.buildSheet(container, me.setting, 30);
        me.workBook.getSheet(0).setColumnWidth(0, 20, GC.Spread.Sheets.SheetArea.rowHeader);
        me.workBook.getSheet(0).setFormatter(-1, 1, "@", GC.Spread.Sheets.SheetArea.viewport);
        me.workBook.getSheet(0).options.isProtected = true;
        me.workBook.bind(GC.Spread.Sheets.Events.ButtonClicked, me.onButtonClicked);//复选框点击事件
        //sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
        me.componentsBtnOpr($('#componentsConf'));
        me.radiosChange();
        //me.searchChange();
    },
    onButtonClicked: function (sender, args) {
        let me = componentOprObj, re = repositoryGljObj;
        let val = args.sheet.getValue(args.row, args.col);
        let thisComponent = me.currentCache[args.row];
        thisComponent.isChecked = val;
        console.log(val);
        if(args.sheet.isEditing()){
            args.sheet.endEdit(true);
        }
        else{
            //维护选中组成物列表
            if(val === true){
                //解决复选框编辑状态的暂时方法todo
                let isExist = false;
                for(let i = 0, len = me.selectedList.length; i < len; i++){
                    if(me.selectedList[i].ID === thisComponent.ID){
                        isExist = true;
                        break;
                    }
                }
                if(!isExist){
                    me.selectedList.push(thisComponent);
                }
            }
            else if(val === false){
                for(let i = 0, len = me.selectedList.length; i < len; i++){
                    if(me.selectedList[i].ID === thisComponent.ID){
                        me.selectedList.splice(i, 1);
                        break;
                    }
                }
                if($("input[name='glj']:checked").val() === 'selectedGljs'){//radio为已选工料机时
                    me.showGljList = [];
                    me.setShowGljList(me.selectedList, false);
                    re.sortGlj(me.showGljList);
                    //重新显示
                    me.showGljItems(me.showGljList, me.gljCurTypeId);
                    if (re.parentNodeIds["_pNodeId_" + me.gljCurTypeId]) {//更新cache
                        me.currentOprParent = 1;
                        me.currentCache = me.getParentCache(re.parentNodeIds["_pNodeId_" + me.gljCurTypeId]);
                    } else {
                        me.currentCache = me.getCache();
                    }
                }
            }
        }
    },
    setShowGljList: function (gljList, clearChecked) {
        //初始为所有工料机,机械类型可添加机械组成物，混凝土,砂浆、配合比可添加普通材料
        let materialArr = [202, 203, 204];//混凝土、砂浆、配合比, 201普通材料
        let that = repositoryGljObj, me = componentOprObj;
            for(let i = 0; i < gljList.length; i++){
                if(that.currentGlj.gljType === 301 && gljList[i].gljType === 302 ||
                    materialArr.indexOf(that.currentGlj.gljType) !== -1 && gljList[i].gljType === 201){
                    //去除与已添加的组成物重复的条目
                    let isExist = false;
                    for(let j = 0; j < that.currentComponent.length; j++){
                        if(that.currentComponent[j].ID === gljList[i].ID){
                            isExist = true;
                            break;
                        }
                    }
                    if(!isExist){
                        if(clearChecked){
                            gljList[i].isChecked = false;//切换的时候清空选择
                        }
                       /* if(isSelected){//已选择工料机显示
                            if(gljList[i].isChecked === true){
                                me.showGljList.push(gljList[i]);
                            }
                        }*/
                   //     else{
                            me.showGljList.push(gljList[i]);
                      //  }
                    }
                }
            }
    },
    //初始默认radio
    initRadio: function () {
        let that = repositoryGljObj, me = componentOprObj;
        $('#searchGlj').val('');//恢复搜索文本
        //初始化组成物列表
        me.selectedList = [];
        //默认radio所有工料机
        if(typeof $("input[name='glj']:checked")[0] !== 'undefined'){
            $("input[name='glj']:checked")[0].checked = false;
        }
        $("input[value = 'allGljs']")[0].checked = true;
        me.radiosSelected = 'allGljs';
        //初始为所有工料机,机械类型可添加机械组成物，混凝土,砂浆、配合比可添加普通材料
        me.showGljList = [];
        if(me.radiosSelected === 'allGljs'){
            me.setShowGljList(that.stdGljList, true);
            me.setShowGljList(that.complementaryGljList, true);
            that.sortGlj(me.showGljList);
        }
    },
    //监听radios选择事件
    radiosChange: function () {
        let me = componentOprObj, re = repositoryGljObj;
        let materialArr = [202, 203, 204];//混凝土、砂浆、配合比, 201普通材料
        $('.glj-radio').change(function () {
            let val = $("input[name='glj']:checked").val();
            me.radiosSelected = val;
            //选择改变，数据重新筛选显示
            //清空选中的组成物列表
            //me.selectedList = [];
            me.showGljList = [];
            if(me.radiosSelected === 'allGljs'){
                me.setShowGljList(re.stdGljList);
                me.setShowGljList(re.complementaryGljList);
            }
            else if(me.radiosSelected === 'stdGljs'){
               me.setShowGljList(re.stdGljList);
            }
            else if(me.radiosSelected === 'complementaryGljs'){
                me.setShowGljList(re.complementaryGljList);
            }
            else if(me.radiosSelected === 'selectedGljs'){
                /*me.setShowGljList(re.stdGljList, false, true);
                me.setShowGljList(re.complementaryGljList, false, true);*/
                me.setShowGljList(me.selectedList, false);
            }
            re.sortGlj(me.showGljList);
            //重新显示
            me.showGljItems(me.showGljList, me.gljCurTypeId);
            //切换radio后更新cache
            if (me.currentOprParent = 1) {
                if(re.parentNodeIds["_pNodeId_" + me.gljCurTypeId]){
                    me.currentCache = me.getParentCache(re.parentNodeIds["_pNodeId_" + me.gljCurTypeId]);
                }
                else{
                    me.currentCache = [];
                }
            } else {
                me.currentCache = me.getCache();
            }
        });
    },
    //实时模糊搜索
    searchChange: function () {
        let me = componentOprObj, re = repositoryGljObj;
        let interval = null;
        function search(){
            console.log($('#searchGlj').val());
        }
        $('#searchGlj').focus(function () {
            interval = setInterval(search, 500);
        }).blur(function () {
            clearInterval(interval);
        });
    },
  /*  //切换分类树时，记住当前分类的选择, value = true、false、null
    setComponentChecked: function (sheet) {
        let me = componentOprObj;
        for(let i = 0; i < sheet.getRowCount(); i ++){
            if(sheet.getValue(i, 0) === true){//选择了
                me.preCache[i].isChecked = true;
            }
            else if(sheet.getValue(i, 0) === false){//避免value为null
                me.preCache[i].isChecked = false;
            }
        }
    },*/
    //获得选择的组成物
    getComponents: function () {
        let rst = [];
        let me = componentOprObj;
        for(let i = 0; i < me.showGljList.length; i++){

        }
    },
    getParentCache: function (nodes) {
        let me = componentOprObj, rst = [];
        for(let i = 0; i < me.showGljList.length; i++){
            if(nodes.indexOf(me.showGljList[i].gljClass) !== -1){
                rst.push(me.showGljList[i]);
            }
        }
        rst.sort(function (a, b) {
            let rst = 0;
            if(a.code > b.code) rst = 1;
            else if(a.code < b.code)rst = -1;
            return rst;
        });
        return rst;
    },
    getCache: function() {
        let me = componentOprObj, rst = [];
        for (let i = 0; i < me.showGljList.length; i++) {
            if (me.showGljList[i].gljClass == me.gljCurTypeId) {
                rst.push(me.showGljList[i]);
            }
        }
        return rst;
    },
    showGljItems: function(data, type) {
        let me = componentOprObj, re = repositoryGljObj;
        if (me.workBook) {
            let cacheSection = [];
            let pArr = re.parentNodeIds["_pNodeId_" + type];
            for (let i = 0; i < data.length; i++) {
                if (pArr && pArr.indexOf(data[i].gljClass) >= 0) {
                    cacheSection.push(data[i]);
                } else if (type == data[i].gljClass) {
                    cacheSection.push(data[i]);
                }
            }
            sheetOpr.cleanSheet(me.workBook.getSheet(0), me.setting, -1);
            sheetOpr.showData(me.workBook.getSheet(0), me.setting, cacheSection, re.distTypeTree);
            me.workBook.getSheet(0).options.isProtected = true;
            me.workBook.getSheet(0).setRowCount(cacheSection);
            cacheSection = null;
        }
    },
    //组成物窗口按钮操作
    componentsBtnOpr: function (conf) {//确定、取消、关闭按钮
        let me = componentOprObj, that = gljComponentOprObj, re = repositoryGljObj;
        conf.click(function () {
           //添加选择添加的组成物
            let updateArr = [];
            if(me.selectedList.length > 0){
                for(let i = 0, len = me.selectedList.length; i < len; i++){
                    re.currentGlj.component.push({ID: me.selectedList[i].ID, consumeAmt: 0});
                }
                updateArr.push(re.currentGlj);
                that.updateComponent(updateArr);
            }

        });
    }
};

let componentTypeTreeOprObj = {
    onClick: function(event,treeId,treeNode) {
        let me = componentOprObj, re = repositoryGljObj, that = gljComponentOprObj, gljTypeId = treeNode.ID;
        if(me.gljCurTypeId !== treeNode.ID){
            me.gljCurTypeId = treeNode.ID;
            if (re.parentNodeIds["_pNodeId_" + treeNode.ID]) {
                me.currentOprParent = 1;
                me.currentCache = me.getParentCache(re.parentNodeIds["_pNodeId_" + treeNode.ID]);
            } else {
                me.currentCache = me.getCache();
            }
        }
        me.showGljItems(me.showGljList, gljTypeId);
    }
}