import { action, observable, toJS } from "mobx";
import * as API from "../apis/WorkflowArchiveService";

import { message, Modal } from "antd";
import { WeaForm, WeaTableNew, WeaLogView } from "comsMobx";
import { WeaLocaleProvider } from "ecCom";
import isFunction from 'lodash/isFunction';
import {checkTableFieldValueUnique}  from "../apis/CommonService";
import {
    getSearchFormHeight,
    checkNonstandard
} from '../util/IntegrationUtil';
const { TableStore } = WeaTableNew;
const { LogStore } = WeaLogView;
const getLabel = WeaLocaleProvider.getLabel;

export class ArchiveStore {
    @observable hasRight = true;
    //归档流程注册
    @observable tableStore = new TableStore();
    @observable searchForm = new WeaForm();
    @observable newForm = new WeaForm();
    @observable dialogSearchForm = new WeaForm();

    @observable newFormRegisterDB = new WeaForm();
    @observable newFormRegisterXML = new WeaForm();

    //归档日志相关
    @observable logTableStore = new TableStore();
    @observable logSearchForm = new WeaForm();
    //归档日志相关 modal tab3
    @observable editlogTableStore = new TableStore();
    @observable editlogSearchForm = new WeaForm();

    @observable ftpTableStore = new TableStore();
    @observable localTableStore = new TableStore();
    @observable dbTableStore = new TableStore();
    @observable schemeTableStore = new TableStore();

    //归档配置
    @observable settingRegisterFTPForm = new WeaForm();
    @observable settingRegisterLocalForm = new WeaForm();
    @observable settingRegisterDatabaseForm = new WeaForm();
    @observable settingRegisterXMLForm = new WeaForm();
    @observable settingRegisterDBForm = new WeaForm();

    @observable logStore = new LogStore();

    @observable settingRegisterID = '';
    @observable rightLoading = true;
    

    @observable
    status = {
        hasNonStandard: true,
        nonStandLoading: true,
        getNewDatailFormrightMenus:[],
        condition:[],

        selectedKey: 0,
        editId: -1,

        ArchiveFormFields: [],
        ArchiveFormHiddenFields: [],

        showSearchAd: false,
        showSearchAd2: false,
        rightMenus: [],
        detailTabSelectedKey: 'dt1',

        logRightMenus: [],
        ftpRightMenus: [],
        localRightMenus: [],
        dbRightMenus: [],
        schemeRightMenus: [],
        rowSelectIds: [],
        rowSelectRows: [],
        showNewFormDialog: false,
        showNewDetailFormDialog: false,
        advancedSearchHeight: 120,

        logArchiveVisible: false,
        logArchiveid: "84",

        ArchiveRegisterFormFields_DB: [],
        ArchiveRegisterFormHiddenFields_DB: [],

        ArchiveRegisterFormFields_XML: [],
        ArchiveRegisterFormHiddenFields_XML: [],
        ArchiveRegisterFormFieldLabels_XML: [],
        summary: '',
        warning:'',


        FTPDialogVisible: false,
        LocalDialogVisible: false,
        DatabaseDialogVisible: false,

        // 注册XML方案
        XMLDialogVisible: false,
        regitTypeOptions: {},
        defaultZhongXinXMlTemplate: '',

        // 注册数据库方案
        ConfigSchemeAddDialogVisible: false,
        configSchemehasDetail: false,
        configSchemedetailTableColumnsData: [],
        configSchemedetailTableColumnsSet: [],
        configSchememainTableColumnsData: [],
        configSchememainTableColumnsSet: [],
        expTableTypeOptions: {},
    };

