import {
    observable,
    action
} from 'mobx';
import * as mobx from 'mobx';
import * as Api from '../apis/staff'; // 引入API接口文件 
import {
    WeaForm
} from 'comsMobx';
import {
    WeaTableNew
} from 'comsMobx';
import {
    Modal,
    message,
} from 'antd'
import {
    i18n
} from '../public/i18n';

const toJS = mobx.toJS;
const {
    TableStore
} = WeaTableNew;

export class StaffStore {
    @observable tableStore = new TableStore();
    @observable topMenu = []
    @observable rightMenu = [];
    @observable condition = [];
    fcondition = [];
    @observable leftCondition = [];
    @observable searchCondition = [];
    @observable isEdit = true;
    @observable isNew = true;
    @observable isPanelShow = false; //高级搜索面板
    @observable form2 = new WeaForm();
    @observable form = new WeaForm();
    @observable form1 = new WeaForm();
    @observable staffName = '';
    @observable conditionNum = 10;
    @observable ids = ''; //选择行id
    @observable searchConditionLoading = true;
    @observable nEdialogTitle = '';
    @observable visible = false;
    @observable dialogLoading = true;
    @observable staffId = '';
    @observable date = '';
    @observable hasRight = '';
    @observable operateType = ''; //1 编辑 2 变更
    @observable exSpinning = false;
    @observable companysId = 1;

    @observable planId = '';
    @observable selectTreeNodeInfo;


