import React from 'react';
import { Icon, Button, Upload, Input, Spin, Table, message, Select, Modal, DatePicker, Cascader, Transfer, InputNumber } from 'antd';
import { technicalSourceList } from '../../../../dataDic.js';
import { techFieldList } from '../../../../DicTechFieldList.js';
import { getTechField, getTechnicalSource, beforeUploadFile, newDownloadFile } from '../../../../tools.js';
import moment from 'moment';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
const ActivityDesc = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            targetKeys: [],
            selectedKeys: []
        };
    },
    handleOk() {
        this.setState({
            loading: true,
        });
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/activity",
            data: {
                id: this.props.data.id,
                uid: this.props.uid,
                activityName: this.state.activityName,
                activityNumber: this.state.activityNumber,
                startDateFormattedDate: this.state.startDateFormattedDate,
                endDateFormattedDate: this.state.endDateFormattedDate,
                technicalField1: this.state.techField[0],
                technicalField2: this.state.techField[1],
                technicalField3: this.state.techField[2],
                technicalSource: this.state.technicalSource,
                intellectualPropertyNumber: this.state.targetKeys.join(","),
                budget: this.state.budget,
                firstYearExpenditure: this.state.firstYearExpenditure,
                secondYearExpenditure: this.state.secondYearExpenditure,
                thirdYearExpenditure: this.state.thirdYearExpenditure,
                implement: this.state.implement,
                technologyInnovation: this.state.technologyInnovation,
                achievement: this.state.achievement,
                participant: this.state.participant,
                proofUrl: this.state.proofUrl
            },
            success: function (data) {
                if (data.data) {
                    this.setState({
                        id: this.props.data.id,
                        activityName: this.state.activityName,
                        activityNumber: this.state.activityNumber,
                        startDateFormattedDate: this.state.startDateFormattedDate,
                        endDateFormattedDate: this.state.endDateFormattedDate,
                        techField: this.state.techField,
                        technicalSource: this.state.technicalSource,
                        intellectualPropertyNumber: this.state.targetKeys,
                        budget: this.state.budget,
                        firstYearExpenditure: this.state.firstYearExpenditure,
                        secondYearExpenditure: this.state.secondYearExpenditure,
                        thirdYearExpenditure: this.state.thirdYearExpenditure,
                        implement: this.state.implement,
                        technologyInnovation: this.state.technologyInnovation,
                        achievement: this.state.achievement,
                        projectMode: this.state.projectMode
                    });
                } else if (data.error.length && data.error[0].message) {
                    message.warning(data.error[0].message);
                }
            }.bind(this),
        }).done(function (data) {
            if (!data.error.length) {
                message.success('保存成功!');
                this.setState({
                    loading: false,
                    visible: false
                });
                this.props.closeDesc(false, true);
            } else {
                message.warning(data.error[0].message);
                this.setState({
                    loading: false
                });
            };
        }.bind(this));
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    componentWillReceiveProps(nextProps) {
        if (!this.state.showDesc && nextProps.showDesc) {
            this.state.activityName = nextProps.data.activityName;
            this.state.activityNumber = nextProps.data.activityNumber;
            this.state.startDateFormattedDate = nextProps.data.startDateFormattedDate;
            this.state.endDateFormattedDate = nextProps.data.endDateFormattedDate;
            this.state.techField = nextProps.data.techField;
            this.state.technicalSource = nextProps.data.technicalSource;
            this.state.targetKeys = nextProps.data.intellectualPropertyNumber || [];
            this.state.budget = nextProps.data.budget;
            this.state.firstYearExpenditure = nextProps.data.firstYearExpenditure;
            this.state.secondYearExpenditure = nextProps.data.secondYearExpenditure;
            this.state.thirdYearExpenditure = nextProps.data.thirdYearExpenditure;
            this.state.implement = nextProps.data.implement;
            this.state.technologyInnovation = nextProps.data.technologyInnovation;
            this.state.achievement = nextProps.data.achievement;
            this.state.projectMode = nextProps.data.projectMode ? String(nextProps.data.projectMode) : undefined;
            this.state.proofUrl = nextProps.data.proofUrl;
            this.state.proofDownloadFileName = nextProps.data.proofDownloadFileName;
            this.state.mockData = nextProps.data.mockData;
        };
        this.state.visible = nextProps.showDesc;
    },
    intellectualChange(nextTargetKeys, direction, moveKeys) {
        this.setState({ targetKeys: nextTargetKeys });
    },
    intellectualSelectChange(sourceSelectedKeys, targetSelectedKeys) {
        this.setState({ selectedKeys: [...sourceSelectedKeys, ...targetSelectedKeys] });
    },
    render() {
        if (this.props.data) {
            return (
                
                    保存,
                            
                        ]}
                        className="admin-desc-content">
                        
                            
                            
                            
                                开始日期:
                                 { this.setState({ startDateFormattedDate: dataString }); }} />
                             
                            
                                结束日期:
                                 { this.setState({ endDateFormattedDate: dataString }); }} />
                             
                         
                        
                            技术领域:
                             { this.setState({ techField: value }); console.log(value) }}
                                placeholder="选择技术领域"
                                showSearch
                            />,
                         
                        
                            技术来源:
                            
                         
                        
                            参与人员:
                             { this.setState({ participant: e }); }} />
                         
                        
                            知识产权编号:
                             `${item.name}-${item.key}`}
                            />
                         
                        
                            研发经费总预算(万元):
                             { this.setState({ budget: e }); }} />
                         
                        
                            研发经费近三年总支出(万元):
                            
                                总计(万元):
                                
                                (自动计算)
                            
                            
                                其中(第一年):
                                 { this.setState({ firstYearExpenditure: e || 0 }); }} />
                            
                            
                                其中(第二年):
                                 { this.setState({ secondYearExpenditure: e || 0 }); }} />
                            
                            
                                其中(第三年):
                                 { this.setState({ thirdYearExpenditure: e || 0 }); }} />
                            
                         
                        
                        
                        
                             {
                                    if (info.file.status !== 'uploading') {
                                        console.log(info.file, info.fileList);
                                    }
                                    if (info.file.status === 'done') {
                                        if (!info.file.response.error.length) {
                                            message.success(`${info.file.name} 文件上传成功!`);
                                        } else {
                                            message.warning(info.file.response.error[0].message);
                                            return;
                                        };
                                        this.state.proofUrl = info.file.response.data;
                                    } else if (info.file.status === 'error') {
                                        message.error(`${info.file.name} 文件上传失败。`);
                                    }
                                }}
                            >
                                
                            
                            {this.state.proofUrl ? {this.state.proofDownloadFileName} : 未上传!}
                         
                    
                 
            );
        } else {
            return 
        }
    },
});
const Activity = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.when($.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/activityList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                uid: this.props.data.uid,
                activityNumber: this.state.activityNumber,
                activityName: this.state.activityName,
                startDateFormattedDate: this.state.startDateFormattedDate,
                endDateFormattedDate: this.state.endDateFormattedDate
            }
        }), $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/intellectualList",
            data: {
                uid: this.props.data.uid,
                pageNo: pageNo || 1,
                pageSize: 99,
            }
        })).done((data1, data2) => {
            let data = data1[0];
            let intellectualList = data2[0];
            if (data.error.length || !data.data || !data.data.list) {
                message.warning(data.error[0].message);
                return;
            };
            if (intellectualList.error.length || !intellectualList.data || !intellectualList.data.list) {
                message.warning(intellectualList.error[0].message);
                return;
            };
            let theArr = [];
            for (let i = 0; i < intellectualList.data.list.length; i++) {
                theArr.push({
                    key: intellectualList.data.list[i].intellectualPropertyNumber,
                    name: intellectualList.data.list[i].intellectualPropertyName
                });
            };
            this.state.mockData = theArr;
            for (let i = 0; i < data.data.list.length; i++) {
                let thisdata = data.data.list[i];
                this.state.data.push({
                    key: i,
                    id: thisdata.id,
                    uid: thisdata.uid,
                    activityNumber: thisdata.activityNumber,
                    activityName: thisdata.activityName,
                    startDate: thisdata.startDate,
                    endDate: thisdata.endDate,
                    mockData: this.state.mockData,
                    techField: [Number(thisdata.technicalField1), Number(thisdata.technicalField2), Number(thisdata.technicalField3)],
                    technicalField: getTechField(thisdata.technicalField1, thisdata.technicalField2, thisdata.technicalField3),
                    technicalField1: thisdata.technicalField1,
                    technicalField2: thisdata.technicalField2,
                    technicalField3: thisdata.technicalField3,
                    technicalSource: thisdata.technicalSource ? String(thisdata.technicalSource) : undefined,
                    intellectualPropertyNumber: thisdata.intellectualPropertyNumber ? thisdata.intellectualPropertyNumber.split(",") : [],
                    budget: thisdata.budget,
                    firstYearExpenditure: thisdata.firstYearExpenditure,
                    secondYearExpenditure: thisdata.secondYearExpenditure,
                    thirdYearExpenditure: thisdata.thirdYearExpenditure,
                    implement: thisdata.implement,
                    technologyInnovation: thisdata.technologyInnovation,
                    achievement: thisdata.achievement,
                    internalLaborCost: thisdata.internalLaborCost,
                    internalDirectCost: thisdata.internalDirectCost,
                    internalDepreciationCost: thisdata.internalDepreciationCost,
                    internalAmortizationCost: thisdata.internalAmortizationCost,
                    internalDesignCost: thisdata.internalDesignCost,
                    internalEquipmentCost: thisdata.internalEquipmentCost,
                    internalOtherCost: thisdata.internalOtherCost,
                    externalTotalCost: thisdata.externalTotalCost,
                    externalAbroadCost: thisdata.externalAbroadCost,
                    enterpriseFiller: thisdata.enterpriseFiller,
                    signDate: thisdata.signDate,
                    sortNumber: thisdata.sortNumber,
                    startDateFormattedDate: thisdata.startDateFormattedDate,
                    endDateFormattedDate: thisdata.endDateFormattedDate,
                    projectMode: thisdata.projectMode,
                    proofUrl: thisdata.proofUrl,
                    proofDownloadFileName: thisdata.proofDownloadFileName
                });
            };
            this.state.pagination.current = data.data.pageNo;
            this.state.pagination.total = data.data.totalCount;
            this.setState({
                dataSource: this.state.data,
                pagination: this.state.pagination
            });
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            mockData: [],
            selectedRowKeys: [],
            selectedRows: [],
            loading: false,
            pagination: {
                defaultCurrent: 1,
                defaultPageSize: 10,
                showQuickJumper: true,
                pageSize: 10,
                onChange: function (page) {
                    this.loadData(page);
                }.bind(this),
                showTotal: function (total) {
                    return '共' + total + '条数据';
                }
            },
            columns: [
                {
                    title: '研发活动编号',
                    dataIndex: 'activityNumber',
                    key: 'activityNumber'
                }, {
                    title: '研发活动名称',
                    dataIndex: 'activityName',
                    key: 'activityName'
                }, {
                    title: '技术领域',
                    dataIndex: 'technicalField',
                    key: 'technicalField'
                }, {
                    title: '技术来源',
                    dataIndex: 'technicalSource',
                    key: 'technicalSource',
                    render: (text) => { return getTechnicalSource(text) }
                }, {
                    title: '知识产权编号',
                    dataIndex: 'intellectualPropertyNumber',
                    key: 'intellectualPropertyNumber'
                }, {
                    title: '开始时间',
                    dataIndex: 'startDateFormattedDate',
                    key: 'startDateFormattedDate'
                }, {
                    title: '结束时间',
                    dataIndex: 'endDateFormattedDate',
                    key: 'endDateFormattedDate'
                }
            ],
            dataSource: []
        };
    },
    componentWillMount() {
        this.loadData();
    },
    tableRowClick(record, index) {
        this.state.RowData = record;
        this.setState({
            showDesc: true
        });
    },
    delectRow() {
        let deletedIds = [];
        for (let idx = 0; idx < this.state.selectedRows.length; idx++) {
            let rowItem = this.state.selectedRows[idx];
            if (rowItem.id) {
                deletedIds.push(rowItem.id)
            }
        }
        this.setState({
            selectedRowKeys: [],
            loading: deletedIds.length > 0
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/deleteActivity",
            data: {
                ids: deletedIds
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('保存成功!');
                this.setState({
                    loading: false,
                });
            } else {
                message.warning(data.error[0].message);
            };
            this.loadData();
        }.bind(this));
    },
    closeDesc(e, s) {
        this.state.showDesc = e;
        if (s) {
            this.loadData();
        };
    },
    search() {
        this.loadData();
    },
    reset() {
        this.state.activityName = undefined;
        this.state.activityNumber = undefined;
        this.state.startDateFormattedDate = undefined;
        this.state.endDateFormattedDate = undefined;
        this.loadData();
    },
    render() {
        const rowSelection = {
            selectedRowKeys: this.state.selectedRowKeys,
            onChange: (selectedRowKeys, selectedRows) => {
                this.setState({
                    selectedRows: selectedRows,
                    selectedRowKeys: selectedRowKeys
                });
            }
        };
        const hasSelected = this.state.selectedRowKeys.length > 0;
        return (
            
        );
    }
});
export default Activity;