import React, { Component, Fragment } from 'react'
import {
	Page, Validator, Notification, FormItem,
	InputItem, Input, Bind, SelectItem, Button, Label, Table, Section, DatePickerItem,ConfirmButton
} from '@/components/Common-Library'
import { Divider } from 'antd'
import Api from '@/service/api'
import mDbabch from '../model'
import CodeTableFactory from '@/components/CodeTable'
import "./index.less"
import { inject, observer } from 'mobx-react'
import { action, observable } from 'mobx'
import { Col, Row, Card } from 'antd'
import { Link } from 'react-router-dom'
import Moment from 'moment'
import Ptspta1 from '@/views/Public/Ptspta1'
const CodeTable = CodeTableFactory.getInstance()

@inject('UserContext')
@inject('i18n')
@observer
export default class DbabchScreen extends Page {
	@observable
  formBlur = false
  
  @observable
  extra = { inifrm: 'dbabch', sptinr: '', upbchkey:[], accbch:[]}
  
  //提交按钮的禁用
  @observable
  isDisabled=false;

	constructor(props) {
		super(props)
		this.model = new mDbabch()
    this.bind = Bind.bind(this)
    this.onSav=this.onSav.bind(this);
  }
  
  //初始化代码
  async componentWillMount() {
    if(this.props.location.state){
      let selflg = this.props.location.state.selflg
			if (selflg)
				this.onSel()
    }else{
      const rtnmsg = await Api.post(this.extra.inifrm + '/init', {})
      if (rtnmsg.retcod == SUCCESS) {
        Object.assign(this.model, rtnmsg.data)
        this.extra.upbchkey = rtnmsg.codetable.upbchkey;
        this.extra.accbch = rtnmsg.codetable.accbch;
      }
      else {
        Notification.error({ message: '服务端请求失败!' })
      }
    }
    //快照模式下，不必开启校验，否则，静默校验，和记录变动
    !this.loadDisplay() && this.model.openMonitor() //开启校验	
  }

	componentWillUnmount() {
		this.model.stopMonitor&&this.model.stopMonitor()//关闭校验
	}

	//按钮事件
	async onSav(){
    this.formBlur = true  //已触发表单提交处理
    this.isDisabled=true  //禁用提交按钮

    // //机构编码重名检核
    // if(this.model.branch){
    //   //检核接口，还需要跟后端核对协调一致
    //   const rtnmsg=await Api.post('dbabch/check',{params:{branch:this.model.branch},data:this.model})
    //   if( rtnmsg.retcod == SUCCESS ){
    //     if(rtnmsg.error.branch){
    //       Notification.error({message:'机构编码已存在!'})
    //       this.isDisabled=false
    //       return
    //     }
    //   }else{
    //     Notification.error({message:'服务端请求失败!'})
    //     this.isDisabled=false
    //     return
    //   }
    // }

		Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
			if (errors){
        this.isDisabled=false;
				return
      }
			const rtnmsg = await Api.post('dbabch/sav', { data: this.model })
			if (rtnmsg.retcod == SUCCESS) {
        console.log(rtnmsg.retcod)
				Notification.success({ message: '提交成功!' })
				this.props.history.push({ pathname: 'dblbch', state: { 'selflg': true } });
			}
			else {
        Notification.error({ message: '服务端请求失败!' })
        this.isDisabled=false;
			}
			//余下逻辑处理
		})
  }
  
	onRtn = () => {
		this.props.history.push("dblbch");
	}

	//交易内实现
	renderPage() {
		const bind = this.bind
		const { i18n } = this.props
		const CodeTable = CodeTableFactory.getInstance()

		return (
			<Card title="机构维护" size="small" className="transTitle">
				<div style={{ position: 'relative' }}>
          <Row>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000003"} />
                </Col>
                <Col span={14}>
                  <InputItem InputProps={bind("branch")} maxLength={8} desp="机构编码" path="/bchgrp/rec/branch" />
                </Col>
              </Row>
            </Col>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000004"} />
                </Col>
                <Col span={14}>
                  <InputItem InputProps={bind("namcn")} maxLength={40} desp="中文名称" path="/bchgrp/rec/namcn" />
                </Col>
              </Row>
            </Col>
          </Row>

          <Row>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000008"} />
                </Col>
                <Col span={14}>
                  <SelectItem InputProps={bind("lev")} code={CodeTable.bchlev} desp="机构级别" path="/bchgrp/rec/lev" />
                </Col>
              </Row>
            </Col>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000006"} />
                </Col>
                <Col span={14}>
                  <InputItem  InputProps={bind("adrcn")} desp="中文地址" path="/bchgrp/rec/adrcn" />
                </Col>
              </Row>
            </Col>
          </Row>

          <Row>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000010"} />
                </Col>
                <Col span={14}>
                  <InputItem InputProps={bind("upbranch")} maxLength={8} desp="上级机构编码" path="/bchgrp/rec/upbranch" />
                </Col>
              </Row>
            </Col>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000005"} />
                </Col>
                <Col span={14}>
                  <InputItem InputProps={bind("decnum")} maxLength={12} desp="金融机构标识码" path="/bchgrp/rec/decnum" />
                </Col>
              </Row>
            </Col>
          </Row>

          <Row>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000009"} />
                </Col>
                <Col span={14}>
                  <SelectItem InputProps={bind("accbch")} code={this.extra.accbch} desp="记账机构" />
                </Col>
              </Row>
            </Col>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000014"} />
                </Col>
                <Col span={14}>
                  <InputItem InputProps={bind("rmbrpt")} maxLength={12} desp="人行支付行号" path="/bchgrp/rec/rmbrpt" />
                </Col>
              </Row>
            </Col>
          </Row>

          <Row>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId={"BCHP_LT000015"} />
                </Col>
                <Col span={14}>
                  <SelectItem InputProps={bind("sta")} code={CodeTable.bchsta} desp="机构状态" path="/bchgrp/rec/sta" />
                </Col>
              </Row>
            </Col>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label value="机构类型" />
                </Col>
                <Col span={14}>
                  <SelectItem InputProps={bind("corflg")} code={CodeTable.corflg} desp="机构类型" path="/bchgrp/rec/corflg" />
                </Col>
              </Row>
            </Col>
          </Row>

          <Row>
            <Col span={12}>
              <Row className="row">
                <Col span={5} offset={2}>
                  <Label i18nId="业务上交单证中心" />
                </Col>
                <Col span={14}>
                  <SelectItem InputProps={bind("upbchkey")} code={this.extra.upbchkey} desp="业务上交单证中心" path="/bchgrp/rec/upbchkey" />
                </Col>
              </Row>
            </Col>
            <Col span={12}>
              <Ptspta1  model={this.model} argArr={{ desc: "关联客户信息", prefix: 'pts', url: 'dbabch/ptsp_ptysel', flg: 'B', datakey: '\\bchp\\ptsp\\ptslst' }} />
            </Col>
          </Row>
      
            <Row type="flex" justify="center">
              <Col>
                <div className="button0">
                  <div className='button2'>
                    {/* 退出按钮 */}
                    <Button type="primary" onClick={this.onRtn}>
                      <Label i18nId="MTABUT_BT000020" />      
                    </Button>
                  </div>
                  <div className='button2'>
                    <ConfirmButton
                      type="primary"
                      onOK={this.onSav}
                      icon="save"
                      message="确定提交？"
                      disabled={this.isDisabled}
                    >
                      <Label i18nId="MTABUT_BT000473" />
                    </ConfirmButton>
                  </div>
                </div>
              </Col>
            </Row>
				</div>
			</Card>
		)
	}
}
