import React, { Component, Fragment } from 'react'
import {
    Page, Validator, Notification, FormItem,
    InputItem, Input, Bind, SelectItem, Button, Label, DatePickerItem, Section, ConfirmButton, Steps, CheckboxItem, TextAreaItem
} 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, Tooltip, Modal, Spin, Table } 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'
import mDitsel from '../model'
const { Pre, Next, StepWrapper } = Steps
const CodeTable = CodeTableFactory.getInstance()
const confirm = Modal.confirm

@withRouter
@inject('UserContext')
@inject('i18n')
@observer
export default class XinyongzhengScreen extends Page {
    @observable
    formBlur = false

    constructor(props) {
        super(props)
        this.model = new mDitsel()
        this.bind = Bind.bind(this)
        this.model.selflg = props.flg
        this.state={
          selectedInr:""
        }
    }

    componentDidMount() {
        
        super.componentDidMount()
        if (this.props.flg == "0") {
            this.ditmod_didlstHeader.push(
                {
                    title: <Label itext="操作" />,
                    width: 60,
                    key: "operation",
                    render: (val, row, index) => {
                        return (
                          <a onClick={()=>{this.link('ditdck',row.inr)}}>到单</a>
                        )
                    }

                }

            )
        }
        else if (this.props.flg == "4") {
            this.ditmod_didlstHeader.push(
                {
                    title: <Label itext="操作" />,
                    width: 60,
                    key: "operation",
                    render: (val, row, index) => {
                        return (
                          <a onClick={()=>{this.link('ditcan',row.inr)}}>闭卷</a>
                        )
                    }

                })
        }
         else  if (this.props.flg == "5") {
             this.didinr="0"
             this.model.inr="0"
            this.model.ramflg="1"
            this.model.rejflg="1"
            this.model.ameflg="1"
             }
    }

    onTabFocus(){
      this.setState({selectedInr:""}) //切换页签时重置行选中样式
    }

    //交易跳转函数，若参数inr存在，则赋值给this.didinr，此时为闭卷或者到单，不存在则为信用证其他
    //调用后台查询是否符合跳转前提（前手交易是否复核），根据返回值决定是否带参跳转至pathname交易
    async link(pathname, inr){
      if(inr){
        this.didinr = inr
      }
     // const rtnmsg = await Api.post('ditsel/cfgfil_isnext', { data: this.model, params: { objinr:this.didinr, objtyp:"did"} })     
      const rtnmsg = await Api.post('ditsel/sysmod_isnext', { data: this.model, params: { objinr:this.didinr, objtyp:"did"} })
      if (rtnmsg.retcod == SUCCESS) {
        this.props.history.push({ pathname, query: { didinr:this.didinr} });
      }else{
        Notification.error({ message: `${rtnmsg.retmsg}` })
      }
    }

   async onSelcon(didinr){
        this.formBlur=true  //已触发表单提交处理
        this.setState({selectedInr:didinr}) //获取选中行的inr,用于改变选中行样式
        this.didinr=didinr;
        this.model.inr=didinr;
        this.model.ramflg="1";
        this.model.rejflg="1";
        this.model.ameflg="1";
        if(this.props.flg == "5"){
          const rtnmsg = await Api.post('ditsel/selcon',{ params: { didinr:didinr }, data:{} })
          this.model.ramflg=rtnmsg.data.ramflg;
          this.model.rejflg=rtnmsg.data.rejflg;
          this.model.ameflg=rtnmsg.data.ameflg;
        }
    }

    ditmod_didlstHeader = [
        {
            title: <Label itext="信用证编号" />,
            dataIndex: "ownref",
            key: "ownref",
            width: 100,
            sorter: (r1, r2) => ColSorter(r1.ownref, r2.ownref),
        },
        {
            title: <Label itext="概要描述" />,
            dataIndex: "nam",
            key: "nam",
            width: 260,
            sorter: (r1, r2) => ColSorter(r1.nam, r2.nam),
        },
        {
            title: <Label itext="开立日期" />,
            dataIndex: "opndat",
            key: "opndat",
            width: 90,
            sorter: (r1, r2) => ColSorter(r1.opndat, r2.opndat),
            render:(val)=>{
              if(val)
              return moment(val).format('YYYY-MM-DD')
            }
        },
        {
            title: <Label itext="开证类型" />,
            dataIndex: "isstyp",
            key: "isstyp",
            width: 100,
            sorter: (r1, r2) => ColSorter(r1.isstyp, r2.isstyp),
            render: (val, row, index) => {
                let text = val
                let em = CodeTable.isstyp.find(item => item.value == val)
                if (em)
                    text = em.label
                return text
            }
        },
        // {
        //     title: <Label itext="贸易方式" />,
        //     dataIndex: "trdtyp",
        //     key: "trdtyp",
        //     width: 100,
        //     sorter: (r1, r2) => ColSorter(r1.trdtyp, r2.trdtyp),
        //     render: (val, row, index) => {
        //         let text = val
        //         let em = CodeTable.trdtyp.find(item => item.value == val)
        //         if (em)
        //             text = em.label
        //         return text
        //     }
        // },
        {
            title: <Label itext="失效日期" />,
            dataIndex: "expdat",
            key: "expdat",
            width: 80,
            sorter: (r1, r2) => ColSorter(r1.expdat, r2.expdat),
            render:(val)=>{
              if(val)
              return moment(val).format('YYYY-MM-DD')
            }
        },
        {
            title: <Label itext="对付方式" />,
            dataIndex: "avbby",
            key: "avbby",
            width: 80,
            sorter: (r1, r2) => ColSorter(r1.avbby, r2.avbby),
            render: (val, row, index) => {
                let text = val
                let em = CodeTable.avbby.find(item => item.value == val)
                if (em)
                    text = em.label
                return text
            }
        },
        {
            title: <Label itext="效期(天)" />,
            dataIndex: "tenmaxday",
            key: "tenmaxday",
            width: 80,
            sorter: (r1, r2) => ColSorter(r1.tenmaxday, r2.tenmaxday),
        },
        {
            title: <Label itext="闭卷日期" />,
            dataIndex: "clsdat",
            key: "clsdat",
            width: 90,
            sorter: (r1, r2) => ColSorter(r1.clsdat, r2.clsdat),
            render:(val)=>{
              if(val)
              return moment(val).format('YYYY-MM-DD')
            }
        },
        {
            title: <Label itext="经办柜员" />,
            dataIndex: "ownusr",
            key: "ownusr",
            width: 100,
            sorter: (r1, r2) => ColSorter(r1.ownusr, r2.ownusr),
        },
    ]

