import {
    inject,
    observer
} from 'mobx-react';
import {
    WeaDialog,
    WeaFormItem,
    WeaLocaleProvider,
} from 'ecCom';
import {
    WeaSwitch
} from 'comsMobx';
import DirtyComponent from './DirtyComponent.js';
import {
    CompositeForm,
} from '../../public/sweet-form.js';
import {
    Loading,
} from '../../public/learn.js';
import {
    getDialogButtons
} from '../../util/save-time-util.js';
import RestTime from './RestTime';
import Form from './Form'
const getLabel = WeaLocaleProvider.getLabel;

@inject('hrmOvertimeRule')
@observer
class Dialog extends React.Component {
    constructor(props) {
        super(props);
    }

    handleCancel = () => {
        this.props.hrmOvertimeRule.closeDialog();
    }

    getBtnConfigDatas = () => {
        const {
            overtimeRuleFormStore: { saveForm, formStore: { loading } }
        } = this.props.hrmOvertimeRule;

        const btnDatas = [{
            name: getLabel('505564', '保存'),
            disabled: loading,
            onBtnClick: saveForm
        }];

        return btnDatas
    }

    getDynamicFields = () => {
        //【工作日加班】表单动态域的配置项
        const workTime = [{
            passiveKey: 'startTime2', //加班起算时间
            activeKey: 'computingMode2', //加班方式计算
            showValue: '1,2,3'
        }, {
            passiveKey: 'minimumLen2', //最小加班时长
            activeKey: 'computingMode2', //加班方式计算
            showValue: '1,2,3'
        }, {
            passiveKey: 'computingMode2', //加班方式计算
            activeKey: 'overtimeEnable2', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'startTime2', //加班起算时间
            activeKey: 'overtimeEnable2', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'minimumLen2', //最小加班时长
            activeKey: 'overtimeEnable2', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'paidLeaveEnable2', //转为调休余额
            activeKey: 'overtimeEnable2', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'lenOfOvertime2', //加班时长
            activeKey: 'paidLeaveEnable2', //转为调休余额
            showValue: '1'
        }, {
            passiveKey: 'lenOfLeave2', //调休时长
            activeKey: 'paidLeaveEnable2', //转为调休余额
            showValue: '1'
        }];

        //【休息日加班】表单动态域的配置项
        const restTime = [{
            passiveKey: 'minimumLen3', //最小加班时长
            activeKey: 'computingMode3', //加班方式计算
            showValue: '1,2,3'
        }, {
            passiveKey: 'computingMode3', //加班方式计算
            activeKey: 'overtimeEnable3', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'minimumLen3', //最小加班时长
            activeKey: 'overtimeEnable3', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'paidLeaveEnable3', //转为调休余额
            activeKey: 'overtimeEnable3', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'hasRestTime3', //排除休息时间
            activeKey: 'overtimeEnable3', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'lenOfOvertime3', //加班时长
            activeKey: 'paidLeaveEnable3', //转为调休余额
            showValue: '1'
        }, {
            passiveKey: 'lenOfLeave3', //调休时长
            activeKey: 'paidLeaveEnable3', //转为调休余额
            showValue: '1'
        }];

        //【节假日加班】表单动态域的配置项
        const holidayTime = [{
            passiveKey: 'minimumLen1', //最小加班时长
            activeKey: 'computingMode1', //加班方式计算
            showValue: '1,2,3'
        }, {
            passiveKey: 'computingMode1', //加班方式计算
            activeKey: 'overtimeEnable1', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'minimumLen1', //最小加班时长
            activeKey: 'overtimeEnable1', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'paidLeaveEnable1', //转为调休余额
            activeKey: 'overtimeEnable1', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'hasRestTime1', //排除休息时间
            activeKey: 'overtimeEnable1', //允许加班
            showValue: '1'
        }, {
            passiveKey: 'lenOfOvertime1', //加班时长
            activeKey: 'paidLeaveEnable1', //转为调休余额
            showValue: '1'
        }, {
            passiveKey: 'lenOfLeave1', //调休时长
            activeKey: 'paidLeaveEnable1', //转为调休余额
            showValue: '1'
        }];

        const dynamicFields = [...workTime, ...restTime, ...holidayTime];

        return dynamicFields;
    }

    getDirtyKeys = () => {
        return ['startTime2', 'minimumLen2', 'minimumLen3', 'minimumLen1'];
    }

    handleFormChange = (form, cb) => {
        const key = Object.keys(cb)[0];
        //关闭【允许加班】开关后，重置【加班方式计算】，包括：radio select、加班起算时间和最小加班时长
        //勾掉【转为调休余额】时，重置调休比例。
        if (cb[key].value === '0') {
            [2, 3, 1].map(v => {
                if (key === `overtimeEnable${v}`) {
                    ['computingMode', 'paidLeaveEnable', 'minimumLen', 'lenOfOvertime', 'lenOfLeave', 'hasRestTime','maxmumLen'].map((key, index) => {
                        let value;
                        if (index === 0) {
                            value = '1';
                        } else if ([1, 5].includes(index)) {
                            value = '0';
                        } else if (index === 2) {
                            value = 30
                        } else {
                            value = 1
                        }
                        form.updateFields({
                            [`${key}${v}`]: {
                                value
                            }
                        });
                    });
                    if (v === 2) {
                        form.updateFields({
                            [`startTime${v}`]: {
                                value: 30
                            }
                        });
                    }
                    if ([3, 1].includes(v)) {
                        this.props.hrmOvertimeRule.resetRestTime(v === 3 ? 'rstTimes' : 'vacationTimes');
                    }
                }
                if (key === `paidLeaveEnable${v}`) {
                    ['lenOfOvertime', 'lenOfLeave'].map((key, index) => {
                        form.updateFields({
                            [`${key}${v}`]: {
                                value: 1
                            }
                        });
                    });
                }
                if (key === `hasRestTime${v}`) {
                    this.props.hrmOvertimeRule.resetRestTime(v === 3 ? 'rstTimes' : 'vacationTimes');
                }

            });
        }

    }

