import {
    observable,
    action
} from 'mobx';
import * as mobx from 'mobx';
import * as Api from '../apis/resourceBasicInfo'; // 引入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 ResourceBasicInfoStore {
    @observable tableStore = new TableStore();
    @observable topMenu = []
    @observable rightMenu = [];
    @observable condition = [];
    @observable searchCondition = [];
    @observable isEdit = true;
    @observable isNew = false;
    @observable operateType = '';//1 资料新增 2类型新增 3资料编辑 4类型编辑
    @observable isPanelShow = false; //高级搜索面板
    @observable form2 = new WeaForm(); //高级搜索
    @observable form = new WeaForm(); //新增编辑
    @observable fname = '';
    @observable conditionNum = 2;
    @observable ids = ''; //选择行id
    @observable searchConditionLoading = true;
    @observable nEdialogTitle = '';
    @observable visible = false;
    @observable dialogLoading = true;
    @observable fId = '';
    @observable date = '';
    @observable hasRight = '';
    @observable treeLoading = true;

    @observable treeConfig = {
        data: [],
        selectedKeys: [],
        treeExpandKeys: [],
        onExpand: (keys) => {
            this.treeConfig.treeExpandKeys = keys;
        },
        onSelectedTreeNode: (key, count, countType) => {
            this.treeConfig.selectedKeys = [key];
            this.selectedTreeNodeInfo = countType.node.props.data;
            this.getTableInfo();
        }
    }
    //选中树节点的信息
    @observable selectedTreeNodeInfo;

    @action initData = () => {
        this.selectedTreeNodeInfo = null;
        this.treeConfig.treeExpandKeys.length = 0;
        this.getTree(); 
    }

    @action getTree(){
        Api.getTree().then(res => {
            if (res.code === 200) {
                if (res.data.length > 0) {
                    this.treeConfig.data = res.data;
                    this.treeConfig.selectedKeys = this.selectedTreeNodeInfo != null ? [this.selectedTreeNodeInfo.domid] : [res.data[0].key];
                    this.treeConfig.treeExpandKeys = "-1";
                    this.selectedTreeNodeInfo = this.selectedTreeNodeInfo != null ? this.selectedTreeNodeInfo : res.data[0];
                    this.getTableInfo();
                    this.setTreeLoading(false);
                }
            } else {
                message.error(res.msg);
            }
        }, error => { })
    }


    @action
    getTableInfo() {
        let params = {
            fclassid: this.selectedTreeNodeInfo && this.selectedTreeNodeInfo.domid,
        }
        this.tableStore = new TableStore();
        if (this.isEmptyObject(this.form2.getFormParams())) {
            params = {
                ...params,
                fname: this.fname,
            };
        } else {
            params = {
                ...params,
                ...this.form2.getFormParams(),
            };
        }
        Api.getSearchList(params).then(res => {
            if (res.code === 200) {
                this.setHasRight(res.data.hasRight);
                res.data.datas && this.tableStore.getDatas(res.data.datas, 1);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })

    }

    //删除
    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);
        })
    }

    //删除类型
    deleteType() {
        Api.deleteType(this.selectedTreeNodeInfo.domid).then(response => {
            return response.json()
        }).then(data => {
            if (data.code === 200) {
                message.success(i18n.message.deleteSuccess());
                this.getTree();
            } else {
                message.warning(data.msg);
            }
        }).catch(error => {
            message.warning(error.msg);
        })
    }


    //恢复
    recover(id) {
        let params = {
            ids: id
        };
        Api.recoverData(params).then(response => {
            return response.json()
        }).then(data => {
            if (data.code === 200) {
                message.success(i18n.message.opSuccess());
                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());
            }
        });
    }


    saveType() {
        let params = {
            ...this.form.getFormParams()
        };
        this.form.validateForm().then(f => {
            if (f.isValid) {
                Api.addType(params).then(response => {
                    return response.json()
                }).then(data => {
                    if (data.code === 200) {
                        message.success(data.msg);
                        this.getTree();
                        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(), fid: this.fId };
        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();
                        this.setVisible(false);
                    } else {
                        message.warning(data.msg);
                    }
                }).catch(error => {
                    message.warning(error.msg);
                })
            } else {
                f.showErrors();
                this.setDate(new Date());
            }
        });
    }

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

    getForm() {
        let params = this.isNew ? {
            fclassid: this.selectedTreeNodeInfo.domid
        } : {
            fid: this.fId
        }
        this.setDialogLoadingStatus(true);
        Api.getForm(params).then(res => {
            if (res.code === 200) {
                this.setDialogLoadingStatus(false);
                res.data && this.setCondition(res.data);
                res.data && this.form.initFormFields(res.data);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }

    getTypeForm() {
        let params = this.isNew ? {} : {
            fid: this.selectedTreeNodeInfo.domid
        }
        this.setDialogLoadingStatus(true);
        Api.getTypeForm(params).then(res => {
            if (res.code === 200) {
                this.setDialogLoadingStatus(false);
                res.data && this.setCondition(res.data);
                res.data && this.form.initFormFields(res.data);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }

    getSearchCondition() {
        this.setScLoadingStatus(false);
        Api.getAdvanceSearchCondition().then(res => {
            if (res.code === 200) {
                this.setScLoadingStatus(false);
                res.data && this.setSearchCondition(res.data);
                res.data && this.form2.initFormFields(res.data);
            } 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);
            } else {
                message.warning(res.msg);
            }
        }, error => {
            message.warning(error.msg);
        })
    }

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

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

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

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

    setFName(val) {
        this.fname = val;
    }

    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;
    }
    setOperateType(val) {
        this.operateType = val;
    }

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

    setFId(fId) {
        this.fId = fId;
    }

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


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

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

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

    setTreeLoading(bool) {
        this.treeLoading = bool;
    }

}