/**
 * 执行记录
 */

import React, { Component } from 'react';
import { Form, Select, Modal, DatePicker, Table, Pagination, Button } from 'antd';
import { Util,dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import CommonPagination from 'components/pagination/index';
import RangeDatePicker from 'components/rangePicker/index';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import store from 'store';
import '../style/index.less';
import moment from 'moment';

const Option = Select.Option;
const FormItem = Form.Item;
const { RangePicker } = DatePicker;

class ShowExecutionRecordModal extends Component {

    constructor(props) {
        super(props);
        this.state = {
            total: 0,
            page: 1,
            pageSize: 10,
            dataSource: []
        }
    }

    componentWillReceiveProps(nextProp) {
        if (nextProp.isShow === true) {
            this.setState({
                executionData: nextProp.executionData
            }, () => {
                this.loadData(1)
            })
        } else if (this.props.isShow === false) {
            this.cleanInput();
        }
    }

    componentDidMount() {
        this.initData()
        this.loadColumn()
    }

    componentWillUnmount() {
		// 组件销毁前将静止让setState修改state的状态
		this.setState = (state, callback) => { return; }
	}

    loadColumn() {
        var params = {
            params: [{
                compontName: 'executionRecordModal',
                type: 'C'
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    var columns = res.result.C;
                    var dynColumns = []
                    for (var i = 0; i < columns.length; i++) {
                        dynColumns.push({
                            title: columns[i].title,
                            dataIndex: columns[i].dataIndex,
                            width: parseInt(columns[i].width),
                            align: columns[i].align || 'center',
                        })

                    }
                    this.setState({
                        column: dynColumns,
                        totalWidth: res.totalWidth
                    })
                    this.props.hanlderThis.setState({
                        reload: false,
                    })
                }
            }
        });
    }

    initData() {
        this.columns = [
            {
                title: '组',
                dataIndex: 'ordLinkSign',
                width: 80,
                align: 'center',
                render: (text, data) => {
                    return <span style={{ color: 'blue' }}>{data.ordLinkSign}</span>
                }
            }, {
                title: '医嘱内容',
                dataIndex: 'ordMessage',
                width: 200,
                align: 'center',
            }, {
                title: '状态',
                dataIndex: 'statusDesc',
                width: 100,
                align: 'center',
            }, {
                title: '执行信息',
                dataIndex: 'execMessage',
                width: 200,
                align: 'center',
            },
        ]
    }

    loadData(page, pageSize, e) {
        var params = {
            params: [{
                ordID: this.state.executionData ? this.state.executionData.ordID : '',
                dateFrom: this.state.dateFrom,
                dateTo: this.state.dateTo
            }],
            pagination: [{
                pageSize: this.state.pageSize,
                currentPage: page
            }]
        }
        $http.post('urlDeault', this, {
            code: "03060008",
            data: params,
            success: function (res) {
                if (res.errorCode === '0') {
                    this.setState({
                        dataSource: res.result.data,
                        total: res.result.totalCount
                    })
                }
            }
        });
    }

    closeModal() {
        let { onHide } = this.props;
        onHide && onHide();
        this.cleanInput();
    }

    //清空输入框
    cleanInput() {
        this.setState({
            code: []
        })
    }

    changeRegistrTime(date, dateStr) {
        this.setState({
            dateFrom: dateStr[0],
            dateTo: dateStr[1]
        }, () => {
            this.loadData(1)
        })
    }

    startChange(e) {
        this.setState({
            dateFrom: e ? moment(e).format(dayFormat) : ''
        })
    }

    endChange(e) {
        this.setState({
            dateTo: e ? moment(e).format(dayFormat) : ''
        })
    }

    //分页切换
    onChange(e) {
        this.setState({ page: e }, () => {
            this.loadData(e);
        });
    }

    //分页切换
    changePageSize(current, pageSize) {
        this.setState({ pageSize: pageSize, page: current }, () => {
            this.loadData(current, pageSize);
        });
    }

    //提交
    onSubmit() {
        let { onSubmit } = this.props;
        onSubmit && onSubmit(this.state.data);
    }

    onColumnRef = (ref) => {
        this.columnRef = ref;
    }

    render() {
        return <div>
            <ColumnAuthoritysModel
                clientWidth='800px'
                compontName="executionRecordModal"
                configType="C"
                onRef={this.onColumnRef}
                reloadData={this.loadColumn}
            />
            <Modal className='execution-modal'
                title={'查看执行记录'}
                maskClosable={false}
                visible={this.props.isShow}
                footer={null}
                onCancel={this.closeModal.bind(this)}
                onOk={this.onSubmit.bind(this)}>
                <Table columns={this.state.column}
                    dataSource={this.state.dataSource}
                    title={() => {
                        return <div style={{ display: 'flex' }}>
                            {/* <div>执行时间：<RangePicker onChange={this.changeRegistrTime.bind(this)} /></div> */}
                            <RangeDatePicker label={'医嘱时间：'} format='YYYY-MM-DD' totalwidth='40vh' labelwidth='10vh'
                                startChange={this.startChange.bind(this)} endChange={this.endChange.bind(this)} />
                            <Button style={{ marginLeft: 10 }} type='primary' onClick={this.loadData.bind(this, '1')}>查询</Button>
                            <div style={{ marginLeft: '20%' }}>共执行<span style={{ color: '#1999db' }}>{this.state.total || 0}</span>次</div>
                        </div>
                    }} bordered
                    rowKey={(data) => {
                        return data.execID
                    }}
                    onHeaderRow={column => {
                        return {
                            onDoubleClick: () => {
                                this.columnRef.onOpenModel();
                            },
                        };
                    }}
                    scroll={{ y: '35vh', x: this.state.totalWidth }}
                    pagination={false}></Table>
                <CommonPagination size="small" current={this.state.page} total={this.state.total} search={this.state.search}
                    onShowSizeChange={this.changePageSize.bind(this)} complete={true} dataSource={this.state.dataSource}
                    loading={this.state.loading} onChange={this.onChange.bind(this)} />
                {/* <Pagination
                    size="small"
                    showQuickJumper
                    showSizeChanger
                    defaultCurrent={1}
                    current={this.state.page}
                    total={this.state.total}
                    pageSize={this.state.pageSize}
                    onShowSizeChange={this.changePageSize.bind(this)}
                    onChange={this.onChange.bind(this)}
                    style={{ textAlign: "right", marginTop: '13px' }}
                /> */}
            </Modal></div >
    }
}

export default Form.create()(ShowExecutionRecordModal);