    @action cleanStore = () => {
        this.searchForm = new WeaForm();
        this.logSearchForm = new WeaForm();
        this.status = {
            ...this.status,
            selectedKey: 0,
            ArchiveConditionInfo: [],
            ArchiveEditconditionInfo:[],

            ArchiveFormFields: [],
            ArchiveFormHiddenFields: [],

            ArchiveLogConditionInfo: [],
            ArchiveLogFormFields: [],

            showSearchAd: false,
            rightMenus: [],
            detailTabSelectedKey: 'dt1',

            logRightMenus: [],
            ftpRightMenus: [],
            localRightMenus: [],
            dbRightMenus: [],
            schemeRightMenus: [],
            rowSelectIds: [],
            rowSelectRows: [],
            showNewFormDialog: false,
            showNewDetailFormDialog: false,
            advancedSearchHeight: 120,

            logArchiveVisible: false,

            ArchiveRegisterFormFields_DB: [],
            ArchiveRegisterFormHiddenFields_DB: [],


            //编辑归档流程注册-XML方案字段映射
            ArchiveRegisterFormFields_XML: [],
            ArchiveRegisterFormHiddenFields_XML: [],
            ArchiveRegisterFormFieldLabels_XML: [],
            ArchiveRegisterForm_MainTableFieldsColumnsSet_XML: [],
            ArchiveRegisterForm_MainTableFieldsColumnsData_XML: [],
            //编辑归档流程注册-数据库方案字段映射
            fieldDetailColumnsSet: [],
            fieldDetailData: [],
            mainFixColumnSet: [],
            mainFixDatas: [],
            detailFixColumnSet: [],
            detailFixDatas: [],
            newDetailSelectedKey: 0,
            topTab: [
                {
                    color: "#000000",
                    groupid: "flowAll",
                    showcount: true,
                    title: `${getLabel(83253, "归档流程注册")}`,
                    viewcondition: 0
                }
                , {
                    color: "#000000",
                    groupid: "flowAll",
                    showcount: true,
                    title: `${getLabel(83254, "归档配置")}`,
                    viewcondition: 1
                }
                , {
                    color: "#000000",
                    groupid: "flowAll",
                    showcount: true,
                    title: `${getLabel(83255, "归档日志")}`,
                    viewcondition: 2
                }
            ],
            archieveTopTab: [
                {
                    color: "#000000",
                    groupid: "archieveTab",
                    showcount: true,
                    title: `${getLabel(83256, "归档FTP列表")}`,
                    viewcondition: "dt1"
                }
                , {
                    color: "#000000",
                    groupid: "archieveTab",
                    showcount: true,
                    title: `${getLabel(83257, "归档本地列表")}`,
                    viewcondition: "dt2"
                }
                , {
                    color: "#000000",
                    groupid: "archieveTab",
                    showcount: true,
                    title: `${getLabel(83259, "归档数据库列表")}`,
                    viewcondition: "dt3"
                }, {
                    color: "#000000",
                    groupid: "archieveTab",
                    showcount: true,
                    title: `${getLabel(83260, "归档方案列表")}`,
                    viewcondition: "dt4"
                }
            ]
        };
    }

    @action
    doInit = () => {
        this.setState({
          nonStandLoading: true
        })
        checkNonstandard({num: "076"}, (result) => {
            this.setState({
              hasNonStandard: result.isOpen,
              nonStandLoading: false
            })
            if(result.isOpen) {
                this.cleanStore();
                //归档流程注册
                this.getArchiveRegisterList();
                this.getArchiveRegisterCondition();
                this.getArchiveRegisterRightMenu();
                this.getArchiveRegisterEditForm();
                this.getArchiveRegisterEditFormLogRightMenu();
                this.getArchiveRegisterEditFormLogCondition();
                //归档日志
                // this.getArchiveLogCondition();
                // this.getArchiveLogRightMenu();
                // this.getArchiveLogList();
        
                //第二个标签页
                this.getArchiveConfigFTPRightMenu();
                this.getArchiveConfigFTPList();
        
                this.getArchiveConfigDatabaseRightMenu();
                this.getArchiveConfigDatabaseList();
        
                this.getArchiveConfigLocalRightMenu();
                this.getArchiveConfigLocalList();
        
                this.getArchiveConfigSchemeRightMenu();
                this.getArchiveConfigSchemeList();        
            }
          })
        
    };

    setState = (params = {}) => {
        let status = { ...this.status };
        Object.keys(params).forEach(key => {
            status[key] = params[key];
        });
        this.status = status;
    };


    //=============以下归档流程注册相关============
    getArchiveRegisterList = () => {
        let params = (this.searchForm && this.searchForm.getFormParams()) || {};
        API.getArchiveRegisterList(params).then(result => {
            if (result.ret && result.ret == "noright") {
                this.hasRight = false;
            } else {
                this.hasRight = true;
            }
            this.rightLoading = false;
            if (result.sessionkey) {
                this.tableStore.getDatas(result.sessionkey, 1);
            } else {
                this.status = { ...this.status, isAlertPage: true };
            }
        });
    };

    getArchiveRegisterRightMenu = (params) => {
        API.getArchiveRegisterRightMenu(params).then(result => {
            this.status = { ...this.status, rightMenus: result.rightMenus };
        });
    };

