import React from 'react';
import $ from 'jquery/src/ajax';
import {
	Modal,
	message,
	Spin,
	Upload,
	Input,
	Button,
    Form,
    Table
} from 'antd';
import {getOrderType,getjiedian,splitUrl,getNewOrderType,getProcessStatus,getApproval,getLiquidationStatus,getProjectStatus} from '@/tools';

const FormItem = Form.Item;

const OrderDetail = Form.create()(React.createClass({
    loadXmu(record) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/admin/newOrder/getOrderTask',
            data: {
            	orderNo:record?record.orderNo:this.props.data.orderNo
            },
            success: function (data) {
                let theArr = [];
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                } else {
                    for (let i = 0; i < data.data.length; i++) {
                        let thisdata = data.data[i];
                        theArr.push({
                             key: i,
                             id:thisdata.id,
                             orderNo:thisdata.orderNo,
                             commodityName:thisdata.commodityName,
                             commodityPrice:thisdata.commodityPrice,
                             commodityQuantity:thisdata.commodityQuantity,
                             taskStatus:thisdata.taskStatus,
                             taskComment:thisdata.taskComment,
                             main:thisdata.main,
                             contacts:thisdata.contacts,
                             contactsMobile:thisdata.contactsMobile,
                             cname:thisdata.cname,
                        });
                    };
                   }
                this.setState({
                    dataSource: theArr,
                    pagination: false,
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            visible: false,
            loading: false,
            jsDate:[],
            auditStatus: 0,
            textFileList: [],
            videoFileList: [],
            pictureUrl: [],
            pictureUrlMin:[],
            columns: [
				{
                    title: '业务项目名称',
                    dataIndex: 'commodityName',
                    key: 'commodityName',
                    render:text=>{
                        return text&&text.length>6?<span title={text}>{text.substr(0,8)}...</span>:text
                    }
                }, {
                    title: '项目类别',
                    dataIndex: 'cname',
                    key: 'cname'
                },  {
                    title: '项目数量(个)',
                    dataIndex: 'commodityQuantity',
                    key: 'commodityQuantity'
                },{
                    title: '服务市价(万元)',
                    dataIndex: 'commodityPrice',
                    key: 'commodityPrice'
                }, {
                    title: '项目状态',
                    dataIndex: 'taskStatus',
                    key: 'taskStatus',
                    render:text=>{
                        return getProjectStatus(text);
                    }
                }, {
                    title: '是否主要项目',
                    dataIndex: 'main',
                    key: 'main',
                    render:text=>{
                        return text?'是':'否'
                    }
                },{
                    title: '项目说明',
                    dataIndex: 'taskComment',
                    key: 'taskComment',
					render:text=>{
                        return text&&text.length>8?<span title={text}>{text.substr(0,8)}...</span>:text
                    }
                }
			],
        };
    },
    loadData(record){
    	this.state.orderList=[];
    	$.ajax({
            method: "get",
			dataType: "json",
			crossDomain: false,
			url: globalConfig.context + '/api/admin/newOrder/getOrderNewDetail',
            data: {
              orderNo:record?record.orderNo:this.props.data.orderNo
            },
            success: function (data) {
                let thisData = data.data;                
                if (!thisData) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    thisData = {};
                };
                this.setState({
					id:thisData.id,
					orderList:thisData,
					approval:thisData.approval==0?thisData.approval.toString():thisData.approval,
					orderRemarks:thisData.orderRemarks,
                    orgCodeUrl: thisData.contractPictureUrl ? splitUrl(thisData.contractPictureUrl, ',', globalConfig.avatarHost + '/upload') : [],
               		orderNo:thisData.orderNo,//订单编号
               		buyerId:thisData.buyerId,
                });
            }.bind(this),
       }).always(function () {
            this.setState({            	
                loading: false
            });
        }.bind(this));  
    },
    jdDate(record){
    	$.ajax({
            method: "get",
			dataType: "json",
			crossDomain: false,
			url: globalConfig.context + '/api/admin/newOrder/selectOrderDun',
            data: {
              orderNo:record?record.orderNo:this.props.data.orderNo
            },
            success: function (data) {
                let thisData = data.data;                
                if (!thisData.length) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    thisData = {};
                }else{
                   this.setState({
                       jsDate:thisData
                   })
                };
                
            }.bind(this),
       }).always(function () {
            this.setState({            	
                loading: false
            });
        }.bind(this));  
    },
    getPictureUrl(e) {
        this.setState({ pictureUrl: e });
    },
    componentWillMount() {
        if (this.props.data.orderNo) {
            this.loadData();
            this.loadXmu();
            this.jdDate();
        } else {
            this.state.data = {};
        };
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.state.textFileList = [];
            this.state.videoFileList = [];
            if (nextProps.data.orderNo) {
                this.loadData(nextProps.data);
                this.loadXmu(nextProps.data);
                this.jdDate(nextProps.data);
            } else {
				this.state.data = {};
                this.state.pictureUrl = [];
                this.state.pictureUrlMin=[];
            };
            this.props.form.resetFields();
        };
    },
    render() {
        const theData = this.state.orderList || {};
        const FormItem = Form.Item
        const formItemLayout = {
            labelCol: { span: 10 },
            wrapperCol: { span: 12 },
        };
        const jsDate = this.state.jsDate || [];
        return (
			<div className='login'>
			<Form layout="horizontal" id="demand-form">
				<Spin spinning={this.state.loading}>
                    <div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="订单编号">
							<span>{theData.orderNo}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="签单时间">
							<span>{theData.signDate}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="客户名称">
							<span>{theData.userName}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="流转状态">
							<span>{getProcessStatus(theData.processStatus)}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="订单类型">
							<span>{getNewOrderType(theData.orderType)}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="结算状态">
							<span>{getLiquidationStatus(theData.liquidationStatus)}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="签单总金额(万元)">
							<span>{theData.totalAmount}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="首付款(万元)">
							<span>{theData.firstAmount}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="已收款(万元)">
							<span>{theData.settlementAmount}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="是否特批">
							<span>{getApproval(theData.approval)}</span>
						</FormItem>
                        {this.props.data.orderStatus=='6'&&<FormItem className="half-item" {...formItemLayout} label="已退(万元)">
							<span>{this.props.data.refundAmount}</span>
						</FormItem>}
					</div>
                    <div className="clearfix">
                        <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 18 }} label="合同扫描件">
                            <Upload className="demandDetailShow-upload"
                                listType="picture-card"
                                fileList={this.state.orgCodeUrl}                               
                                onPreview={(file) => {
                                    this.setState({
                                        previewImage: file.url || file.thumbUrl,
                                        previewVisible: true,
                                    });
                                }} >
                            </Upload>
                            <Modal maskClosable={false} footer={null}
                                visible={this.state.previewVisible}
                                onCancel={() => { this.setState({ previewVisible: false }) }}>
                                <img alt="" style={{ width: '100%' }} src={this.state.previewImage || ''} />
                            </Modal>
                        </FormItem>
                    </div> 
                    <div className="clearfix">
                        <FormItem className="half-item" {...formItemLayout} label="合同编号">
							<span>{theData.contractNo}</span>
						</FormItem>           
                    </div>
                    <div className="clearfix">
						<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 18 }} label="订单留言">
							<span>{theData.orderRemarks}</span>
						</FormItem>
					</div>
                    <div className="clearfix">
						<FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 18 }} label="收款节点">
							{
                                jsDate.map(item=>{
                                    return <p>{getjiedian(item.dunSubject)} -- {item.money} 万元</p>
                                })
                            }
						</FormItem>
					</div>
                    <div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="订单负责人">
							<span>{theData.salesmanName}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="订单负责人电话">
							<span>{theData.salesmanMobile}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="财务负责人">
							<span>{theData.financeName}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="财务负责人电话">
							<span>{theData.financeMobile}</span>
						</FormItem>
					</div>
                    <div className="clearfix">
                        <p style={{fontSize:18,marginLeft:20}}>项目业务</p>
                        <div className="patent-table">
                            <Spin spinning={this.state.loading}>
                                <Table columns={this.state.columns}
                                    dataSource={this.state.dataSource}
                                    pagination={this.state.pagination}
                                    onRowClick={this.tableRowClick} 
                                    />
                            </Spin>
                        </div>    
                    </div>
				</Spin>
			</Form>   
		</div>
        )
    }
}));
export default OrderDetail;




