import React, { Component, Fragment } from 'react'
import {
  Page, Validator, Notification, FormItem, DatePickerItem, AmountLabel,
  InputItem, Input, Bind, SelectItem, Button, Label, Image, Table
} from '@/components/Common-Library'
import Api from '@/service/api'
import mTrnrel from '../model'
import CodeTableFactory from '@/components/CodeTable'
import "./index.less"
import { inject, observer } from 'mobx-react'
import { action, observable } from 'mobx'
import { Row, Col } from 'antd'
import DateFormat from '@/utils/DateUtil'
import { Divider } from 'antd'
import Moment from 'moment'
import ArrayModal from '../ArrayModal';
import { UnicodePicker } from '@/components/Business-Library'
import { Card } from 'antd'

const CodeTable = CodeTableFactory.getInstance()


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

  constructor(props) {
    super(props)
    this.model = new mTrnrel()
    this.bind = Bind.bind(this)
  }

  componentWillUnmount() {
  
  }

  async componentWillMount() {
    // 将之前保存的数据赋值给model中
    this.model.atptxt = ArrayModal.atptxt;
    this.model.trn_ownref = ArrayModal.trn_ownref;
    this.model.atp_cod = ArrayModal.atp_cod;
    this.model.trncorco_iniusr = ArrayModal.trncorco_iniusr;
    this.model.trncorco_inidatfro = ArrayModal.trncorco_inidatfro;
    this.model.trncorco_inidattil = ArrayModal.trncorco_inidattil;
    this.model.trncorco_relflg = ArrayModal.trncorco_relflg;
    if (!this.model.trncorco_relflg)
      this.model.trncorco_relflg = "W";
    if (!this.model.trncorco_inidatfro)
      this.model.trncorco_inidatfro = DateFormat();
    this.onSearow()
		/*if(this.props.location.query){
			let selinr = this.props.location.query.inr
			const rtnmsg = await Api.post('dbiusr/init', { params: {selinr} })
			if (rtnmsg.retcod == SUCCESS) {
				//Notification.success({ message: '数据提交成功!' })
				Object.assign(this.model,rtnmsg.data)
			}
			else {
				Notification.error({ message: '服务端请求失败!' })
			}	
		}
		*/
  }

  trnlstHeader = [
    {
      title: <Label i18nId="TRNREL_TH000191" />,
      dataIndex: "inifrm",
      key: "inifrm",
      width: 110,
    },
    {
      title: <Label i18nId="TRNREL_TH000192" />,
      dataIndex: "ownref",
      key: "ownref",
      width: 100,
    },
    {
      title: <Label i18nId="TRNREL_TH000193" />,
      dataIndex: "iniusr",
      key: "iniusr",
      width: 100,
    },
    {
      title: <Label i18nId="TRNREL_TH000194" />,
      dataIndex: "reloricur",
      key: "reloricur",
      width: 100,
      render: (val, row, index) => {
        let text = val;
        let em = CodeTable.curtxt.find(item => item.value == val);
        if (em) {
          text = em.label;
        }
        return text
      },
    },
    {
      title: <Label i18nId="TRNREL_TH000195" />,
      dataIndex: "reloriamt",
      key: "reloriamt",
      width: 100,
      render: (val, row, index) => {
        return <AmountLabel cur={row.reloricur} amt={val} />
      },
    },
    {
      title: <Label i18nId="TRNREL_TH000196" />,
      dataIndex: "inidattim",
      key: "inidattim",
      width: 180,
      render: (val, row, index) => {
        if (val) {
          let dat = Moment(val).format('YYYY-MM-DD HH:mm:ss')
          return dat;
        }
        return val
      },
    },
    {
      title: <Label i18nId="TRNREL_TH000199" />,
      dataIndex: "relreq",
      key: "relreq",
      width: 100,
    },
    {
      title: <Label i18nId="TRNREL_TH000200" />,
      dataIndex: "relres",
      key: "relres",
      width: 100,
    },
    {
      title: <Label i18nId="TRNREL_TH000197" />,
      dataIndex: "relflg",
      key: "relflg",
      width: 100,
      render: (val, row, index) => {
        let text = val
        let em = CodeTable.relflg.find(item => item.value == val)
        if (em)
          text = em.label
        return text
      }
    },
    {
      title: '操作',
      dataIndex: "op",
      key: "op",
      width: 150,
      render: (val, row, index) => {
        return (
          row.relflg == "W" ? (
            <a href="javascript:;" onClick={() => this.onRelrow(row)}>复核</a>
          ) : (
            <a href="javascript:;" onClick={() => this.onRelrow(row)}>明细</a>
          )
        )
      }
    },
  ]


  //按钮事件
  onReset = () => {
    this.model.clear();
  }
  
  onSearow = () => {
    this.formBlur = true  //已触发表单提交处理
    Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
      if (errors)
        return
      const rtnmsg = await Api.post('trnrel/searow', { data: this.model })
      if (rtnmsg.retcod == SUCCESS) {
        //Notification.success({message:'数据提交成功!'})
        this.model.recpan_trnlst = rtnmsg.data['recpan_trnlst'];
      }
      else {
        Notification.error({ message: '服务端请求失败!' })
      }
      //余下逻辑处理

    })
  }

  onRelrow = (row) => {
    ArrayModal.atptxt = this.model.atptxt;
    ArrayModal.trn_ownref = this.model.trn_ownref;
    ArrayModal.atp_cod = this.model.atp_cod;
    ArrayModal.trncorco_iniusr = this.model.trncorco_iniusr;
    ArrayModal.trncorco_inidatfro = this.model.trncorco_inidatfro;
    ArrayModal.trncorco_inidattil = this.model.trncorco_inidattil;
    ArrayModal.trncorco_relflg = this.model.trncorco_relflg;

    this.formBlur = true  //已触发表单提交处理
    this.props.history.push({ pathname:'trndet', state: { 'trninr': row.inr } });
  }


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

    return (
      <Card title="复核夹" size="small" className="transTitle">
        <div style={{ position: 'relative', padding: 20 }}>
          {/* <UnicodePicker model={{}} prefix="txcode" /> */}
          {/*第一行*/}
          <Row type="flex" justify="center" style={{ marginBottom: 24 }}>
            <Col span={11} style={{ height: 40 }}>
              <Row className='row'>
                <Col span={4}>
                  <Label i18nId={"TRNREL_SF000094"} />
                </Col>
                <Col span={13}>
                  <InputItem InputProps={bind("atptxt")} />
                </Col>
              </Row>
            </Col>
            <Col span={11} style={{ height: 40 }} offset={1}>
              <Row className='row'>
                <Col span={4}>
                  <Label i18nId={"TRNREL_SF000075"} />
                </Col>
                <Col span={13}>
                  <InputItem InputProps={bind("trn_ownref")} />
                </Col>
              </Row>
            </Col>
          </Row>
          {/*第二行*/}
          <Row type="flex" justify="center" style={{ marginBottom: 24 }}>
            <Col span={11} style={{ height: 40 }}>
              <Row className='row'>
                <Col span={4}>
                  <Label i18nId={"TRNREL_SF000077"} />
                </Col>
                <Col span={13}>
                  <InputItem InputProps={bind("atp_cod")} />
                </Col>
              </Row>
            </Col>
            <Col span={11} style={{ height: 40 }} offset={1}>
              <Row className='row'>
                <Col span={4}>
                  <Label i18nId={"TRNREL_SF000124"} />
                </Col>
                <Col span={13}>
                  <InputItem InputProps={bind("trncorco_iniusr")} />
                </Col>
              </Row>
            </Col>
          </Row>
          {/*第三行*/}
          <Row type="flex" justify="center" style={{ marginBottom: 24 }}>
            <Col span={11} style={{ height: 40 }}>
              <Row className='row'>
                <Col span={4}>
                  <Label i18nId={"TRNREL_SF000079"} />
                </Col>
                <Col span={20}>
                  <Col span={7}>
                    <DatePickerItem InputProps={bind("trncorco_inidatfro")} />
                  </Col>
                  <Col span={2} offset={1}>
                    <Label i18nId={"TRNREL_SF000076"} />
                  </Col>
                  <Col span={7}>
                    <DatePickerItem InputProps={bind("trncorco_inidattil")} />
                  </Col>
                </Col>
              </Row>
            </Col>
            <Col span={11} style={{ height: 40 }} offset={1}>
              <Row className='row'>
                <Col span={4}>
                  <Label i18nId={"TRNREL_SF000078"} />
                </Col>
                <Col span={13}>
                  <SelectItem InputProps={bind("trncorco_relflg")} code={CodeTable.relflg} />
                </Col>
              </Row>
            </Col>
          </Row>
          {/*按钮行*/}
          <Row type="flex" justify="end" style={{ marginTop: 20, marginBottom: 20 }}>
            <Col span={2} style={{ height: 40 }}>
              <Button type="primary" icon="search" onClick={this.onSearow}>
                <Label i18nId="TRNREL_FD000184" />
              </Button>
            </Col>

            <Col span={2} style={{ height: 40 }}>
              <Button type="primary" onClick={this.onReset}>
                <Label i18nId="MTABUT_BT000023" />
              </Button>
            </Col>
          </Row>
          <Table pagination={{ pageSize: 10, hideOnSinglePage: true }} rowKey="inr" columns={this.trnlstHeader} dataSource={this.model.recpan_trnlst} />
        </div>
      </Card>
    )
  }
}