    getArchiveRegisterCondition = (params) => {
        API.getArchiveRegisterCondition(params).then(result => {
            if(result.condition) {
                this.searchForm.initFormFields(result.condition);
                let advancedSearchHeight = getSearchFormHeight(result.condition);
                this.setState({advancedSearchHeight});
            }
            this.status = {
                ...this.status,
                ArchiveConditionInfo: result.condition,
            };
        });
    };

    // getArchiveRegisterAddForm = (params) => {
    //     API.getArchiveRegisterAddForm(params).then(result => {
    //         this.newForm.initFormFields(result.condition);
    //         console.log(result)
    //         this.status = {
    //             ...this.status,
    //             ArchiveFormFields: result.condition,
    //             ArchiveFormHiddenFields: result.hiddenFields,
    //         };
    //     });
    // };

    getArchiveRegisterEditForm = (params) => {
        API.getArchiveRegisterEditForm(params).then(result => {
            this.newForm.initFormFields(result.condition);
            this.status = {
                ...this.status,
                ArchiveFormFields: result.condition,
                ArchiveFormHiddenFields: result.hiddenFields,
            };
        });
    };

    //todo
    getArchiveRegisterEditFormFieldXMLMapping = (params) => {
        API.getArchiveRegisterEditFormFieldXMLMapping(params).then(result => {
            this.newFormRegisterXML.initFormFields(result.condition);
            this.status = {
                ...this.status,
                ArchiveRegisterFormFields_XML: result.condition,
                ArchiveRegisterFormHiddenFields_XML: result.hiddenFields,
                ArchiveRegisterFormFieldLabels_XML: result.fieldLabels,
                ArchiveRegisterForm_MainTableFieldsColumnsSet_XML: result.mainTableFieldsXMLColumnsSet,
                ArchiveRegisterForm_MainTableFieldsColumnsData_XML: result.mainTableFieldsXMLColumnsData,
                summary: result.summary,
                warning: result.warning
            };
        });
    };

    getArchiveRegisterEditFormFieldDBMapping = (params={}) => {
        this.newFormRegisterDB = new WeaForm();
        API.getArchiveRegisterEditFormFieldDBMapping(params).then(result => {
            this.newFormRegisterDB.setCondition(result.condition);
            this.setState({
                ArchiveRegisterFormFields_DB: result.condition,
                ArchiveRegisterFormHiddenFields_DB: result.hiddenFields,
                fieldDetailColumnsSet: result.fieldDetailColumnsSet,
                fieldDetailData: result.fieldDetailData,
                mainFixColumnSet: result.mainFixColumnSet,
                mainFixDatas: result.mainFixDatas,
                detailFixColumnSet: result.detailFixColumnSet,
                detailFixDatas: result.detailFixDatas,
            });
        });
    };

    getArchiveRegisterEditFormLogList = (p={}) => {
        let params =Object.assign({}, p, ((this.dialogSearchForm && this.dialogSearchForm.getFormParams()) || {}));
        API.getArchiveRegisterEditFormLogList(params).then(result => {
            this.editlogTableStore.getDatas(result.sessionkey, 1);
        });
    };

    getArchiveRegisterEditFormLogCondition = (params) => {
        API.getArchiveRegisterEditFormLogCondition(params).then(result => {
            result.condition && result.condition[0] && result.condition[0].items && result.condition[0].items.splice(3,1);
            this.dialogSearchForm.initFormFields(result.condition);
            this.status = {
                ...this.status,
                ArchiveEditconditionInfo: result.condition,
            };

        });
    };

    getArchiveRegisterEditFormLogRightMenu = (params) => {
        API.getArchiveRegisterEditFormLogRightMenu(params).then(result => {
            this.status.getNewDatailFormrightMenus = result.rightMenus
        });
    };


    getArchiveRegisterOperation = (params={}, callback) => {
        API.getArchiveRegisterOperation(params).then(result => {
            if (params && params.operation == 'ExpGetProInf') {

            } else if (params && params.operation == 'exp') {
                if(this.status.showNewDetailFormDialog) {
                    this.editlogTableStore.reloadCurrent();
                } else {
                    //this.tableStore.reloadCurrent();
					//this.logTableStore.reloadCurrent();
					this.getArchiveLogList();
                }
            }
            else {
                if(result.status == '1') {
                    this.getArchiveRegisterList();
                    this.setState({ showNewDetailFormDialog: false, rowSelectIds: [], rowSelectRows: [] });
                    if(params.operation == 'delete') {
                        message.success(getLabel(20461, '删除成功'));
                    }
                }
            }
            if (!callback) {
                if(result.status == "1") {
                  if (result.errmsgTip) {
                    message.success(result.errmsgTip);
                  }
                }
            }
            isFunction(callback) && callback(result);
        });

    };




