import React from 'react';
import $ from 'jquery/src/ajax';
import {
	Modal,
	message,
	Spin,
	Upload,
	Input,
	Button,
    Form,
    Table
} from 'antd';
import {getOrderType,splitUrl,getNewOrderType,getProcessStatus,getApproval,getLiquidationStatus} 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/order/getOrderCommodity',
            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,
                             commodityType:thisdata.commodityType,
                             commodityMode:thisdata.commodityMode,
                             commodityQuantity:thisdata.commodityQuantity,
                             commodityPrice:thisdata.commodityPrice,
                             discountPrice:thisdata.discountPrice,
                             commodityFirstPayment:thisdata.commodityFirstPayment,
                             createTimes:thisdata.createTimes,
                             discountFirstPayment:thisdata.discountFirstPayment,
                             remarks:thisdata.remarks,
                             commodityId:thisdata.commodityId,
                        });
                    };
                   }
                this.setState({
                    dataSource: theArr,
                    pagination: false,
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            visible: false,
            loading: false,
            auditStatus: 0,
            textFileList: [],
            videoFileList: [],
            pictureUrl: [],
            pictureUrlMin:[],
            columns: [
				{
                    title: '业务项目名称',
                    dataIndex: 'commodityName',
                    key: 'commodityName'
                }, {
                    title: '项目类别',
                    dataIndex: 'commodityType',
                    key: 'commodityType',
                    render:(text)=>{
                    	return (getOrderType(text))
                    }
                },{
                    title: '项目数量',
                    dataIndex: 'commodityQuantity',
                    key: 'commodityQuantity'
                }, {
                    title: '服务市价(万元)',
                    dataIndex: 'commodityPrice',
                    key: 'commodityPrice'
                }, {
                    title: '实签价格(万元)',
                    dataIndex: 'discountPrice',
                    key: 'discountPrice'
                }, {
                    title: '下单时间',
                    dataIndex: 'createTimes',
                    key: 'createTimes'
                }, {
                    title: '项目说明',
                    dataIndex: 'remarks',
                    key: 'remarks',
					render:(text)=>{
						return (text&&text.length>8?text.substr(0,8)+'…':text)
					}
                }
			],
        };
    },
    loadData(record){
    	this.state.orderList=[];
    	$.ajax({
            method: "get",
			dataType: "json",
			crossDomain: false,
			url: globalConfig.context + '/api/admin/order/getServiceOrderDetail',
            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));  
    },
    getPictureUrl(e) {
        this.setState({ pictureUrl: e });
    },
	cancelFun(){
		this.props.closeDesc();
    },
    //审核通过/拒绝
    updateFun(index){
        this.setState({
            loading:true
        })
        $.ajax({
            method: "post",
            dataType: "JSON",
            crossDomain: false,
            url: globalConfig.context +  '/api/admin/achievement/audit',
            data: {
                id: this.state.id,
                auditResult:index,
                auditInfo:this.state.refuseValue
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('操作成功！');
                this.setState({
                    loading: false,
                });
                this.props.handOk()
            } else {
                message.warning(data.error[0].message);
            };
        }.bind(this));
    },
    //审核拒绝
    refuse(){
        this.setState({
            refuseValue:'',
            refuseVisible:true
        })
    },
    refuseOk(){
        if(!this.state.refuseValue){
            message.warning('请填写拒绝原因。')
            return;
        }
        this.setState({
            refuseVisible:false
        })
        this.updateFun(0)
    },
    refuseCancel(){
        this.setState({
            refuseValue:'',
            refuseVisible:false
        })
    },
    componentWillMount() {
        if (this.props.data.orderNo) {
            this.loadData(this.props.data.id);
            this.loadXmu(this.props.data.data);
        } 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.id);
                this.loadXmu(nextProps.data);
            } else {
				this.state.data = {};
                this.state.pictureUrl = [];
                this.state.pictureUrlMin=[];
            };
            this.props.form.resetFields();
        };
    },
    render() {
        const theData = this.state.orderList || {};
        const { getFieldDecorator } = this.props.form;
        const FormItem = Form.Item
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
        };
        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.releaseDate?(new Date(theData.releaseDate)).toLocaleString():''}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="客户名称">
							<span>{theData.name}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="流转状态">
							<span>{getProcessStatus(theData.processStatus)}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="结算状态">
							<span>{getLiquidationStatus(theData.liquidationStatus)}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="已收款">
							<span>{theData.settlementAmount}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="订单类型">
							<span>{getNewOrderType(theData.orderType)}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="是否特批">
							<span>{getApproval(theData.approval)}</span>
						</FormItem>
					</div>
                    <div className="clearfix">
                        <FormItem labelCol={{ span: 3 }} 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.orderNo}</span>
						</FormItem>           
                    </div>
                    <div className="clearfix">
						<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="订单留言">
							<span>{theData.orderRemarks}</span>
						</FormItem>
					</div>
                    <div className="clearfix">
						<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="收款节点">
							<span>{theData.introduction}</span>
						</FormItem>
					</div>
                    <div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="订单负责人">
							<span>{theData.introduction}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="订单负责人电话">
							<span>{theData.introduction}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="财务负责人">
							<span>{theData.introduction}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="财务负责人电话">
							<span>{theData.introduction}</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;




