import {action, observable, asMap} from 'mobx';
import {WeaForm, WeaTableNew} from 'comsMobx';
import * as API from "../../apis/Base.js";

const {TableStore} = WeaTableNew;

export default class BaseSetStore {

    //基础信息维护表单
    @observable baseSetForm = new WeaForm();

    //查询表单
    @observable searchForm = new WeaForm();

    //列表
    @observable tableStore = new TableStore();

    //TableEdit数据
    @observable tableEditDatas = observable.map ? observable.map({}) : asMap({});

    //Table待保存数据
    tableRowDatas = {};

    //初始化TableEdit状态
    tableInitStatus = observable.map ? observable.map({}) : asMap({});

    //初始化Cells
    cells = observable.map ? observable.map({}) : asMap({});;

    pkqSelectedKey = '';
	
	//保存按钮显示控制
    buttonShowStatus = '';
    
    baseModifyType = '';

    // 日志显示内容
    @observable logValue = '';

    @observable status = {

        showDialog: false,
        //API地址
        apiUrl: {},

        datas: {},

        initTableData:{},

        isAlertPage: false,
        //标题信息
        topinfo: {},
        //菜单
        menuinfo: [],
        //收藏
        collectInfo: {},
        //表单
        fieldsinfo: [],
        //tableEdit
        tablesinfo: [],
        //说明
        description: [],
        //按钮信息
        buttoninfo: [],
        //更多按钮
        moreBtninfo: [],
        // 测试table列和标题
        testColumns: [],
        testTitle: {},

        // 弹出框类型
        dialogType: '',

        // 选择行信息
        rowSelectIds:[],

         //列信息
         colums:[],

        // 批量合并测试数据
        testMatchColumns: [],
        testMatchDatas: [],

         //显示高级搜索
        showSearchAd: false,

        showLogDetailDialog: false,
        loading: false
    };

    @action 
    setPKQSelectedKey = (val) => {
        this.pkqSelectedKey = val;
    }
	
	setButtonShowStatus = (val) => {
		this.buttonShowStatus = val;
    }
    
    setInputModifyType = (val) => {
		this.baseModifyType = val;
    }
    
    @action
    setSyncLogValue = (val) => {
		this.logValue = val;
	}

    @observable
    createTask = {
        editData: {
            runstatus: "",
            cronexpr: ""
        },
    };

    @action
    //tableInit初始化准备OK
    tableInit(tableKey){
        this.tableInitStatus.set(tableKey,true);
    }

    @action
    //页面初始化
    doInit = (initTableEdit,initCustomColum,initTableData) => {
        this.setState({
            topinfo: {},
            menuinfo: [],
            collectInfo: {},
            fieldsinfo: [],
            tablesinfo: [],
            description: [],
            buttoninfo: [],
            moreBtninfo: [],
            testColumns: [],
            testTitle: {},
            loading: false,
        });
        this.baseSetForm = new WeaForm();
        this.tableRowDatas = {};
        this.tableEditDatas = observable.map ? observable.map({}) : asMap({});
        this.tableInitStatus = observable.map ? observable.map({}) : asMap({});
        this.cells = observable.map ? observable.map({}) : asMap({});
        this.getMenu();
        this.getFields(initTableEdit,initCustomColum,initTableData);
    };

    setState = (params = {}) => {
        Object.assign(this.status, params);
    }

    //获取右键菜单
    getMenu = () => {
        let params = this.status.datas || {};

        API.getRightMenu(this.status.apiUrl, params).then(result => {
            if (result.status) {
                this.status = {
                    ...this.status,
                    menuinfo: result.data.rightMenus,
                    collectinfo: result.data.collectParams,
                    topinfo: result.data.top,
                    buttoninfo: result.data.button,
                    moreBtninfo: result.data.moreButton
                };
            }
        });
    }

    
    //获取基础信息表单
    getFields = (initTableEdit,initCustomColum,initTableData) => {
        let params = this.status.datas || {};
        
        API.getFields(this.status.apiUrl, params).then(result => {
                if (result.status) {
                    if (result.data) {
                        if (result.data.fields) {
                            if(params.ldapType) {
                              this.baseSetForm.initFormFields(result.data.fields);
                              this.status = {...this.status, fieldsinfo: result.data.fields};
                            }else {
                              if(params.ldapId) {
                                result.data.fields[0] && result.data.fields[0].items && result.data.fields[0].items.find(item => item.domkey[0]==='ldapid') 
                                  && (result.data.fields[0].items.find(item => item.domkey[0]==='ldapid').hasBorder = true); 
                              }else if(params.formartid) {
                                result.data.fields[0] && result.data.fields[0].items && result.data.fields[0].items.map(item => item.hasBorder = true);
                              }
                              this.baseSetForm.initFormFields(result.data.fields);
                              this.status = {...this.status, fieldsinfo: result.data.fields};
                            }
                        }
                        if (result.data.tables) {  
                            let tables = result.data.tables;
                            tables && tables.map((c,ci) => {
                                c.items && c.items.map((f,fi) => {
    
                                    if(initTableEdit && typeof (initTableEdit) == "function"){
                                        initTableEdit(f.ref);
                                    }else{
                                        this.tableInit(f.ref);
                                    }
                                    
                                    if(f.dataUrl){
                                        this.getTableEditData(f.dataUrl,f.ref,f.rowKey?f.rowKey : "id",initTableData);
                                    } else {
                                        this.tableEditDatas.set(f.ref,[]);
                                    }
                                    f.columns && f.columns.map((fc,fci) => {
    
                                        fc.com && fc.com.map((com,comi) => {
                                            if(com.type && com.type == "custom"){
                                                if(initCustomColum && typeof (initCustomColum) == "function"){
                                                    tables[ci].items[fi].columns[fci].com[comi] = initCustomColum(f.ref,com);
                                                } else {
                                                    tables[ci].items[fi].columns[fci].com[comi] = {};
                                                }
                                            }
                                        });
                                       
                                    });
                                });
                            });
                            this.status = {...this.status, tablesinfo: tables};
                        }
                        if (result.data.description) {
    
                            this.status = {...this.status, description: result.data.description};
                        }
                    }
                }
        });
    };

