import React, { Component, Fragment } from 'react'
import {
	Page, Validator, Notification, FormItem,
	InputItem, Input, Bind, SelectItem, Button, Label, DatePickerItem, Section, Table, TextAreaItem,ConfirmButton,Steps
} 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 } from 'mobx'
import { Row, Col, Popconfirm, Modal } from 'antd';
import {PtyPicker} from '@/components/Business-Library'
import {Link,withRouter} from 'react-router-dom'


const {Pre,Next,StepWrapper} = Steps
const CodeTable = CodeTableFactory.getInstance()
const confirm = Modal.confirm

@withRouter
@inject('UserContext')
@inject('i18n')
@observer
export default class BusinessScreen extends Component {
	@observable
	formBlur = false
	
	constructor(props) {
		super(props)
		this.model = props.model
		this.bind = Bind.bind(this)
	}
	
	async componentWillMount() {	
	}
	
	componentWillUnmount() {
		//this.model.closeValidate()//关闭校验
	}
	// 退出按钮
	onRtn=async ()=>{
		this.formBlur = true  //已触发表单提交处理 
		//this.props.history.push("/business/home");
		const rtnmsg = await Api.post(this.props.extra.inifrm + '/exi',{})
		this.props.history.go(-1);
	}

	onRtn1=async ()=>{
		this.formBlur = true  //已触发表单提交处理 
		this.props.history.go(-1);
	}
	onRtnPed=async ()=>{
		let rtnflg='x'
    let  sptinr  = this.props.extra.sptinr
    if (sptinr) {
			rtnmsg = await Api.post(this.props.extra.inifrm + '/ped', { params: { sptinr,rtnflg }, data: this.props.model })
		} else {
			rtnmsg = await Api.post(this.props.extra.inifrm + '/ped', { params: { rtnflg }, data: this.props.model })
    }
    	if (rtnmsg.retcod == SUCCESS) {
		//	Notification.success({ message: '数据暂存成功!' })
		Notification.success({ message:rtnmsg.retmsg})
			this.props.model.spt_inr=rtnmsg.data.spt_inr
			this.onRtn()//暂存并退出
    	}
    	else {
            Notification.error({ message:rtnmsg.retmsg})
    	}
        
	}


	
	// 暂存按钮
	 onPed= async ()=>{
		this.formBlur = true  //已触发表单提交处理
		let { sptinr } = this.props.extra
		if (sptinr) {
			rtnmsg = await Api.post(this.props.extra.inifrm + '/ped', { params: { sptinr }, data: this.model })
		} else {
			rtnmsg = await Api.post(this.props.extra.inifrm + '/ped', { data: this.model })
    }
		if (rtnmsg.retcod == SUCCESS) {
	  //Notification.success({ message: '数据暂存成功!' })
	  Notification.success({ message:rtnmsg.retmsg})
	  this.props.model.spt_inr=rtnmsg.data.spt_inr
      // 页面跳转到home界面！！
     // this.props.history.push("/business/home");
		}
		else {
      Notification.error({ message: rtnmsg.retmsg })
		}
		//余下逻辑处理
	}
	
