/*
* 排班
* */

import React, { Component } from 'react';
import { Input, Form, Select, Modal, Button } from 'antd';
import '../style/index.less';
import { Util } from 'tools/index';
import moment from 'moment';

const Option = Select.Option;
const FormItem = Form.Item;
//全局申明时间
const now = new Date(); //当前日期
const nowDayOfWeek = now.getDay(); //今天本周的第几天
const nowDay = now.getDate(); //当前日
const nowMonth = now.getMonth(); //当前月
const nowYear = now.getFullYear(); //当前年

class SaveSchedulingModal extends Component {

    constructor(props) {
        super(props);
        this.state = {
        }
    }

    componentWillReceiveProps(nextProp) {
        if (this.props.isShow === false && nextProp.isShow === true) {

        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    closeModal() {
        let { onHide } = this.props;
        onHide && onHide();
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.props.form.resetFields();
    }

    //获取当前年的当前周
    getWeekOfYear() {
        var today = new Date();
        var firstDay = new Date(today.getFullYear(), 0, 1);
        var dayOfWeek = firstDay.getDay();
        var spendDay = 1;
        if (dayOfWeek != 0) {
            spendDay = 7 - dayOfWeek + 1;
        }
        firstDay = new Date(today.getFullYear(), 0, 1 + spendDay);
        var d = Math.ceil((today.valueOf() - firstDay.valueOf()) / 86400000);
        var result = Math.ceil(d / 7);
        return result + 1;
    };

    changeWeek(e) {
        this.setState({
            newDateStr: e
        })
    }

    changeBeforeWeek(e) {
        this.setState({
            oldDateStr: e
        })
    }
    //获得上周的开始日期
    getLastWeekStartDate(day) {
        var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - day);
        return this.formatDate(weekStartDate);
    }
    //获得上周的结束日期
    getLastWeekEndDate(day) {
        var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - day);
        return this.formatDate(weekEndDate);
    }


    //获得下周开始时间
    getNextWeekStartDate(day) {
        var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + day);
        return this.formatDate(weekStartDate);
    }

    //获得下周结束时间
    getNextWeekEndDate(day) {
        var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek) + day);
        return this.formatDate(weekEndDate);
    }

    //格式化日期：yyyy-MM-dd
    formatDate(date) {
        var myyear = date.getFullYear();
        var mymonth = date.getMonth() + 1;
        var myweekday = date.getDate();
        if (mymonth < 10) {
            mymonth = "0" + mymonth;
        }
        if (myweekday < 10) {
            myweekday = "0" + myweekday;
        }
        return (myyear + "-" + mymonth + "-" + myweekday);
    }

    //提交
    onSubmit() {
        let { validateFields } = this.props.form;
        validateFields({ force: true }, (err, params) => {
            let { onSubmit } = this.props;

            onSubmit && onSubmit(this.state.oldDateStr, this.state.newDateStr);
        });
    }

    render() {
        let { getFieldDecorator, getFieldValue } = this.props.form;
        let formItemLayout = {
            labelCol: { span: 10 },
            wrapperCol: { span: 19 }
        };

        //获取年份、月、日
        var date = new Date();
        var year = date.getFullYear();
        var month = moment(date).format('MM-DD');

        //已排班
        let lastListOption = []
        this.props.lastList && this.props.lastList.map((item, index) => {
            lastListOption.push(<Option key={index} value={item.dateStr}>{item.descripts}</Option>)
        })

        //需要排班
        let nextListOption = []
        this.props.nextList && this.props.nextList.map((item, index) => {
            nextListOption.push(<Option key={index} value={item.dateStr}>{item.descripts}</Option>)
        })

        return <Modal className={this.props.identity == 'save' ? 'time-solt-style' : 'timeSoltModal'}
            title={this.props.identity === 'save' ? '保存排班' : '复制排班'}
            maskClosable={false}
            visible={this.props.isShow}
            onCancel={this.closeModal.bind(this)}
            footer={this.props.identity == 'save' ? <Button className='time-save-button' onClick={this.closeModal.bind(this)}>确定</Button> :
                <Button className='time-save-button' onClick={this.onSubmit.bind(this)}>确定</Button>}>
            <div>
                <div className="modal-body">
                    {this.props.identity === 'save' ?
                        <div>
                            <span>
                                <h4>排班保存成功，生成排班表后排班生效</h4>
                                <h4>执行生成排班表操作之前，可编辑修改排班</h4>
                            </span>
                        </div>
                        : <Form className="card-form-item">
                            <FormItem {...formItemLayout} label="已进行排班的排班周期(来源)">
                                {getFieldDecorator('hospital', {
                                    initialValue: this.props.hospital,
                                })(
                                    <Select placeholder="请选择排班周期" showSearch={true}
                                        onChange={this.changeBeforeWeek.bind(this)}
                                        getPopupContainer={triggerNode => triggerNode.parentNode}>
                                        {lastListOption}
                                    </Select>
                                )}
                            </FormItem>

                            <FormItem {...formItemLayout} label="选择需要复制的排班周期(目标)">
                                {getFieldDecorator('name', {
                                    initialValue: this.props.name,
                                })(
                                    <div>
                                        <Select placeholder="请选择排班周期"
                                            onChange={this.changeWeek.bind(this)}
                                            getPopupContainer={triggerNode => triggerNode.parentNode}>
                                            {nextListOption}
                                        </Select>
                                        <div style={{ marginTop: '10px' }}>说明：已生成排班表周期不能复制排班</div>
                                    </div>
                                )}
                            </FormItem>
                        </Form>}
                </div>
            </div>
        </Modal>
    }
}

export default Form.create()(SaveSchedulingModal);