import React from 'react';
import { Switch, Icon, Form, Button, Input, Spin, Table, message, Select, Modal, InputNumber, Upload } from 'antd';
import './techProduct.less';
import { 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 FinanceDescFrom = Form.create()(React.createClass({
    loadData(uid, year) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/financeDetail",
            data: {
                uid: uid != undefined ? uid : this.props.data.uid || this.props.uid,
                year: year != undefined ? year : this.props.data.year
            }
        }).done((data) => {
            if (!data.data) {
                if (data.error && data.error.length) {
                    message.warning(data.error[0].message);
                };
                return;
            };
            this.setState({
                managementCost: data.data.managementCost,
                researchCost: data.data.researchCost,
                grossProfit: data.data.grossProfit,
                operatingProfit: data.data.operatingProfit
            });
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            loading: false,
            targetKeys: [],
            selectedKeys: [],
            yearOption: [],
        };
    },
    componentWillMount() {
        let d = new Date();
        let _me = this;
        d = d.getFullYear();
        for (let i = d; i > d - 20; i--) {
            _me.state.yearOption.push(
                {i}
            )
        };
        if (this.props.data.uid) {
            this.loadData();
        };
    },
    handleSubmit(e) {
        e.preventDefault();
        if (!this.state.year && !this.props.data.year) {
            message.warning("请选择一个年份!")
            return;
        };
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.props.spinState(true);
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/api/admin/disposeFinance",
                    data: {
                        id: this.props.data.id,
                        uid: this.props.uid,
                        mainBusinessIncome: values.mainBusinessIncome,
                        managementCost: values.managementCost,
                        researchCost: values.researchCost,
                        operatingProfit: values.operatingProfit,
                        variousTax: values.variousTax,
                        currentAsset: values.currentAsset,
                        netFixedAsset: values.netFixedAsset,
                        totalAsset: values.totalAsset,
                        netAsset: values.netAsset,
                        grossProfit: values.grossProfit,
                        netProfit: values.netProfit,
                        salesRevenue: values.salesRevenue,
                        year: this.state.year || this.props.data.year,
                        financeUrl: this.state.financeUrl
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功!');
                        this.props.clickOk();
                        this.props.spinState(false);
                        this.props.form.resetFields();
                    } else {
                        message.warning(data.error[0].message);
                        this.props.spinState(false);
                    }
                }.bind(this));
            }
        });
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            if (nextProps.data.uid) {
                this.loadData(nextProps.data.uid, nextProps.data.year);
            };
            this.props.form.resetFields();
            this.state.fileList = [];
            this.state.managementCost = undefined;
            this.state.researchCost = undefined;
            this.state.grossProfit = undefined;
            this.state.operatingProfit = undefined;
        };
    },
    checkResearchCost(rule, value, callback) {
        const form = this.props.form;
        if (value > form.getFieldValue('managementCost')) {
            callback('管理费用小于研发费用!');
        } else {
            callback();
        }
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        const theData = this.props.data;
        const formItemLayout = {
            labelCol: { span: 8 },
            wrapperCol: { span: 16 },
        };
        const _me = this;
        return (
            
        );
    },
}));
const FinanceDesc = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false
        };
    },
    componentWillReceiveProps(nextProps) {
        this.state.visible = nextProps.showDesc
    },
    showModal() {
        this.setState({
            visible: true,
        });
    },
    handleOk() {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false, true);
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    spinChange(e) {
        this.setState({
            loading: e
        });
    },
    render() {
        return (
            
                
                    
                        
                    
                
            
        );
    },
});
const Finance = React.createClass({
    loadData(pageNo) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/finance",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                uid: this.props.data.uid
            }
        }).done((data) => {
            if (data.error.length || !data.data || !data.data.list) {
                message.warning(data.error[0].message);
                return;
            };
            this.state.data = [];
            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,
                    mainBusinessIncome: thisdata.mainBusinessIncome,
                    managementCost: thisdata.managementCost,
                    researchCost: thisdata.researchCost,
                    operatingProfit: thisdata.operatingProfit,
                    variousTax: thisdata.variousTax,
                    currentAsset: thisdata.currentAsset,
                    netFixedAsset: thisdata.netFixedAsset,
                    totalAsset: thisdata.totalAsset,
                    netAsset: thisdata.netAsset,
                    grossProfit: thisdata.grossProfit,
                    netProfit: thisdata.netProfit,
                    salesRevenue: thisdata.salesRevenue,
                    year: thisdata.year,
                    financeUrl: thisdata.financeUrl,
                    financeDownloadFileName: thisdata.financeDownloadFileName,
                    finance: [thisdata.id, thisdata.financeDownloadFileName]
                });
            };
            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 {
            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: 'year',
                    key: 'year'
                }, {
                    title: '主营业务收入(万元)',
                    dataIndex: 'mainBusinessIncome',
                    key: 'mainBusinessIncome'
                }, {
                    title: '管理费用(万元)',
                    dataIndex: 'managementCost',
                    key: 'managementCost'
                }, {
                    title: '营业利润(万元)',
                    dataIndex: 'operatingProfit',
                    key: 'operatingProfit',
                }, {
                    title: '支付的各项税费(万元)',
                    dataIndex: 'variousTax',
                    key: 'variousTax',
                }, {
                    title: '资产总额(万元)',
                    dataIndex: 'totalAsset',
                    key: 'totalAsset',
                }, {
                    title: '所有者权益(净资产)(万元)',
                    dataIndex: 'netAsset',
                    key: 'netAsset',
                }, {
                    title: '相关附件',
                    dataIndex: 'finance',
                    key: 'finance',
                    render: (text) => {
                        return {text[1]}
                    }
                }
            ],
            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/deleteFinance",
            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.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.finance = 1 : this.state.proportion.finance = 0;
                            saveProportion(this.state.proportion.id, this.props.data.uid, 'finance', this.state.proportion.finance);
                            this.setState({ proportion: this.state.proportion });
                        }} />
                
                
                
                
             
        );
    }
});
export default Finance;