    getLongitudinalSiblingComponents = (params) => {
        const {
            form,
            items
        } = params;

        if (items.length === 0) return;

        const all = {};
        //items为二维数组
        items.map((oneDimensionalArray, index) => {
            const innerArray = [];

            const number = (index === 0) ? 2 : (index === 1) ? 3 : 1;

            let lenOfOvertime, lenOfLeave;

            ['lenOfOvertime', 'lenOfLeave'].map((key, index) => {
                const value = form.getFormParams()[`${key}${number}`];
                if (index === 0) {
                    lenOfOvertime = value
                } else {
                    lenOfLeave = value;
                }
            });

            oneDimensionalArray.map((item, index) => {
                const propertys = {
                    tipPosition: 'bottom',
                    error: form.getError(item),
                    colon: false,
                };
                if (index === 0) {
                    Object.assign(propertys, {
                        label: getLabel('505598', "加班时间转调休时间的比例"),
                        labelCol: {
                            span: 17
                        },
                        wrapperCol: {
                            span: 7
                        },
                        style: {
                            width: '47%'
                        },
                        errorStyle: {
                            width: 140,
                            marginLeft: -90
                        }
                    })


                } else {
                    Object.assign(propertys, {
                        label: '',
                        labelCol: {
                            span: 2
                        },
                        wrapperCol: {
                            span: 22
                        },
                        style: {
                            width: '48%'
                        }
                    });

                }

                Object.assign(propertys.style, {
                    float: 'left'
                })

                innerArray.push(
                    <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@kahqzi@${index}`}
						{...propertys}
          			>
          				<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@nm24zh@${index}`} fieldConfig={item} form={form} formParams={form.getFormParams()} />
          				{
          					(index === 0)&&<span style={{position:'absolute',top: 5,left: '109%'}}>:</span>
          				}
          				{
          					(index === 1) && <span style={{marginLeft: 12, color:'#999'}}>{`${getLabel('6151',"加班")}${lenOfOvertime ? lenOfOvertime : ''}${getLabel('391',"小时")}, ${getLabel('505599',"转调休")}${lenOfLeave ? lenOfLeave : ''}${getLabel('391',"小时")}`}</span>
          				}
          			</WeaFormItem>
                )
            });

            Object.assign(all, {
                [`paidLeaveEnable${number}`]: <div className='hrm-overtime-input' style={{paddingTop: 10}}>{innerArray}</div>
            });
        });

        const longitudinalSiblingComponents = {};

        ['paidLeaveEnable2', 'paidLeaveEnable3', 'paidLeaveEnable1'].map(field => {
            if (form.getFormParams()[field] === '1') {
                Object.assign(longitudinalSiblingComponents, {
                    [field]: all[field]
                });
            }
        });

        return longitudinalSiblingComponents;
    }

    getLongitudinalSecondComponent = (conditions) => {
        const longitudinalSecondComponents = [];

        let innerArray = [];

        conditions.map(c => {
            c.items.map(item => {
                const {
                    domkey
                } = item;

                if (['lenOfOvertime2', 'lenOfLeave2', 'lenOfOvertime3', 'lenOfLeave3', 'lenOfOvertime1', 'lenOfLeave1'].includes(domkey[0])) {
                    innerArray.push(item);

                    if (innerArray.length === 2) {
                        longitudinalSecondComponents.push(innerArray);
                        innerArray = [];
                    }
                }
            });
        });

        return longitudinalSecondComponents;
    }

    getRenderBlacklist = () => {
        return ['lenOfOvertime2', 'lenOfLeave2', 'lenOfOvertime3', 'lenOfLeave3', 'lenOfOvertime1', 'lenOfLeave1'];
    }

    getCustomizedComponent = (form) => {
        const {
            hasRestTime3,
            hasRestTime1,
        } = form.getFormParams();
        const {
            restTime
        } = this.props.hrmOvertimeRule, {
            rstTimes,
            vacationTimes
        } = restTime

        const customizations = {
            hasRestTime3: hasRestTime3 === '1' && <RestTime ecId={`${this && this.props && this.props.ecId || ''}_RestTime@ct2h1i`} times={rstTimes} type='rstTimes'/>,
            hasRestTime1: hasRestTime1 === '1' && <RestTime ecId={`${this && this.props && this.props.ecId || ''}_RestTime@9vbcbi`} times={vacationTimes} type='vacationTimes'/>
        }

        return customizations;
    }

    render() {
        const {
            dialog,
            dialogStaticProps,
            style,
            formElements,
            overtimeRuleFormStore
        } = this.props.hrmOvertimeRule, {
            title,
            visible,
        } = dialog, {
            form,
            date,
            loading,
            conditions,
        } = formElements;

        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@1ee0gz`}
          		title={title}
          		visible={visible}
          		style={style}
          		buttons={getDialogButtons({datas:this.getBtnConfigDatas()})}
          		onCancel={this.handleCancel}
	            {...dialogStaticProps}
        	>
        		{
        		 visible && (
                    <div className='overtimeForm'>
                     <Form ecId={`${this && this.props && this.props.ecId || ''}_Form@yi8ny4`} store={overtimeRuleFormStore}/>
                     </div>
                )
        		}
        	</WeaDialog>
        )
    }
}

export default Dialog