	// 校验 按钮
	 onChk=()=>{
		this.props.page && (this.props.page.formBlur = true ) //已触发表单提交处理
		Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
			if (errors)
			{
        // console.log(errors)
        // console.log(this.props)
				this.props.steps && this.props.steps.findFirstItem(errors)
				return
			}
			const rtnmsg = await Api.post(this.props.extra.inifrm+'/chk', { data: this.model })
			if (rtnmsg.retcod == SUCCESS) {
        Notification.success({ message: '数据检核成功!' })
				// return true;
			}
			else {
				Notification.error({ message: '服务端请求失败!' })
			}
			//余下逻辑处理
		})
	}
	
	//  提交 按钮
	onSav= ()=>{
		this.props.page && (this.props.page.formBlur = true )  //已触发表单提交处理
		Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
			if (errors)
			{
				console.log(errors)
				this.props.steps && this.props.steps.findFirstItem(errors)
				return
      }
      let sptinr=this.props.extra.sptinr
      let oldinr=this.props.extra.oldinr  //此参数用于修改、退回等操作
			if(sptinr){
				rtnmsg = await Api.post(this.props.extra.inifrm+'/sav', {display:this.model.getDisplay(), params: {sptinr,oldinr},data: this.model })
			}else{
				rtnmsg = await Api.post(this.props.extra.inifrm+'/sav', {display:this.model.getDisplay(),params: {oldinr}, data: this.model })
      }
			if (rtnmsg.retcod == SUCCESS) {
				this.props.history.push("/business/home");
				return true;
			}
			else {
        Notification.error({ message: '交易提交失败，请重试!' })
			}
			//余下逻辑处理
    })
	}
	
	// 重算 按钮（费用明细，财务明细）
	onRecal=async ()=>{
		this.formBlur = true  //已触发表单提交处理
		const rtnmsg = await Api.post(this.props.extra.inifrm+'/setmod_ast', { data: this.model })
		if (rtnmsg.retcod == SUCCESS) {
      //费用   账务
			this.model.fegmod_feslst = rtnmsg.data.fegmod_feslst;
      this.model.glgmod_glslst = rtnmsg.data.glgmod_glslst;
      
      //会计分录
      this.model.glemod_glelst = rtnmsg.data.glemod_glelst;
      
      if(rtnmsg.data.setmod_docamt != 0){
        this.model.setmod_docamt = rtnmsg.data.setmod_docamt;
        this.model.setmod_doccur = rtnmsg.data.setmod_doccur;
      }
			this.model.setmod_feemac = rtnmsg.data.setmod_feemac;
      this.model.setmod_setmac = rtnmsg.data.setmod_setmac;
      
      //利息   计提摊销
      this.model.acrmod_acrlst = rtnmsg.data.acrmod_acrlst;
      this.model.acrmod_acelst = rtnmsg.data.acrmod_acelst;
		}
		else {
			Notification.error({ message: '服务端请求失败!' })
		}
	}

	 onRecal1=async()=> {
		this.formBlur = true  //已触发表单提交处理
		const rtnmsg = await Api.post(this.props.extra.inifrm + '/setmod_recal', { data: this.model })
		if (rtnmsg.retcod == SUCCESS) {
			this.model.fegmod_feslst = rtnmsg.data.fegmod_feslst;
			this.model.glgmod_glslst = rtnmsg.data.glgmod_glslst;
      this.model.glemod_glelst = rtnmsg.data.glemod_glelst;
      if(rtnmsg.data.setmod_docamt != 0){
        this.model.setmod_docamt = rtnmsg.data.setmod_docamt;
        this.model.setmod_doccur = rtnmsg.data.setmod_doccur;
      }
			this.model.setmod_feemac = rtnmsg.data.setmod_feemac;
			this.model.setmod_setmac = rtnmsg.data.setmod_setmac;
		}
		else {
			Notification.error({ message: '服务端请求失败!' })
		}
		//余下逻辑处理
	}

	
	onRecal2=async()=> {
		this.formBlur = true  //已触发表单提交处理
		const rtnmsg = await Api.post(this.props.extra.inifrm + '/liaall_astlia', { data: this.model })
		if (rtnmsg.retcod == SUCCESS) {
			this.model.liaccv_liaccvg = rtnmsg.data.liaccv_liaccvg;
      this.model.liaall_liaallg = rtnmsg.data.liaall_liaallg;
      //保证金字段赋值
      this.model.liaccv_concur = rtnmsg.data.liaccv_concur
      this.model.liaccv_newamt = rtnmsg.data.liaccv_newamt
      this.model.liaccv_chgamt = rtnmsg.data.liaccv_chgamt
      this.model.liaccv_pctresamt = rtnmsg.data.liaccv_pctresamt
      this.model.liaccv_totcovamt = rtnmsg.data.liaccv_totcovamt
      this.model.liaccv_relcshpct = rtnmsg.data.liaccv_relcshpct
			
		}
		else {
			Notification.error({ message: '服务端请求失败!' })
		}
		//余下逻辑处理
	}
	
	 // 加载会计分录	
	onRebook=async ()=>{ 
	     this.onRecal1()
		/*this.formBlur = true  //已触发表单提交处理 
		const rtnmsg = await Api.post(this.props.extra.inifrm+'/glemod_rebook', { data: this.model })
		if (rtnmsg.retcod == SUCCESS) {
			this.model.glemod_glelst = rtnmsg.data.glemod_glelst;
			this.model.glgmod_glslst = rtnmsg.data.glgmod_glslst;
		}
		else {
			Notification.error({ message: '服务端请求失败!' })
		}
		*/
	}
	
    // 加载面函
	onReldoc=async ()=>{ 
		this.formBlur = true  //已触发表单提交处理
		const rtnmsg = await Api.post(this.props.extra.inifrm+'/trndoc_reldoc', { data: this.model })
		if (rtnmsg.retcod == SUCCESS) {
			// Notification.success({ message: '数据提交成功!' })
			this.model.docmod_doclst = rtnmsg.data.docmod_doclst;
		}
		else {
			Notification.error({ message: '服务端请求失败!' })
		}
  }
  
  //信贷页面加载方法
  onLimmod_astlim= async()=>{
    this.formBlur=true  //已触发表单提交处理
    const rtnmsg = await Api.post(this.props.extra.inifrm+'/limmod_astlim',{data:this.model})
    if(rtnmsg.retcod == SUCCESS)
    {
        Object.assign(this.model, rtnmsg.data)

        //将后台发送的码表返给业务组件
        let codetable = []
        rtnmsg.codetable.limmod_dir1 && codetable.push(rtnmsg.codetable.limmod_dir1)
        rtnmsg.codetable.limmod_inv1 && codetable.push(rtnmsg.codetable.limmod_inv1)
        return codetable
    }
    else
    {
      Notification.error({message:'服务端请求失败!'})
    }
      //余下逻辑处理
  }
	
	render(){
		const bind = this.bind
		return null
	}
}