    //=============以下归档日志相关============
    getArchiveLogList = () => {
        let params = (this.logSearchForm && this.logSearchForm.getFormParams()) || {};
        API.getArchiveLogList(params).then(result => {
            if (result.sessionkey) {
                this.logTableStore.getDatas(result.sessionkey, 1);
            } else {
                this.status = { ...this.status, isAlertPage: true };
            }
        });
    };

    getArchiveLogRightMenu = (params) => {
        API.getArchiveLogRightMenu(params).then(result => {
            this.status = { ...this.status, logRightMenus: result.rightMenus };
        });
    };

    getArchiveLogCondition = (params) => {
        API.getArchiveLogCondition(params).then(result => {
            if(result.condition) {
                this.logSearchForm.initFormFields(result.condition);
                let advancedSearchHeight = getSearchFormHeight(result.condition);
                this.setState({advancedSearchHeight});
            }
            this.status = {
                ...this.status,
                ArchiveLogConditionInfo: result.condition,
            };
        });
    };


    //=============以下ftp相关============
    getArchiveConfigFTPList = () => {
        let params = {};
        API.getArchiveConfigFTPList(params).then(result => {
            if (result.sessionkey) {
                this.ftpTableStore.getDatas(result.sessionkey, 1);
            } else {
                this.status = { ...this.status, isAlertPage: true };
            }
        });
    };

    getArchiveConfigFTPRightMenu = (params) => {
        API.getArchiveConfigFTPRightMenu(params).then(result => {
            this.status = { ...this.status, ftpRightMenus: result.rightMenus };
        });
    };

    getArchiveConfigFTPAddForm = (params) => {
        API.getArchiveConfigFTPAddForm(params).then(result => {
            this.settingRegisterFTPForm = new WeaForm();
            this.settingRegisterFTPForm.setCondition(result.condition);
            this.status.FTPDialogVisible = true;
            this.settingRegisterID = '';
        });
    };