    @action getTableInfo(isOnChange = false) {
        let params = {
            planId: this.planId,
            ...this.selectTreeNodeInfo,
        }
        const {current} = this.tableStore;
        if (this.isEmptyObject(this.form2.getFormParams())) {
            params = {
                ...params,
                staffName: this.staffName,
            };
        } else {
            params = {
                ...this.form2.getFormParams(),
                ...params,
            };
        }
        Api.getSearchList(params).then(res => {
            if (res.code === 200) {
                this.setHasRight(res.data.hasRight);
                if(res.data.hasRight) {
                    isOnChange ? this.tableStore.getDatas(res.data.datas, current) : this.tableStore.getDatas(res.data.datas,1);
                }
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })

    }

    @action("导出") async  exportData() {
        if(this.planId.length == 0) {
            return message.warning(i18n.message.checkStaff())
        }
        this.exSpinning = true;
        await Api.exportData(this.planId);
        this.exSpinning = false;
      }

    //删除
    delete() {
        let params = {
            ids: this.ids
        };
        Api.deleteTableData(params).then(response => {
            return response.json()
        }).then(data => {
            if (data.code === 200) {
                message.success(i18n.message.deleteSuccess());
                this.getTableInfo();
            } else {
                message.warning(data.msg);
            }
        })
            .catch(error => {
                message.warning(error.msg);
            })
    }

    save() {
        let params = {
            ...this.form.getFormParams()
        };
        this.form.validateForm().then(f => {
            if (f.isValid) {
                Api.add(params).then(response => {
                    return response.json()
                }).then(data => {
                    if (data.code === 200) {
                        message.success(data.msg);
                        this.getTableInfo();
                        this.setVisible(false);
                    } else {
                        message.warning(data.msg);
                    }
                }).catch(error => {
                    message.warning(error.msg);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }

    edit() {
        let params = { ...this.form.getFormParams(), id: this.staffId };
        this.form.validateForm().then(f => {
            if (f.isValid) {
                Api.edit(params).then(response => {
                    return response.json()
                }).then(data => {
                    if (data.code === 200) {
                        message.success(data.msg);
                        this.getTableInfo(true);
                        this.setVisible(false);
                    } else {
                        message.warning(data.msg);
                    }
                }).catch(error => {
                    message.warning(error.msg);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }

    @action("leftTree事件") doSearch(params) {
        this.selectTreeNodeInfo = params;
        this.getTableInfo();
    }

    getForm() {
        let params = this.isNew ? {} : {
            id: this.staffId,
            operateType: this.operateType
        }
        this.setDialogLoadingStatus(true);

        Api.getForm(params).then(res => {
            if (res.code === 200) {
                this.setDialogLoadingStatus(false);
                res.data.condition && this.setFcondition(res.data.condition);
                res.data.condition && this.setCondition(res.data.condition);
                res.data.condition && this.form.initFormFields(res.data.condition);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }

    updateConditions(data) {
        if(data.planId.value == '') return;
        this.form.updateFields({
            ecCompany: {
                value: ''
            },
            ecDepartment: {
              value: ''
            },
            jobId: {
                value: ''
            }
          });
        let title = data.planId.valueObj[0].title;
        if(title != undefined) {
            title = title.replace(/ /g, "").split("|")[3];
        }
        const type = data.planId.valueObj[0].control_dimensions || title;
        this.condition = this.fcondition;
        switch(type) {
            case  '分部':
                this.condition[0].items = this.condition[0].items.filter(item => {
                    if(item.domkey[0] == 'ecCompany'){
                        item.viewAttr = 3;
                        item.rules = "required";
                    }
                    return (item.domkey[0] != 'ecDepartment' && item.domkey[0] != 'jobId')
                });
                break;
            case '部门':
                this.condition[0].items = this.condition[0].items.filter(item => {
                    if(item.domkey[0] == 'ecDepartment'){
                        item.viewAttr = 3;
                        item.rules = "required";
                    }
                    return  item.domkey[0] != 'ecCompany' && item.domkey[0] != 'jobId'
                })
                break;
            case '岗位':
                this.condition[0].items = this.condition[0].items.filter(item => {
                    if(item.domkey[0] == 'jobId'){
                        item.viewAttr = 3;
                        item.rules = "required";
                    }
                    return  item.domkey[0] != 'ecCompany' && item.domkey[0] != 'ecDepartment'
                })
            
        }
        this.setCondition(this.condition);
        this.form.initFormFields(this.condition);
    }

    getSearchCondition() {
        this.setScLoadingStatus(false);
        Api.getAdvanceSearchCondition().then(res => {
            if (res.code === 200) {
                this.setScLoadingStatus(false);
                res.data.conditions && this.setSearchCondition(res.data.conditions);
                res.data.conditions && this.form2.initFormFields(res.data.conditions);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }


    @action getHasRight() {
        Api.getHasRight().then(res => {
            if (res.code === 200) {
                res.data.rightMenu && this.setRightMenu(res.data.rightMenu);
                res.data.topMenu && this.setTopMenu(res.data.topMenu);
                res.data.condition && this.setLeftCondition(res.data.condition);
                res.data.condition && this.form1.initFormFields(res.data.condition);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }

    updateFields(val) {
        this.form2.updateFields({
            staffName: {
                value: val
            }
        });
    }

    setSearchCondition(condition) {
        this.searchCondition = condition;
    }

    setScLoadingStatus(bool) {
        this.searchConditionLoading = bool;
    }

    setPanelStatus(bool) {
        this.isPanelShow = bool;
        bool && this.getSearchCondition();
        if (!bool) {
            this.scLoadingReset();
        }
    }

    setStaffName(staffName) {
        this.staffName = staffName;
    }

    isEmptyObject(obj) {
        for (let key in obj) {
            return false;
        }
        return true;
    }

    setIds(ids) {
        this.ids = ids;
    }

    scLoadingReset() {
        this.searchConditionLoading = true;
    }


    formReset() {
        this.form = new WeaForm();
    }

    dialogLoadingReset() {
        this.dialogLoading = true;
    }

    setVisible(bool) {
        this.visible = bool;
        this.formReset();
        !bool && this.dialogLoadingReset();
    }

    setDialogLoadingStatus(bool) {
        this.dialogLoading = bool;
    }

    setNeDialogTitle(title) {
        this.nEdialogTitle = title;
    }

    setIsNew(bool) {
        this.isNew = bool;
    }

    setCondition(condition) {
        this.condition = condition;
    }

    setStaffId(staffId) {
        this.staffId = staffId;
    }

    @action
    setDate(date) {
        this.date = date;
    }

    setTopMenu(topMenu) {
        this.topMenu = topMenu;
    }

    setRightMenu(rightMenu) {
        this.rightMenu = rightMenu;
    }

    setHasRight(bool) {
        this.hasRight = bool;
    }

    setOperateType(operateType) {
        this.operateType = operateType;
    }

    setLeftCondition(leftCondition) {
        this.leftCondition = leftCondition;
    }

    setPlanId(planId) {
        this.planId = planId;
        this.getTableInfo();
    }

    setFcondition(condition) {
        this.fcondition = condition;
    }


}