    //获取tableEdit 数据内容
    getTableEditData = (url, editType,id,initTableData) => {

        const params = this.status.datas || {};

        API.getOfGetDatas(url, params).then(result => {
            if (result.status) {
                if (result.data) {
                    this.tableEditDatas.set(editType,result.data);
                    this.tableRowDatas[editType] = result.data;
                    if(initTableData && typeof(initTableData)=="function"){
                        let obj = initTableData(editType,id,result.data);
                        if(obj && obj.cells){
                            this.cells.set(editType, obj.cells);
                        }
                    }
                } else {
                    this.tableEditDatas.set(editType,[]);
                    this.tableRowDatas[editType] = [];
                    this.cells.set(editType,[]);
                }
            }
        });
    }

    // 获取JAVA方法可选项
    @action getJavaAnalytic = (params) => {
        API.getJavaAnalytic(this.status.apiUrl,params).then(result => {
            if (result.status) {
                if (result.status) {
                    this.status = {
                        ...this.status
                    };
                }
            }
        })
    }

    @action doJavaClazzName = (v) => {//java服务调用类 onBlur、重新解析
        const formParams = this.baseSetForm.getFormParams(); 
        const clazzName = v || formParams.formartclass;
    
        if(clazzName !== ''){
          this.getJavaAnalytic({
            clazzName: clazzName
          });
        }
      }

    // //获取右键菜单
    // getMenu = () => {
    //     API.getRightMenu(this.status.apiUrl).then(result => {
    //         if (result.status) {
    //             this.status = {
    //                 ...this.status,
    //                 menuinfo: result.data.rightMenus,
    //                 collectinfo: result.data.collectParams,
    //                 topinfo: result.data.top,
    //                 buttoninfo: result.data.button
    //             };
    //         }
    //     });
    // }

    //获取查询条件
    getCondition = () => {
        API.getCondition(this.status.apiUrl).then(result => {
            if (result.status) {
                this.searchForm.initFormFields(result.data.condition);
				//this.searchForm.init(result.data.condition);
                this.status = {...this.status, conditioninfo: result.data.condition};
            }
        });
    }

    //显示高级搜索
    setShowSearchAd = (showSearchAd) => {
        this.status = {...this.status, showSearchAd: showSearchAd}
    }

    //获取列表数据
    getDatas = (isOnChange,datas) => {
        let params;
        params = this.searchForm && this.searchForm.getFormParams() || {};
        Object.assign(params, {ldapId: datas.ldapId});
        //let params = this.searchForm && this.searchForm.getFormParams() || {};
        API.getDatas(this.status.apiUrl,params).then(result => {

            if (result.status) {
                if (result.data.sessionkey) {
                    this.tableStore = new TableStore();
                    isOnChange ? this.tableStore.getDatas(result.data.sessionkey, 1) : this.tableStore.getDatas(result.data.sessionkey);
                    this.status = {...this.status, logType: result.data.logType, logSmallType: result.data.logSmallType}
                }
            }
        });
    }

    /**
     * 显示同步日志信息
     */
    getLogDetial = (datas, lineNumber) => {

        API.getLogDetial(datas.logId, {lineNumber: lineNumber}).then(result => {
            this.logValue += result.data;
            this.setSyncLogValue(this.logValue);
            let currentLineNumber;
            if(result.status == true){
                currentLineNumber = 0;
            }else if(result.status == false && "106" == result.errorCode){
                if (result.ext && !isNaN(result.ext) && parseInt(result.ext) >= 0) {
                    currentLineNumber = parseInt(result.ext) + 1;
                    setTimeout((datas, currentLineNumber) => {
                        this.getLogDetial(datas, currentLineNumber)
                    }, 200, datas, currentLineNumber);
                }
            }
        },(error,datas) => {
            if(datas && "106" == result.errorCode) {
                if (result.ext && !isNaN(result.ext) && parseInt(result.ext) >= 0) {
                    currentLineNumber = parseInt(result.ext) + 1;
                    setTimeout((datas, currentLineNumber) => {
                        this.getLogDetial(datas, currentLineNumber)
                    }, 200, datas, currentLineNumber);
                }
            }
        });
    }
}