    getArchiveConfigFTPEditForm = (params) => {
        API.getArchiveConfigFTPEditForm(params).then(result => {
            this.settingRegisterFTPForm = new WeaForm();
            this.settingRegisterFTPForm.setCondition(result.condition);
            this.status.FTPDialogVisible = true;
            this.settingRegisterID = params.id;
        });
    };
    getArchiveConfigFTPOperation = (ps) => {
        let params = this.settingRegisterFTPForm.getFormParams();
        if (this.settingRegisterID) {
            params.id = this.settingRegisterID;
            params.operation = 'edit';
        } else {
            params.operation = 'add';
        }
        const cb = () => {
            API.getArchiveConfigFTPOperation(params).then(result => {
                this.status.FTPDialogVisible = false;
                this.ftpTableStore.reloadCurrent();
                if(result.status == '1' && params.operation == 'delete') {
                    message.success(getLabel(20461, '删除成功'));
                }else if(result.status == '1') {
                    message.success(getLabel(22619,'保存成功'));
                }
            });
        }
        if (ps && ps.operation == 'delete') {
            params = ps;
            cb();
        } else {
            this.settingRegisterFTPForm.validateForm().then(f => {
                if (f.isValid) {
                  let checkParams = {
                    tableName: 'exp_ftpdetail',
                    fieldName: 'name',
                    fieldValue: this.settingRegisterFTPForm.getFormParams().name,
                    idName: 'id',
                    idValue: this.settingRegisterID
                  };
                  checkTableFieldValueUnique(checkParams).then(result => {
                    if(result&&result.isNotUnique) {
                      this.settingRegisterFTPForm.showError('name',getLabel('129943','名称已存在'));
                      // message.error(getLabel('387654','标识已存在'));
                    }else{
                      cb();
                    }
                  });

                } else {
                    f.showErrors();
                }
            });
        }
    };
    getArchiveConfigLocalAddForm = (params) => {
        API.getArchiveConfigLocalAddForm(params).then(result => {
            this.settingRegisterLocalForm = new WeaForm();
            this.status.LocalDialogVisible = true;
            this.settingRegisterLocalForm.setCondition(result.condition);
            this.settingRegisterID = '';
        });
    };
    getArchiveConfigLocalEditForm = (params) => {
        API.getArchiveConfigLocalEditForm(params).then(result => {
            this.settingRegisterLocalForm = new WeaForm();
            this.status.LocalDialogVisible = true;
            this.settingRegisterLocalForm.setCondition(result.condition);
            this.settingRegisterID = params.id;
        });
    };
    getArchiveConfigLocalOperation = (ps) => {
        let params = this.settingRegisterLocalForm.getFormParams();
        if (this.settingRegisterID) {
            params.id = this.settingRegisterID;
            params.operation = 'edit';
        } else {
            params.operation = 'add';
        }
        const cb = () => {
            API.getArchiveConfigLocalOperation(params).then(result => {
                this.status.LocalDialogVisible = false;
                this.localTableStore.reloadCurrent();
                if(result.status == '1' && params.operation == 'delete') {
                    message.success(getLabel(20461, '删除成功'));
                }else if(result.status == '1') {
                    message.success(getLabel(22619,'保存成功'));
                }
            });
        }
        if (ps && ps.operation == 'delete') {
            params = ps;
            cb()
        } else {
            this.settingRegisterLocalForm.validateForm().then(f => {
                if (f.isValid) {
                  let checkParams = {
                    tableName: 'exp_localdetail',
                    fieldName: 'name',
                    fieldValue: this.settingRegisterLocalForm.getFormParams().name,
                    idName: 'id',
                    idValue: this.settingRegisterID
                  };
                  checkTableFieldValueUnique(checkParams).then(result => {
                    if(result&&result.isNotUnique) {
                      this.settingRegisterLocalForm.showError('name',getLabel('129943','名称已存在'));
                      // message.error(getLabel('387654','标识已存在'));
                    }else{
                      cb();
                    }
                  });

                } else {
                    f.showErrors();
                }
            });
        }
    };
    getArchiveConfigDatabaseAddForm = (params) => {
        API.getArchiveConfigDatabaseAddForm(params).then(result => {
            this.settingRegisterDatabaseForm = new WeaForm();
            this.settingRegisterDatabaseForm.setCondition(result.condition);
            this.status.DatabaseDialogVisible = true;
            this.settingRegisterID = '';
        });
    };
    getArchiveConfigDatabaseEditForm = (params) => {
        API.getArchiveConfigDatabaseEditForm(params).then(result => {
            this.settingRegisterDatabaseForm = new WeaForm();
            this.settingRegisterDatabaseForm.setCondition(result.condition);
            this.status.DatabaseDialogVisible = true;
            this.settingRegisterID = params.id;
        });
    };

    checkArchiveConfigDatabaseTableExists=(callback)=>{
      let params = this.settingRegisterDatabaseForm.getFormParams();
      params.operation = "checkTableExists";
      API.getArchiveConfigDatabaseOperation(params).then(result => {
        if(result.notExistMainTable){
          this.settingRegisterDatabaseForm.showError('maintable',getLabel('125711','主表在对应的数据源里不存在！'));
          return;
        }
        if(result.notExistDetailTable){
          this.settingRegisterDatabaseForm.showError('detailtable',getLabel('125712','明细表在对应的数据源里不存在！'));
          return;
        }
        isFunction(callback)&& callback();
      });
    }

