import React, { Component, Fragment } from 'react'
import {
    Page, Validator, Notification, FormItem, Table,
    InputItem, Input, Bind, SelectItem, Button, Label
} from '@/components/Common-Library'
import Api from '@/service/api'
import mDbitsn from '../model'
import CodeTableFactory from '@/components/CodeTable'
import "./index.less"
import { inject, observer } from 'mobx-react'
import { action, observable } from 'mobx'
import { Row, Col, Card } from 'antd'
import { TableExpansion } from '@/components/Business-Library'

const CodeTable = CodeTableFactory.getInstance()

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

    constructor(props) {
        super(props)
        this.model = new mDbitsn()
        // this.model.openValidate() //开启校验
        this.bind = Bind.bind(this)
    }
    @observable
    tfccod = []
    componentWillUnmount() {
        // this.model.closeValite()//关闭校验
    }
    async componentWillMount() {
        if (this.props.location.state) {
            let selflg = this.props.location.state.selflg
            if (selflg)
                this.onSel()
        } else {
            const rtnmsg = await Api.post('dbitsn/init', {})
            if (rtnmsg.retcod == SUCCESS) {
                Object.assign(this.model, rtnmsg.data)
                this.tfccod = rtnmsg.codetable['tfccod'];
            }
            else {
                Notification.error({ message: '服务端请求失败!' })
            }
        }
    }
    tsnlstHeader = [
        {
            title: <Label value="单证中心" />,
            dataIndex: "tfccod",
            key: "tfccod",
            width: 100,
        },
        {
            title: <Label value="业务品种" />,
            dataIndex: "objtyp",
            key: "objtyp",
            width: 100,
        },
        {
            title: <Label value="交易" />,
            dataIndex: "frm",
            key: "frm",
            width: 80,
        },
        {
            title: <Label value="交易说明" />,
            dataIndex: "txt",
            key: "txt",
            width: 140,
        },
        {
            title: <Label value="组别" />,
            dataIndex: "usg",
            key: "usg",
            width: 100,
        },
        {
            title: <Label value="优先级" />,
            dataIndex: "proity",
            key: "proity",
            width: 100,
            render: (val, row, index) => {
                let text = val
                let em = CodeTable.proity.find(item => item.value == val)
                if (em)
                    text = em.label
                return text
            },
            type: {
                edit: { type: "select", code: CodeTable.proity }
            }
        },
        {
            title: <Label value="参与标志" />,
            dataIndex: "exeflg",
            key: "exeflg",
            width: 80,
            render: (val, row, index) => {
                let text = val
                let em = CodeTable.exeflg.find(item => item.value == val)
                if (em)
                    text = em.label
                return text
            },
            type: {
                edit: { type: "select", code: CodeTable.exeflg }
            }
        },
    ]


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

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

        })
    }
    // 按钮事件 保存
    onButsav = () => {
        this.formBlur = true  //已触发表单提交处理
        Validator.validate(this.model, this.model.descriptor, async (errors, values) => {
            if (errors)
                return
            const rtnmsg = await Api.post('dbitsn/butsav', { data: this.model })
            if (rtnmsg.retcod == SUCCESS) {
                Notification.success({ message: '数据提交成功!' })

            }
            else {
                Notification.error({ message: '服务端请求失败!' })
            }
            //余下逻辑处理

        })
    }


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


        return (
            <Card title="交易分配设置" size="small" className="transTitle">
                <div style={{ position: 'relative' }}>

                    <Row>
                        <Col span={20}>
                            <Row>
                                <Col span={12}>
                                    <Row className='row'>
                                        <Col span={5} offset={2}>
                                            <Label value="单证中心" />
                                        </Col>
                                        <Col span={14}>
                                            <SelectItem InputProps={bind("tfccod")} code={this.tfccod} />
                                        </Col>
                                    </Row>
                                </Col>
                                <Col span={12}>
                                    <Row className='row'>
                                        <Col span={5} offset={2}>
                                            <Label value="业务品种" />
                                        </Col>
                                        <Col span={14}>
                                            <SelectItem InputProps={bind("objtyp")} code={CodeTable.busrou} />
                                        </Col>
                                    </Row>
                                </Col>
                            </Row>
                            <Row>
                                <Col span={12}>
                                    <Row className='row'>
                                        <Col span={5} offset={2}>
                                            <Label value="组别" />
                                        </Col>
                                        <Col span={14}>
                                            <InputItem InputProps={bind("usg")} />
                                        </Col>
                                    </Row>
                                </Col>
                            </Row>
                        </Col>
                    </Row>
                    <div style={{ textAlign: "right" }}>
                        <Button style={{ marginLeft: 10, marginRight: 10 }} type="primary" icon="search" onClick={this.onButsel}>
                            {/* <Label i18nId="DBITSN_BT000001" /> */}
                            <Label value="搜索" />
                        </Button>
                    </div>
                    <TableExpansion add={false} onChange={(value) => { this.model.tsnlst = value }} columns={this.tsnlstHeader} dataSource={this.model.tsnlst} />

                    <div style={{ textAlign: "center", marginTop: 12 }}>
                        <Button style={{ marginLeft: 10, marginRight: 10 }} type="primary" onClick={this.onButmdf}>
                            {/* <Label i18nId="DBITSN_BT000009" /> */}
                            <Label value="修改" />
                        </Button>
                        <Button style={{ marginLeft: 10, marginRight: 10 }} type="primary" icon="save"
                            onClick={this.onButsav}>
                            {/* <Label i18nId="DBITSN_BT000010" /> */}
                            <Label value="保存" />
                        </Button>
                        <Button style={{ marginLeft: 10, marginRight: 10 }} type="primary" onClick={this.onButbak}>
                            {/* <Label i18nId="DBITSN_BT000011" /> */}
                            <Label value="返回" />
                        </Button>
                    </div>
                </div>
            </Card>
        )
    }
}
