import React,{Component} from 'react';
import {Button, DatePicker, Input, message, Modal, Radio} from "antd";
import {
    highTechColumns,//高新
    highTechColumnsVIP,//高新会员
    science,//科技
    patent,//专利
    currency,//软著和通用
} from './type';
import moment from "moment";
import $ from "jquery";

class NowProjectStatus extends Component{
    constructor(props) {
        super(props);
        this.state={
            projectStatus: parseInt(props.projectStatus),
            list:
                props.cSort === 3 ? science :
                props.cSort === 6 ? highTechColumnsVIP :
                props.projectType === 0 || props.projectType === 2 || props.projectType === 3 || props.projectType === 4 || props.projectType === 6?
                    currency:
                props.projectType === 1 ?
                    patent:
                props.projectType === 5 ?
                    highTechColumns :
                    [],//projectType  0 正常 1专利 2软著 3审计 4双软 5高新 6商标   cSort: 3 科技项目  6: 会员
            dataInfor:Object.assign({},props.projectTypeJSON),
            loading: false,
        }
    }

    updateProjectStatus(){
        let arr = this.state.list.filter(v=>v.id === this.state.projectStatus);
        let dataInforLength  = this.state.dataInfor[this.state.projectStatus] ? Object.keys(this.state.dataInfor[this.state.projectStatus]).length : 0;
        if(this.state.dataInfor[this.state.projectStatus] && this.state.dataInfor[this.state.projectStatus]['title']){
            dataInforLength -= 1;
        }
        if(arr.length !== 0 && arr[0]['valueType'] && dataInforLength !== arr[0]['valueType'].length){
            message.warning('有内容没有填写,请补充完整!')
            return false;
        }
        this.setState({
            loading: true,
        })
        let data = Object.assign({},this.state.dataInfor);
        Object.keys(data).map(v=>{
            if(Object.keys(data[v]).length === 0){
                delete data[v];
            }
        })
        if(data[this.state.projectStatus]){
            data[this.state.projectStatus]['title'] =arr[0]['label'] || arr[0]['name'];
        }
        let obj = {
            tid:this.props.tid,
            projectStatus: this.state.projectStatus,
            timeRecord: JSON.stringify(data)
        }
        if(arr[0]['valueType']){
            //证书编号
            let type = arr[0]['valueType'].filter(v=>v==='certificateNumber');
            if(type.length > 0){
                obj.certificateNumber = this.state.dataInfor[this.state.projectStatus]['certificateNumber']
            }
            //立项金额
            let type1 = arr[0]['valueType'].filter(v=>v==='setUpAmount');
            if(type1.length > 0){
                obj.setUpAmount = this.state.dataInfor[this.state.projectStatus]['setUpAmount']
            }
        }
        $.ajax({
            url: globalConfig.context + '/api/admin/orderProject/updateProjectStatus',
            method: 'post',
            data: obj,
        }).done(
            function (data) {
                this.setState({
                    loading: false,
                })
                if (!data.error.length) {
                    message.success('保存成功!');
                    this.props.onPreservation(this.state.projectStatus);
                } else {
                    message.warning(data.error[0].message)
                }
            }.bind(this)
        )
    }

    componentDidMount() {
        /*旧数据处理
        * endDate           完成时间      对应已提交时间     4
        * acceptDate        受理时间      对应已推荐时间     6
        * publicityDate     公示时间      对应已拟公示时间   8
        * licenceDate       发证时间      对应已下证时间     10
        * setUpAmount       立项金额                      18
        * */
        const {endDate,acceptDate,publicityDate,licenceDate,setUpAmount} = this.props.oldData;
        if((!this.props.projectTypeJSON[4]) && endDate){
            this.state.dataInfor[4]={title:'完成时间'};
            this.state.dataInfor[4]['date'] = endDate;
        }
        if((!this.props.projectTypeJSON[6]) && acceptDate){
            this.state.dataInfor[6]={title:'受理时间'};
            this.state.dataInfor[6]['date'] = acceptDate;
        }
        if((!this.props.projectTypeJSON[8]) && publicityDate){
            this.state.dataInfor[8]={title:'公示时间'};
            this.state.dataInfor[8]['date'] = publicityDate;
        }
        if((!this.props.projectTypeJSON[10]) && licenceDate){
            this.state.dataInfor[10]={title:'发证时间'};
            this.state.dataInfor[10]['date'] = licenceDate;
        }
        if((!this.props.projectTypeJSON[18]) && setUpAmount){
            this.state.dataInfor[18]={title:'立项金额'};
            this.state.dataInfor[18]['setUpAmount'] = setUpAmount;
        }
        this.setState({
            dataInfor: this.state.dataInfor,
        })

    }