    getArchiveConfigDatabaseOperation = (ps) => {
        let params = this.settingRegisterDatabaseForm.getFormParams();
        if (this.settingRegisterID) {
            params.id = this.settingRegisterID;
            params.operation = 'edit';
        } else {
            params.operation = 'add';
        }
        const cb = () => {
            API.getArchiveConfigDatabaseOperation(params).then(result => {
                this.status.DatabaseDialogVisible = false;
                this.dbTableStore.reloadCurrent();
                if(result.status == '1' && params.operation == 'delete') {
                    message.success(getLabel(20461, '删除成功'));
                }else if(result.status == '1') {
                    message.success(getLabel(22619,'保存成功'));
                }
            });
        }
        if (ps && ps.operation == 'delete') {
            params = ps;
            cb();
        } else {
            this.settingRegisterDatabaseForm.validateForm().then(f => {
                if (f.isValid) {
                  let checkParams = {
                    tableName: 'exp_dbdetail',
                    fieldName: 'name',
                    fieldValue: this.settingRegisterDatabaseForm.getFormParams().name,
                    idName: 'id',
                    idValue: this.settingRegisterID
                  };
                  checkTableFieldValueUnique(checkParams).then(result => {
                    if(result&&result.isNotUnique) {
                      this.settingRegisterDatabaseForm.showError('name',getLabel('129943','名称已存在'));
                      // message.error(getLabel('387654','标识已存在'));
                    }else{
                      cb();
                    }
                  });
                } else {
                    f.showErrors();
                }
            });
        }
    };
    getArchiveConfigSchemeAddFormXML = (params) => {
        API.getArchiveConfigSchemeAddFormXML().then(result => {
            this.settingRegisterXMLForm = new WeaForm();
            if(result.condition && result.condition.length > 2) {
              result.condition[0].items && (result.condition[0].items.find(field => field.domkey[0] == 'syncmethodHz').otherParams = {valueIsObject: true});
              result.condition[1].items && (result.condition[1].items.find(field => field.domkey[0] == 'xmltext').maxRows = 50);
            }
            this.settingRegisterXMLForm.setCondition(result.condition);
            this.status.condition = result.condition;
            this.status.XMLDialogVisible = true;
            this.status.regitTypeOptions = result.regitTypeOptions;
            this.status.defaultZhongXinXMlTemplate = result.defaultZhongXinXMlTemplate;
            this.settingRegisterID = '';
        });
    };
    getArchiveConfigSchemeEditFormXML = (params) => {
        API.getArchiveConfigSchemeEditFormXML(params).then(result => {
            this.settingRegisterXMLForm = new WeaForm();       
            if(result.condition && result.condition.length > 2) {
              result.condition[0].items && (result.condition[0].items.find(field => field.domkey[0] == 'syncmethodHz').otherParams = {valueIsObject: true});
              result.condition[1].items && (result.condition[1].items.find(field => field.domkey[0] == 'xmltext').maxRows = 50);
            }
            this.settingRegisterXMLForm.setCondition(result.condition);
            this.status.XMLDialogVisible = true;
            this.status.regitTypeOptions = result.regitTypeOptions;
            this.status.defaultZhongXinXMlTemplate = result.defaultZhongXinXMlTemplate;
            this.settingRegisterID = params.id;
        });
    };
    getArchiveConfigSchemeOperationXML = (ps) => {
        let params = this.settingRegisterXMLForm.getFormParams();
        let syncmethodHz = params.syncmethodHz;
        delete params.syncmethodHz;
        params = Object.assign({}, params, syncmethodHz);
        
        if (this.settingRegisterID) {
            params.id = this.settingRegisterID;
            params.operation = 'edit';
        } else {
            params.operation = 'add';
        }
        const cb = () => {
            API.getArchiveConfigSchemeOperationXML(params).then(result => {
                this.status.XMLDialogVisible = false;
                this.schemeTableStore.reloadCurrent();
                if(result.status == '1' && params.operation == 'delete') {
                    message.success(getLabel(20461, '删除成功'));
                }else if(result.status == '1') {
                    message.success(getLabel(22619,'保存成功'));
                }
            });
        }
        if (ps && ps.operation == 'delete') {
            params = ps;
            cb();
        } else {
            this.settingRegisterXMLForm.validateForm().then(f => {
                if (f.isValid) {
                  let checkParams = {
                    tableName: 'exp_XMLProSettings',
                    fieldName: 'name',
                    fieldValue: this.settingRegisterXMLForm.getFormParams().name,
                    idName: 'id',
                    idValue: this.settingRegisterID
                  };
                  checkTableFieldValueUnique(checkParams).then(result => {
                    if(result&&result.isNotUnique) {
                      this.settingRegisterXMLForm.showError('name',getLabel('129943','名称已存在'));
                    }else{
                      cb();
                    }
                  });
                } else {
                    f.showErrors();
                }
            });
        }
    };
    getArchiveConfigSchemeAddFormDB = () => {
        API.getArchiveConfigSchemeAddFormDB().then(result => {
            if(result.condition && result.condition.length > 1) {
              result.condition[0].items && (result.condition[0].items.find(field => field.domkey[0] == 'syncmethodHz').otherParams = {valueIsObject: true});
            }
            this.settingRegisterDBForm = new WeaForm();
            this.settingRegisterDBForm.setCondition(result.condition);
            this.status.ConfigSchemeAddDialogVisible = true;
            this.status.regitTypeOptions = result.regitTypeOptions;
            this.status.configSchemehasDetail = result.hasDetail;
            this.status.configSchemedetailTableColumnsData = result.detailTableColumnsData;
            this.status.configSchemedetailTableColumnsSet = result.detailTableColumnsSet;
            this.status.configSchememainTableColumnsData = result.mainTableColumnsData;
            this.status.configSchememainTableColumnsSet = result.mainTableColumnsSet;
            this.status.expTableTypeOptions = result.expTableTypeOptions;
            this.settingRegisterID = '';
        });
    };
    getArchiveConfigSchemeEditFormDB = (params) => {
        API.getArchiveConfigSchemeEditFormDB(params).then(result => {
            if(result.condition && result.condition.length > 1) {
              result.condition[0].items && (result.condition[0].items.find(field => field.domkey[0] == 'syncmethodHz').otherParams = {valueIsObject: true});
            }
            this.settingRegisterDBForm = new WeaForm();
            this.settingRegisterDBForm.setCondition(result.condition);
            this.status.ConfigSchemeAddDialogVisible = true;
            this.status.regitTypeOptions = result.regitTypeOptions;
            this.status.configSchemehasDetail = result.hasDetail;
            this.status.configSchemedetailTableColumnsData = result.detailTableColumnsData;
            // 临时mock 处理下明细只读，后端改好后删掉
            result.detailTableColumnsSet = result.detailTableColumnsSet.map(item => {
                if (item.com[0].type === "CHECKBOX") {
                    delete item.com[0].otherParams;
                    item.com[0].viewAttr = "2";
                }
                return item;
            })
            // console.log(result.detailTableColumnsSet)
            this.status.configSchemedetailTableColumnsSet = result.detailTableColumnsSet;
            this.status.configSchememainTableColumnsData = result.mainTableColumnsData;
            this.status.configSchememainTableColumnsSet = result.mainTableColumnsSet;
            this.status.expTableTypeOptions = result.expTableTypeOptions;
            this.settingRegisterID = params.id;
        });
    };
    getArchiveConfigSchemeOperationDB = (ps) => {
        const {
            configSchemedetailTableColumnsData=[],
            configSchememainTableColumnsData=[]
        } = this.status;
        let params = this.settingRegisterDBForm.getFormParams();
        params.mainlist = JSON.stringify(configSchememainTableColumnsData);
        params.detaillist = JSON.stringify(configSchemedetailTableColumnsData);
        let syncmethodHz = params.syncmethodHz;
        delete params.syncmethodHz;
        params = Object.assign({}, params, syncmethodHz);
        if (this.settingRegisterID) {
            params.id = this.settingRegisterID;
            params.operation = 'edit';
        } else {
            params.operation = 'add';
        }
        const cb = () => {
            API.getArchiveConfigSchemeOperationDB(params).then(result => {
                this.status.ConfigSchemeAddDialogVisible = false;
                this.schemeTableStore.reloadCurrent();
                if(result.status == '1') {
                  message.success(getLabel(22619,'保存成功'));
                }
            });
        }
        if (ps && ps.operation == 'delete') {
            params = ps;
            cb();
        } else {
            if(params.FileSaveType == 2) {
              // 数据库方案隐藏字段 
              let datas = [];
              if(params.ExpWorkflowFileFlag == 1) {
                datas.push("ExpWorkflowFilePath");
              }
              if(params.ExpWorkflowInfoFlag == 1) {
                datas.push("ExpWorkflowInfoPath");
              }
              if(params.ExpSignFileFlag == 1) {
                datas.push("ExpSignFilePath");
              }
              window.e9HideFormFieldKeys = datas;
              // 主键等必填项的判断
              let needHandleArray = params.expTableType === "0" ?
                [configSchemedetailTableColumnsData, configSchememainTableColumnsData] :
                [configSchememainTableColumnsData, configSchemedetailTableColumnsData];
              let needHandleKey = params.expTableType === "0" ? ["dtiskey", "mainis"] : ["mainiskey", "dtis"];
              // 主键必须有一个勾选
              if(needHandleArray[0] && needHandleArray[0].length) {
                  let paramKey = needHandleArray[0].find(item => item[needHandleKey[0]] == '1');
                  if(!paramKey) {
                    message.error(getLabel('128193','请勾选必填项'));
                    return;
                  }
              } else {
                // if(params.expTableType != "0") {
                //     message.error(getLabel('517269','请设置主表字段'));
                //     return;
                // }
              }
              if(needHandleArray[1] && needHandleArray[1].length && params.expTableType != "0") {
                  // 所有的check框都必须要有一条数据勾选
                  let isKey = false, isDoc = false, isDoctype = false, isDocname = false, isMainkey = false;
                  needHandleArray[1].forEach(item => {
                    if(item[`${needHandleKey[1]}key`] == '1') {
                        isKey = true;
                    }
                    if(item[`${needHandleKey[1]}doc`] == '1') {
                        isDoc = true;
                    }
                    if(item[`${needHandleKey[1]}doctype`] == '1') {
                        isDoctype = true;
                    }
                    if(item[`${needHandleKey[1]}docname`] == '1') {
                        isDocname = true;
                    }
                    if(item[`${needHandleKey[1]}mainkey`] == '1') {
                        isMainkey = true;
                    }
                  });
                  if(!(isKey && isDoc && isDoctype && isDocname && (params.expTableType === "0" || (params.expTableType === "1" && isMainkey)))) {
                    message.error(getLabel('128193','请勾选必填项'));
                    return;
                  }
              }
            }
            this.settingRegisterDBForm.validateForm().then(f => {
                if (f.isValid) {
                  let checkParams = {
                    tableName: 'exp_DBProSettings',
                    fieldName: 'name',
                    fieldValue: this.settingRegisterDBForm.getFormParams().name,
                    idName: 'id',
                    idValue: this.settingRegisterID
                  };
                  checkTableFieldValueUnique(checkParams).then(result => {
                    if(result&&result.isNotUnique) {
                      this.settingRegisterDBForm.showError('name',getLabel('129943','名称已存在'));
                    }else{
                      cb();
                    }
                  });
                } else {
                    f.showErrors();
                }
            });
        }
    };