    // onRefClear = () => {
    //     if (!this.model.ownref1) {
    //         this.model.clear()
    //         this.props.initTrans()
    //     }
    // }
    onSellst = async () => {
        this.formBlur = true  //已触发表单提交处理


        const rtnmsg = await Api.post('ditsel/sellst', { data: this.model })
        if (rtnmsg.retcod == SUCCESS) {
            this.model.ditmod_didlst = rtnmsg.data['ditmod_didlst'];

        }
        else {
            Notification.error({ message: '查询失败!' })
        }
        //余下逻辑处理


    }

    rowClass = (row, index)=>{
      //表格选中行变色
      if(this.props.flg == 5 &&  row.inr == this.state.selectedInr){
        return "selectedStyle"
      }
      //单双行样式
      if(index %2)
      {
        return "tableEvenStyle"
      }
      return "tableOddStyle"
    }

    //交易内实现
    renderPage() {
        const bind = this.bind
        const { i18n } = this.props
        const { bchlevelLst } = this.props.extra
        const dateFormat = 'YYYY-MM-DD';
        //const {next,pre} = this.props.steps

        return (
            <div style={{ position: 'relative' }}>
                <Business wrappedComponentRef={ref => (this.com = ref)} extra={this.props.extra} model={this.model} />
                {/* 第一行 */}
                <Row>
                    <Col span={12}>
                        <Row className="row">
                            <Col span={4} >
                                <Label itext="信用证编号" trn="ditsel" />
                            </Col>
                            <Col span={6} >
                                <InputItem InputProps={{...bind("ditmod_diref"),onPressEnter:this.onSellst}} maxLength={16} desp="信用证编号" path="/ditmod/diref" />
                            </Col>


                            <Col span={6} offset={2}>
                                <Button type="primary" icon="search" size="small" onClick={this.onSellst}  desp="查询" path="/sellst">
                                    <Label itext="查询" />
                                </Button>
                            </Col>
                        </Row>
                    </Col>

                </Row>
                <Row style={{ marginTop: 30 }}>
                    <Col span={24} >
                        <Table  
                          rowKey="inr"  size="middle"
                          onRow={ row=>({onClick:()=>this.onSelcon(row.inr)}) } 
                          columns={this.ditmod_didlstHeader} 
                          dataSource={this.model.ditmod_didlst}
                          rowClassName={this.rowClass}
                        />
                    </Col>
                </Row>


                {this.props.flg=="5"?
                <div style={{ textAlign: "center", marginTop: 30 }}>

                     <Button style={{ marginLeft: 10, marginRight: 10 }} disabled={this.model.rejflg=="1"?true:false}type="primary" size="small" onClick={()=>{this.link("ditrej")}}>
                         <Label itext=" 拒绝通知处理" />
                     </Button>
                     <Button style={{ marginLeft: 10, marginRight: 10 }} disabled={this.model.ramflg=="1"?true:false}type="primary" size="small" onClick={()=>{this.link("ditram")}}>
                         <Label itext="修改请求" />
                     </Button>
                     <Button style={{ marginLeft: 10, marginRight: 10 }} disabled={this.model.ameflg=="1"?true:false}type="primary" size="small" onClick={()=>{this.link("ditame")}}>
                         <Label itext="修改确认" />
                    </Button> 
                    <Button style={{ marginLeft: 10, marginRight: 10 }} disabled={this.model.inr=="0"?true:false}type="primary" size="small" onClick={()=>{this.link("ditfre")}}>
                        <Label itext="自由格式报文" />
                    </Button>
                    <Button style={{ marginLeft: 10, marginRight: 10 }} disabled={this.model.inr=="0"?true:false} type="primary" size="small" onClick={()=>{this.link("ditfee")}}>
                        <Label itext=" 收费" />
                    </Button>
                </div>
                :null
        }
               



            </div >
        )
    }
}
