import React from 'react'
import {toJS} from 'mobx';
import {observer} from 'mobx-react';
import {Button} from 'antd';
import {
    WeaFormItem,
    WeaLocaleProvider,
    WeaSearchGroup,
    WeaTools,
    WeaCron,
    WeaInput
} from 'ecCom';

import {WeaSwitch} from 'comsMobx';
import {checkTableFieldValueUnique}  from "../../../apis/CommonService";

const {getKey} = WeaTools;
const getLabel = WeaLocaleProvider.getLabel;

@observer
export default class SchedulePlugins extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
        };
    }

    /**
     * FORM表单控件修改触发
     * @param changeItem
     * @param v
     */
    onFormChange(changeItem, v) {
        const {baseSetStore} = this.props;
        const {baseSetForm} = baseSetStore;

        let onChange = false;
        let defaultValue = {};

        if(v.pointid) {
            let checkParams = {
                tableName: 'schedulesetting',
                fieldName: 'pointid',
                fieldValue: v.pointid.value.replace(/^(\s*)|(\s*)$/g, ''),
                idName: 'id',
                idValue: ''
            };
            checkTableFieldValueUnique(checkParams).then(result => {
                if(result&&result.isNotUnique) {
                    baseSetForm.showError('pointid',getLabel('387654','标识已存在'));
                }
            });
        }

        if (changeItem) {
            for (let item of changeItem) {
                if (item.value) {
                    if (item.key && v[item.key] && item.value == v[item.key].value) {

                        baseSetForm.updateFields(item.change);
                        onChange = false;
                        break;
                    }
                } else if (item.key && v[item.key]) {
                    onChange = true;
                    defaultValue = item.change;
                }
            }

            if (onChange) {
                baseSetForm.updateFields(defaultValue);
            }
        }
    }

    getFormItem() {
        const {baseSetStore} = this.props;
        const {status, baseSetForm} = baseSetStore;
        const {fieldsinfo} = toJS(status);
        const {isFormInit} = baseSetForm;
        const formParams = baseSetForm.getFormParams();
        let group = [];

        isFormInit && fieldsinfo && fieldsinfo.map((c, index) => {
            let items = [];
            c.items && c.items.map(f => {
                items.push({
                    com: (<WeaFormItem
                        ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@integrationScheduleWeaFormItem@integrationldapschedulePluginForm${f.domkey[0]}`}
                        label={`${f.label}`}
                        labelCol={{span: `${f.labelcol}`}}
                        error={baseSetForm.getError(f)}
                        wrapperCol={{span: `${f.fieldcol}`}}
                        hideLabel={f.hideLabel}
                        tipPosition="bottom"
                    >
                       {
                          getKey(f) != 'cronexpr' 
                            ? <WeaSwitch fieldConfig={f} ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@integrationScheduleWeaSwitch@integrationldapschedulePluginForm${f.domkey[0]}`} form={baseSetForm} formParams={formParams} onChange={(v) => this.onFormChange(f.changeItem, v)} />
                            :  <div>
                                <nobr>
                                  <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@integrationScheduleInput@integrationldapschedulePluginForm${f.domkey[0]}`} {...f} value={formParams.cronexpr} onChange={v => baseSetForm.updateFields({cronexpr: v.trim()})} />
                                  <div style={{display: 'inline-block'}}>
                                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@integrationldapset@integrationldapschedulePluginButtonset${f.domkey[0]}`}  onClick={() => $("#scheduleCronexpr").toggle()}>{getLabel('30747','设置')}</Button>
                                    <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@integrationldapreset@integrationldapschedulePluginButtonreset${f.domkey[0]}`}  onClick={() => baseSetForm.updateFields({cronexpr: '0 0 * * * ?'})} style={{marginLeft: 10}}>{getLabel('2022','重置')}</Button>
                                  </div>
                                </nobr>
                                <div id="scheduleCronexpr" style={{position: 'absolute', border: '1px solid #d9d9d9', marginTop: 10, zIndex: 1, backgroundColor: 'white', padding: '20px 25px 0', display: 'none'}}>
                                  <div>{getLabel('508909','Cron表达式生成器')}</div>
                                  <div style={{ width: 410, height: 270}}>
                                    <WeaCron ecId={`${this && this.props && this.props.ecId || ''}_WeaCron@integrationSchedule@integrationldapschedulePluginForm${f.domkey[0]}`}
                                      cronExpression={baseSetForm.getFormParams().cronexpr}
                                      onChange={v => baseSetForm.updateFields({cronexpr: v})}
                                    />
                                  </div>
                                </div>
                              </div>
                        }
                    </WeaFormItem>),
                    colSpan: 1,
                    hide: baseSetForm.isHide(f, (keys, allParams) => {
                        let bool = false;
                        f && f.showWhere && f.showWhere.length > 0 && f.showWhere.map(w => {
                            if(!bool){
                                let show = false;
                                for(let val of w.value){
                                    if(allParams[w.domkey] == val){
                                        show = true;
                                        break;
                                    }
                                }
                                bool = !show;
                            }
                        })
                        return bool;
                    })
                });
            });
            group.push(<WeaSearchGroup col={1} needTigger={true} center={!c.leftshow} ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@integrationScheduleForm@integrationldapschedulePluginForm${index}`} showGroup={c.defaultshow} title={c.title} items={items}/>);
        });

        return group;
    }

    render() {

        return ( <div>
            {this.getFormItem()}
        </div> )
    }
}