    //=============以下local相关============
    getArchiveConfigLocalList = () => {
        let params = {};
        API.getArchiveConfigLocalList(params).then(result => {
            if (result.sessionkey) {
                this.localTableStore.getDatas(result.sessionkey, 1);
            } else {
                this.status = { ...this.status, isAlertPage: true };
            }
        });
    };

    getArchiveConfigLocalRightMenu = (params) => {
        API.getArchiveConfigLocalRightMenu(params).then(result => {
            this.status = { ...this.status, localRightMenus: result.rightMenus };
        });
    };


    //=============以下db相关============
    getArchiveConfigDatabaseList = () => {
        let params = {};
        API.getArchiveConfigDatabaseList(params).then(result => {
            if (result.sessionkey) {
                this.dbTableStore.getDatas(result.sessionkey, 1);
            } else {
                this.status = { ...this.status, isAlertPage: true };
            }
        });
    };

    getArchiveConfigDatabaseRightMenu = (params) => {
        API.getArchiveConfigDatabaseRightMenu(params).then(result => {
            this.status = { ...this.status, dbRightMenus: result.rightMenus };
        });
    };

    //=============以下scheme相关============
    getArchiveConfigSchemeList = () => {
        let params = {};
        API.getArchiveConfigSchemeList(params).then(result => {
            if (result.sessionkey) {
                this.schemeTableStore.getDatas(result.sessionkey, 1);
            } else {
                this.status = { ...this.status, isAlertPage: true };
            }
        });
    };

