/***
 * 
 * 明细  
 * 
***/

import React, { Component, Fragment } from 'react'
import {
	Page, Validator, Notification, FormItem,
	InputItem, Input, Bind, SelectItem, Button, Label, DatePickerItem, Section, ConfirmButton, Steps,CheckboxItem, BottomBtn
} from '@/components/Common-Library'
import Api from '@/service/api'
import CodeTableFactory from '@/components/CodeTable'
import "./index.less"
import { inject, observer } from 'mobx-react'
import { action, observable, autorun } from 'mobx'
import { Row, Col, Popconfirm, Modal ,Spin, Radio } from 'antd';
import { PtyPicker } from '@/components/Business-Library'
import { Link, withRouter } from 'react-router-dom'
import Ptspta1 from '@/views/Public/Ptspta1'
import Business from '@/views/Public/Business'

import moment from 'moment';

const { Pre, Next, StepWrapper } = Steps
const CodeTable = CodeTableFactory.getInstance()
const confirm = Modal.confirm
  
@withRouter
@inject('UserContext')
@inject('i18n')
@observer
export default class OvwpScreen extends Page {
	@observable
	formBlur = false

	constructor(props) {
		super(props)
		this.model = props.model
    this.bind = Bind.bind(this)
  }
  
	//交易描述赋值
	componentDidMount() {
		super.componentDidMount()
		this.autoDispose = autorun(() => {
			this.model.nam = `${this.model.nom1_cur}   ${this.model.nom1_amt}   ${this.model.pts3_namcn || ""}`
			//最大金额计算
      // this.model.max_amt = (this.model.nom1_amt * (1 + this.model.nomtop / 100))
      this.model.max_amt = (this.model.nom1_amt * (1 + this.model.nomtop / 100)).toFixed(2)
			if (this.model.avbby == 'USNC') {
				if(this.model.tenmaxday>366){
					this.model.tenmaxday='366';
				}
      }else{
				this.model.tenmaxday = '0'
      }
      
      //拒绝通知不为“是”时，清空拒绝理由
      if(!(this.model.rejflg=="RJCT")){
        this.model.rejrsn=""
      }
		})
  }
  
	componentWillUnmount() {
		this.autoDispose && this.autoDispose()
	}

	//按钮事件 获取业务编号
	async onGetref() {
		const rtnmsg = await Api.post(this.props.extra.inifrm + '/getref', { data: this.model })
		if (rtnmsg.retcod == SUCCESS) {
			// Notification.success({ message: 'cptopn/mtabut/getref数据提交成功!' })
			this.model.ownref = rtnmsg.data.ownref;
		}
		else {
			Notification.error({ message: 'cptopn/mtabut/getref服务端请求失败!' })
		}
		//余下逻辑处理
	}

	// 退出按钮
	onRtn = async () => {
		this.com.wrappedInstance.wrappedInstance.onRtn();
	}

	// 暂存按钮
	onPed = async () => {
		 this.com.wrappedInstance.wrappedInstance.onPed();
	}

	// 校验 按钮
	onChk = () => {
		this.com.wrappedInstance.wrappedInstance.onChk();
	}

	//提交按钮
	onSav = () => {
		this.com.wrappedInstance.wrappedInstance.onSav();
	}

  //控制开立日期选择范围
  dateDisabled=(time)=>{
    return time && time > moment();
  }