    render() {
        return(
            <Modal
                className="customeDetails"
                footer=""
                title=""
                width="700px"
                visible={this.props.visible}
                onOk={()=>{
                    this.props.onCancel();
                }}
                onCancel={()=>{
                    this.props.onCancel();
                }}
            >
                <div>
                    <div style={{
                        paddingBottom: '15px',
                        fontSize:'18px',
                        fontWeight:"bolder",
                    }}>
                        项目状态:
                    </div>
                    <div>
                        <Radio.Group
                            style={{
                                dispaly:'flex',
                                flexRow:'column'
                            }}
                            value={parseInt(this.state.projectStatus)}
                            onChange={(e) => {
                                if(!this.state.dataInfor[e.target.value]){
                                    this.state.dataInfor[e.target.value] = {};
                                }
                                this.state.dataInfor[this.state.projectStatus] = this.props.projectTypeJSON[this.state.projectStatus] || {};
                                this.setState({
                                    projectStatus: e.target.value,
                                    dataInfor:this.state.dataInfor,
                                });
                            }}
                        >
                            {
                                this.state.list.map((v,k)=>(
                                    <div key={k} style={{paddingBottom:'15px'}}>
                                        <Radio value={v.id}>{v.name}</Radio>
                                        {
                                            (v.valueType || []).map(d=>(
                                                parseInt(this.state.projectStatus) !== parseInt(v.id) ?
                                                    <span style={{marginRight:'15px'}}>{this.state.dataInfor[v.id] && this.state.dataInfor[v.id][d]}</span> :
                                                d === 'date' ?
                                                <DatePicker
                                                    style={{ marginRight: '5px',width:'120px' }}
                                                    showTime
                                                    format="YYYY-MM-DD"
                                                    onOk={() => {}}
                                                    value={
                                                        this.state.dataInfor[v.id][d] ? moment(this.state.dataInfor[v.id][d]) : null
                                                    }
                                                    onChange={(data, dataString) => {
                                                        this.state.dataInfor[v.id][d] = dataString;
                                                        this.setState({
                                                            dataInfor:this.state.dataInfor,
                                                        })
                                                    }}
                                                /> :
                                                d === 'setUpAmount' || d === 'certificateNumber'? <Input
                                                    placeholder={v.placeholder}
                                                    value={this.state.dataInfor[v.id][d]}
                                                    style={{ width: '120px',marginRight: '5px', }}
                                                    onChange={(e) => {
                                                        this.state.dataInfor[v.id][d] = e.target.value;
                                                        this.setState({
                                                            dataInfor:this.state.dataInfor,
                                                        })
                                                    }}
                                                /> : null
                                            ))
                                        }
                                        <span style={{color:'#f00',marginLeft:'15px'}}>{v.explain}</span>
                                    </div>
                                ))
                            }
                        </Radio.Group>
                        <div style={{
                            display:'flex',
                            justifyContent:'center',
                            paddingTop:'20px',
                            paddingBottom: '20px',
                        }}>
                            <Button
                                className="cancel"
                                type="ghost"
                                onClick={()=>{
                                    this.updateProjectStatus();
                                }}
                            >
                                保存
                            </Button>
                        </div>
                        <div style={{textAlign:'center',color:'#f00'}}>注:请根据实际项目情况填写</div>
                    </div>
                </div>
            </Modal>
        )
    }
}

export default NowProjectStatus;