    getArchiveConfigSchemeRightMenu = (params) => {
        API.getArchiveConfigSchemeRightMenu(params).then(result => {
            this.status = { ...this.status, schemeRightMenus: result.rightMenus };
        });
    };

    saveRegisterXML() {
        let params = this.settingRegisterXMLForm.getFormParams();
        let syncmethodHz = params.syncmethodHz;
        delete params.syncmethodHz;
        params = Object.assign({}, params, syncmethodHz);
        if (this.settingRegisterXMLID) {

        }
        this.settingRegisterXMLForm.validateForm().then(f => {
            if (f.isValid) {
                API.getArchiveConfigSchemeOperationXML(params).then(result => {
                });
            } else {
                f.showErrors();
            }
        });
    }
    //确认

    confirmModal = (content, func, val) => {
        //确认信息， 方法， 方法参数
        Modal.confirm({
            title: `${getLabel(131329, '信息确认')}`,
            content: content,
            okText: `${getLabel(16631, '确定')}`,
            cancelText: `${getLabel(201, '取消')}`,
            onOk: (typeof func === 'function') ? () => func(val) : ()=>{},
        })
    };

    setConfigSchememainTableColumnsSetBrowserConditionParam = (value) => {
        this.status.configSchememainTableColumnsSet[0].com[0].browserConditionParam.dataParams.dmlformid = value;
        this.status.configSchemedetailTableColumnsSet[0].com[0].browserConditionParam.dataParams.dmlformid = value;
    }
}