	//交易内实现
	renderPage() {
		const bind = this.bind
		const { i18n } = this.props
		const { ownbch } = this.props.extra
    const {next,pre} = this.props.steps
    const rejflgCode = [
      { label: `拒绝通知`, value: `RJCT` },
      { label: `同意通知`, value: `ACPT` },
    ]

    //将模型变量绑定到页签的bindSet对象中，才能在检核的时候被检查到变量所在页签，继而跳转到对应页签
    bind("pts2_namcn")
    bind("pts3_namcn")
    bind("pts5_namcn")

		if(this.props.flag==true){
			return <Spin style={{width:"100%", justifyContent:"center"}} spinning={this.props.flag} />
		}
		return (
			<div style={{ position: 'relative' }}>
			  <Business  wrappedComponentRef={ref => (this.com = ref)} page={this} steps={this.props.steps} extra={this.props.extra}  model={this.model} />
				{/* 第一行 */}
				<Row>
					<Col span={12}>
						<Row className="row">
							<Col span={5} offset={2}>
								{/* 业务编号 */}
								<Label i18nId={"CPTP_LT000022"} />
							</Col>
							<Col span={10}>
								<InputItem InputProps={bind("ownref")} disabled={true} />
							</Col>
							<Col span={4} offset={1}>
                <Button type="primary" onClick={() => this.onGetref()} 
                disabled={this.model.ownref}>
									<Label i18nId="CPTP_BT000084" />
								</Button>
							</Col>
						</Row>
					</Col>
					<Col span={12}>
						<Row className="row">
							<Col span={5} offset={2}>
								{/* 概要描述 */}
								<Label itext="概要描述"  />	
							</Col>
							<Col span={14}>
							  <InputItem InputProps={bind("nam")}  maxLength={40}  desp="概要描述" path="/dedgrp/rec/nam" disabled={true} />
							</Col>
						</Row>
					</Col>
				</Row>
				
				<Row>
					<Col span={12}>
						<Row className="row">
							<Col span={5} offset={2}>
								{/* 归属机构*/}
								<Label itext="归属机构"  />	
							</Col>
							<Col span={14}>
              	<SelectItem InputProps={bind("ownbch")} code={ownbch} />
							</Col>
						</Row>
					</Col>
          <Col span={12}>
						<Row className="row">
							<Col span={5} offset={2}>
								<Label itext="信用证编号"  />	
							</Col>
							<Col span={14}>
              	<InputItem InputProps={bind("lcref")} />
							</Col>
						</Row>
					</Col>
				</Row>
				{/* 第二行 */}
				<Row>
					<Col span={12}>
						<Col span={24}>
							<Row className="row">
								<Col span={5} offset={2}>
									<Label itext="客户经理"  />	
								</Col>
								<Col span={14}>
									<InputItem InputProps={bind("accusr")}  maxLength={8}  desp="客户经理" path="/dedgrp/rec/accusr" />
								</Col>
							</Row>
						</Col>
						<Col span={24}>
							<Row className="row">
								<Col span={5} offset={2}>
									<Label itext="开立日期"  />
								</Col>
								<Col span={6}>
                  <DatePickerItem InputProps={bind("opndat")}  dateDisabled={this.dateDisabled} />
								</Col>
							</Row>
						</Col>
						<Col span={24}>
							<Row className="row">
								<Col span={5} offset={2}>
									<Label itext="有效日期" />
								</Col>
								<Col span={6}>
									<DatePickerItem InputProps={bind("expdat")}  desp="有效日期" path="/dedgrp/rec/expdat"  />
								</Col>
							</Row>
						</Col>
					</Col>
					<Col span={12}>
						<Ptspta1 model={this.model} argArr={{ desc: "受益人", prefix: 'pts3', url: 'detopn/benp_ptysel', flg: 'C', datakey: '\\detp\\benp\\ptslst' }} formBlur={this.formBlur}  />
					</Col>
				</Row>

				{/* 第四行 */}
				<Row>
					<Col span={12}>
            <Row className="row">
							<Col span={5} offset={2}>
								{/* 最迟货物装运 */}
								<Label itext="最迟货物装运日期" />
							</Col>
							<Col span={6}>
								<DatePickerItem InputProps={bind("shpdat")}  desp="最迟货物装运/服务提供日期 " path="/dedgrp/rec/shpdat" />
							</Col>
						</Row>
					</Col>
					<Col span={12}>
						<Row className="row">
							<Col span={5} offset={2}>
								<Label itext="受益人账号" trn="detopn" />	
							</Col>
							<Col span={14}>
								<InputItem InputProps={bind("pts3_extact")}  maxLength={32}  desp="结算账号" path="/dedgrp/ben/pts/extact" />
							</Col>
						</Row>
					</Col>
				</Row>


				{/* 第五行 */}
				<Row>
					<Col span={12}>
						<Col span={24}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label itext="信用证金额" />
                </Col>
                <Col span={6}>
                  <SelectItem InputProps={{
                    ...bind("nom1_cur"), onChange: val => {
                      this.model.nom1_cur = val
                      this.model.liqtyp = ""
                      this.model.swftyp = ""
                    }
                  }}
                    code={CodeTable.curtxt} disabled={true} />
                </Col>
                <Col span={7} offset={1}>
                  <InputItem   InputProps={ {...bind("nom1_amt"), type:"amount",cur:this.model.nom1_cur} } />
                </Col>
              </Row>
						</Col>
            <Col span={24}>
              <Row className="row">
								<Col span={5} offset={2}>
									<Label itext="上下浮动比例+/-% "  />
								</Col>
								<Col span={6}>
									<InputItem InputProps={bind("nomtop")}  maxLength={8}  desp="上浮" path="\dedgrp\rec\nomtop" />
								</Col>
								<Col span={1} offset={1}>
									<Label itext="/"  />
								</Col>
								<Col span={6}>
									<InputItem InputProps={bind("nomton")}  maxLength={8}  desp="下浮" path="\dedgrp\rec\nomton" />
								</Col>
							</Row>
            </Col>
						<Col span={24}>
							<Row className="row">
								<Col span={5} offset={2}>
									<Label itext="信用证最大金额" />
								</Col>
								<Col span={6}>
									<SelectItem InputProps={{
										...bind("max_cur"), onChange: val => {
											this.model.max_cur = val
											this.model.liqtyp = ""
											this.model.swftyp = ""
										}
									}}
										code={CodeTable.curtxt} disabled={true} />
								</Col>
								<Col span={7} offset={1}>
									<InputItem   InputProps={ {...bind("max_amt"), type:"amount", cur:this.model.max_cur} }  disabled={true}/>
								</Col>
							</Row>
						</Col>
					</Col>
					
					<Col span={12}>
						<Ptspta1 model={this.model} argArr={{ desc: "开证行", prefix: 'pts5', url: 'detopn/issp_ptysel', flg: 'B', datakey: '\\detp\\issp\\ptslst' }} formBlur={this.formBlur}  />
					</Col>
				</Row>


				{/* 第六行 */}
				<Row>
					<Col span={12}>
            <Col span={24}>
              <Row className="row">
								<Col span={5} offset={2}>
									<Label itext="开证类型"  />
								</Col>
								<Col span={14}>
                  <SelectItem InputProps={{...bind("isstyp"),
                    onAfterChange:(val)=>{
                      if(val=='OWIS' || !val){
												this.model.pts1_extkey="";
												this.model.pts1_namcn="";
												this.model.pts1_adrcn="";
											}
                    }}} 
                    code={CodeTable.isstyp} desp="开证类型" path="\dedgrp\rec\isstyp"
                  />
								</Col>
							</Row>
            </Col>
						<Col span={24}>
							<Row className="row">
								<Col span={5} offset={2}>
									<Label itext="贸易方式"  />
								</Col>
								<Col span={14}>
									<SelectItem InputProps={bind("trdtyp")} code={CodeTable.mytype}  desp="贸易方式" path="/dedgrp/rec/trdtyp"/>
								</Col>
							</Row> 
						</Col>
						<Col span={24}>
							<Row className="row">
								<Col span={5} offset={2}>
									<Label itext="信用证兑付方式"  />
								</Col>
								<Col span={6}>
									<SelectItem InputProps={bind("avbby")} code={CodeTable.avbby} />
								</Col>
								<Col span={5} offset={1}>
									<InputItem InputProps={bind("tenmaxday")} disabled={this.model.avbby=='SIGT'} />
								</Col>
								<Col span={4} offset={1}>
									<Label itext="天" />
								</Col>
							</Row>
						</Col>
					</Col>
					<Col span={12}>
						<Ptspta1 model={this.model} argArr={{ desc: "申请人", prefix: 'pts2', url: 'detopn/aplp_ptysel', flg: 'C', datakey: '\\detp\\aplp\\ptslst' }} isOpen={true} formBlur={this.formBlur}  />
					</Col>
				</Row>

				{/* 第七行 */}
				<Row>
					<Col span={12}>
						<Row className="row">
							<Col span={5} offset={2}>
								<Label itext="远期付款指示"  />
							</Col>
							<Col span={14}>
								<InputItem InputProps={bind("defdet")}  maxLength={60}  desp="远期付款指示" path="/dedgrp/rec/defdet" />
							</Col>
						</Row>
					</Col>
					<Col span={12}>
						<Row className="row">
							<Col span={5} offset={2}>
								<Label itext="申请人账号" trn="detopn" />
							</Col>
							<Col span={14}>
								<InputItem InputProps={bind("pts2_extact")}  maxLength={32}  desp="结算账号" path="/dedgrp/apl/pts/extact" />
							</Col>
						</Row>
					</Col>
				</Row>

        <Row>
          <Col span={12}>
            <Row className="row">
              <Col span={5} offset={2}>
                <Label itext="是否拒绝通知" trn="detopn" />
              </Col>
              <Col span={14}>
                <SelectItem InputProps={bind("rejflg")} code={rejflgCode} />
              </Col>
            </Row>
          </Col>
          <Col span={12}>
            <Row className="row">
              <Col span={5} offset={2}>
                <Label itext="拒绝理由" trn="detopn" />
              </Col>
              <Col span={14}>
              <InputItem InputProps={bind("rejrsn")}  maxLength={40} disabled={!(this.model.rejflg=="RJCT")} />
              </Col>
            </Row>
          </Col>
        </Row>

        {/* 底部按钮 */}
        <BottomBtn _this={this} />

			</div >
		)
	}
}
