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, getTechField } from '../../../../DicTechFieldList.js';
import { 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';
if (!Array.prototype.includes) {
    Object.defineProperty(Array.prototype, 'includes', {
        value: function (searchElement, fromIndex) {
            // 1. Let O be ? ToObject(this value).
            if (this == null) {
                throw new TypeError('"this" is null or not defined');
            }
            var o = Object(this);
            // 2. Let len be ? ToLength(? Get(O, "length")).
            var len = o.length >>> 0;
            // 3. If len is 0, return false.
            if (len === 0) {
                return false;
            }
            // 4. Let n be ? ToInteger(fromIndex).
            //    (If fromIndex is undefined, this step produces the value 0.)
            var n = fromIndex | 0;
            // 5. If n ≥ 0, then
            //  a. Let k be n.
            // 6. Else n < 0,
            //  a. Let k be len + n.
            //  b. If k < 0, let k be 0.
            var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
            // 7. Repeat, while k < len
            while (k < len) {
                // a. Let elementK be the result of ? Get(O, ! ToString(k)).
                // b. If SameValueZero(searchElement, elementK) is true, return true.
                // c. Increase k by 1.
                // NOTE: === provides the correct "SameValueZero" comparison needed here.
                if (o[k] === searchElement) {
                    return true;
                }
                k++;
            }
            // 8. Return false
            return false;
        }
    });
}
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: [data.data.technicalField1, data.data.technicalField2, data.data.technicalField3]
            });
        }.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 = [];
            this.state.proofUrl = undefined;
            if (nextProps.data.id) {
                this.loadData(nextProps.data.id);
            } else {
                this.state.data = {};
                this.state.targetKeys = [];
                this.state.techField = undefined;
            };
        };
    },
    intellectualChange(nextTargetKeys, direction, moveKeys) {
        debugger
        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 (