import React from 'react';
import { Icon, Button, Upload, Input, Spin, Table, message, Select, Modal, DatePicker, Cascader, Transfer, InputNumber, Switch, Form } from 'antd';
import { technicalSourceList } from '../../../../dataDic.js';
import { techFieldList } from '../../../../DicTechFieldList.js';
import { getTechField, getTechnicalSource, beforeUploadFile, newDownloadFile, getProportion, saveProportion, getPreview } from '../../../../tools.js';
import moment from 'moment';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
const ActivityForm = Form.create()(React.createClass({
    getInitialState() {
        return {
            loading: false,
            targetKeys: [],
            selectedKeys: []
        };
    },
    loadData(id) {
        this.setState({ loading: true });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/activityDetail",
            data: {
                id: id || this.props.data.id
            }
        }).done(function (data) {
            if (!data.data) {
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                };
            };
            this.setState({
                data: data.data,
                targetKeys: data.data.intellectualPropertyNumber ? data.data.intellectualPropertyNumber.split(",") : [],
                techField: [Number(data.data.technicalField1), data.data.technicalField2 ? Number(data.data.technicalField2) : null, data.data.technicalField3 ? Number(data.data.technicalField3) : null]
            });
        }.bind(this)).always(function (data) {
            this.setState({ loading: false });
        }.bind(this));
    },
    handleSubmit(e) {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                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: values.activityName,
                        activityNumber: values.activityNumber,
                        projectMode: values.projectMode,
                        startDateFormattedDate: values.startDate ? values.startDate.format("YYYY-MM-DD") : null,
                        endDateFormattedDate: values.endDate ? values.endDate.format("YYYY-MM-DD") : null,
                        technicalField1: values.techField[0],
                        technicalField2: values.techField[1],
                        technicalField3: values.techField[2],
                        technicalSource: values.technicalSource,
                        intellectualPropertyNumber: this.state.targetKeys.join(","),
                        budget: values.budget,
                        implement: values.implement,
                        technologyInnovation: values.technologyInnovation,
                        achievement: values.achievement,
                        proofUrl: this.state.proofUrl
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功!');
                        this.props.closeModal(true);
                    } else {
                        message.warning(data.error[0].message);
                    };
                    this.setState({ loading: false });
                }.bind(this));
            }
        });
    },
    componentWillMount() {
        if (this.props.data.id) {
            this.loadData();
        } else {
            this.state.data = {};
            this.state.targetKeys = [];
            this.state.techField = undefined;
        };
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.state.mockData = nextProps.data.mockData;
            this.props.form.resetFields();
            this.state.fileList = [];
            if (nextProps.data.id) {
                this.loadData(nextProps.data.id);
            } else {
                this.state.data = {};
                this.state.targetKeys = [];
                this.state.techField = undefined;
            };
        };
    },
    intellectualChange(nextTargetKeys, direction, moveKeys) {
        this.setState({ targetKeys: nextTargetKeys });
    },
    intellectualSelectChange(sourceSelectedKeys, targetSelectedKeys) {
        this.setState({ selectedKeys: [...sourceSelectedKeys, ...targetSelectedKeys] });
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        const theData = this.state.data;
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
        };
        if (theData) {
            return (
                
                    
                
            )
        } else {
            return (
)
        };
    }
}));
const ActivityDesc = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false
        };
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        if (e) {
            this.props.closeDesc(true);
        };
    },
    componentWillReceiveProps(nextProps) {
        this.state.visible = nextProps.showDesc;
    },
    render() {
        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(",") : [],
                    intellectualPropertyNumberText: thisdata.intellectualPropertyNumber,
                    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));
        let _me = this;
        getProportion(this.props.data.uid, function (data) { _me.setState({ proportion: data }); });
    },
    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: 'intellectualPropertyNumberText',
                    key: 'intellectualPropertyNumberText'
                }, {
                    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) {
        this.state.showDesc = false;
        if (e) {
            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 (
            
                
                    企业研究开发活动情况表
                     是否已完成:
                     {
                            e ? this.state.proportion.activity = 1 : this.state.proportion.activity = 0;
                            saveProportion(this.state.proportion.id, this.props.data.uid, 'activity', this.state.proportion.activity);
                            this.setState({ proportion: this.state.proportion });
                        }} />
                
                
                
                
             
        );
    }
});
export default Activity;