//住院病人预览
import React from 'react'
import { Table, Row, Col, Button, Input, DatePicker, Select, Card } from 'antd'
import PubilcTablePagination from '../../common/PubilcTablePagination.jsx'; // 公共table组件
import moment from 'moment';
import { $http } from '../../../containers/config/https'
import store from '../../../store';
import '../style/index.less';


import CommonPagination from '../../../components/pagination/index';
import ColumnAuthoritysModel from '../../../pages/columnAuthority/ColumnAuthoritysModel';
const { Option } = Select

export default class FindHospitalizedBSAdm extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            data: [],
            HBUserArr: [],
            AdmReasonArr: [],
            startDate: moment(new Date),
            stopDate: moment(new Date),
            userID: JSON.parse(window.sessionStorage.userData).userID,
            patID: '',
            admReasonID: undefined,
            // 表格数据
            tableData: [],
            showLoading: true,
            // 表格数据总条数
            total: 0,
            page: 1,
            pageSize: 10,
            columns: [], //表格列表
            languageTable1: {}, // 国际化Table1
            languageTable2: {}, // 国际化Table2
            languageData: {}, // 国际化语言包
            language: {},   // 国际化语言
            languageCode: JSON.parse(window.sessionStorage.userData).language, // 国际化语言标识
        }
        // this.columns = [
        //     {
        //         title: '病人ID',
        //         dataIndex: 'patNo',
        //         width: '100px',
        //         align: 'center',
        //     },
        //     {
        //         title: '科室',
        //         dataIndex: 'locDesc',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '病区',
        //         dataIndex: 'wardDesc',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '姓名',
        //         dataIndex: 'name',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '年龄',
        //         dataIndex: 'age',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '电话',
        //         dataIndex: 'mobile',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '登记员',
        //         dataIndex: 'doctorDesc',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '身份证',
        //         dataIndex: 'IDCardNo',
        //         width: '150px',
        //         align: 'center'
        //     },
        //     {
        //         title: '费别',
        //         dataIndex: 'admReasonDesc',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '押金',
        //         dataIndex: 'payAmtAll',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '费用',
        //         dataIndex: 'patAmtAll',
        //         width: '100px',
        //         align: 'center'
        //     },
        //     {
        //         title: '余额',
        //         dataIndex: 'balance',
        //         width: '100px',
        //         align: 'center'
        //     },
        // ]
    }
    // 查询条件 --- 开始日期
    onChangeStartDate = (e) => {
        this.setState({ startDate: e });
    }
    // 查询条件 --- 结束日期
    onChangeStopDate = (e) => {
        this.setState({ stopDate: e });
    }
    // 查询条件 --- 登记员
    onChangeUserID = (e) => {
        this.setState({ userID: e });
    }
    // 查询条件 --- 病人ID
    onChangePatID = (e) => {
        this.setState({ patID: e.target.value });
    }
    // 查询条件 --- 费别
    onChangeAdmReasonID = (e) => {
        this.setState({ admReasonID: e });
    }
    //初始化数据
    componentDidMount() {
        this.tableList("OPFindHospitalizedBSAdm", "OPFindHospitalizedBSAdm");
        // this.child.handleLoading()
        this.getUserData()
        this.getAdmReasonData()
        this.handleQuery()
    }
    //费别下拉框
    getAdmReasonData = () => {
        $http.post("urlS", this, {
            code: '05100129',
            data: {
                params: [{
                    "hospID": JSON.parse(window.sessionStorage.userData).hospID, //医院id 
                }]
            },
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({ AdmReasonArr: res.data })
                }
            }
        })
    }
    //操作员下拉框
    getUserData = () => {
        $http.post("urlS", this, {
            code: '05210023',
            data: {
                params: [{
                    "hospID": JSON.parse(window.sessionStorage.userData).hospID, //医院id 
                    "locTypeCode": "C"
                }]
            },
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({ HBUserArr: res.user })
                }
            }
        })
    }
    //基础数据
    handleQuery = () => {
        let { } = this.state;
        let { startDate, stopDate, userID, patID, admReasonID, page, pageSize } = this.state;
        let data = {}
        data = {
            params: [{
                "startDate": startDate,
                "stopDate": stopDate,
                "userID": userID || '',
                "patID": patID,
                "admReasonID": admReasonID || '',
                "hospID": JSON.parse(window.sessionStorage.userData).hospID,
            }],
            pagination: [{
                pageSize: pageSize,
                currentPage: page,
                sortColumn: 'code',
                sortOrder: 'asc'
            }]
        }
        $http.post('urlDeanlt', this, {
            code: '05100163',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.result) {
                    for (var i = 0; i < res.result.length; i++) {
                        res.result[i].key = i;
                    }
                    this.setState({ tableData: res.result.Data, total: res.result.TotalCount, loading: false })
                } else {
                    this.setState({ tableData: [], total: 0, loading: false })
                }
            }
        })
    }
    // 获取主列表columns
    tableList = (reactCode, compontName) => {
        var data = {
            params: [{
                "reactCode": [reactCode],
                "compontName": compontName,
                "type": "C"
            }]
        }
        console.log(1, data)
        $http.post('urlS', this, {
            code: '01040073',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    let resC = res.result.C
                    let { languageCode } = this.state
                    for (var i = 0; i < resC.length; i++) {
                        resC[i].key = i;
                    }
                    if (languageCode === "EN") {
                        resC.map((item, i) => {
                            item.title = item.enDescripts
                        })
                    }

                    if (languageCode === "CN") {
                        resC.map((item, i) => {
                            item.title = item.descripts
                        })
                    }
                    let arr = res.result && resC.length > 0 ? JSON.parse(JSON.stringify(resC)).map(item => {
                        return {
                            align: item.align,
                            widthNum: parseInt(item.width),
                            width: item.width,
                            dataIndex: item.dataIndex,
                            title: <div onDoubleClick={() => this.columnRef.onOpenModel()}>{item.title}</div>,
                            display: item.display,
                            key: item.seqNo
                        }
                    }) : '';
                    let filterArr = arr.filter(function (item) { return item.display === 'Y' });
                    this.setState({
                        columns: filterArr,
                        languageTable1: res.result.B || {},  // 国际化Table1
                        languageTable2: res.result.C || {},  // 国际化Table2
                        languageData: res.result.language || {},  // 国际化语言包
                    }, () => {
                        this.LanCNEN(this.state.languageCode)
                        //   this.listAdd(filterArr.length+1, 0,languageCode);
                    })
                }
            }
        })
    }
    LanCNEN = (code) => {
        let { languageData } = this.state
        console.log(languageData)
        this.setState({
            language: languageData[code]
        })
        console.log(languageData)
    }
    onColumnRef = (ref) => {
        console.log(ref)
        this.columnRef = ref;
    }
    // 调用公共组件的连接事件
    onRef = (ref) => {
        this.child = ref
    }
    // 提供修改page和pageSize的回调函数
    compilePage(page, pageSize) {
        // 修改page，或者pageSize
        this.setState({ page: page, pageSize: pageSize }, () => {
            // 重新调用查询方法
            this.handleQuery();
        })
    }
    // 条数改变的回调
    apponShowSizeChange = (current, size) => {
        this.setState({
            page: current,
            pageSize: size,
        }, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }
    // 页码改变的回调，参数是改变后的页码及每页条数
    appPageChange = (page, pageSize) => {
        this.setState({
            page: page,
            pageSize: pageSize,
        }, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }
    render() {
        let { AdmReasonArr, HBUserArr, handleQuery, stratDate, endDate, UserID, language } = this.state;
        // 组件所需参数
        // const param = {
        //     // 表头配置
        //     columns: this.columns,
        //     // 表格数据
        //     data: this.state.tableData,
        //     // 数据总条数
        //     total: this.state.total,
        //     // 数据页数
        //     page: this.state.page,
        // }
        //费别
        let admReason = AdmReasonArr && AdmReasonArr.map((item, index) => {
            return (<Option key={index} title={item.descripts} value={item.id}>{item.descripts}</Option>)
        })
        // 操作员
        let hBUser = HBUserArr && HBUserArr.map((item, index) => {
            return (<Option key={index} title={item.descripts} value={item.id}>{item.descripts}</Option>)
        })

        return (
            <div>
                <div style={{ width: '100%', height: '100%', background: '#F5F7FE' }} className='hosptial-adm-body'>
                    <Card className="department-header" bodyStyle={{ padding: '10px' }}>
                        <Col span={4} className='common-col-ul'>
                            <span className='common-span-ui'>{language.OPFindHospitalizedBSAdmstratDate && language.OPFindHospitalizedBSAdmstratDate.descripts}：</span>
                            <DatePicker className='common-right-ui' allowClear={false} format="YYYY-MM-DD" style={{ marginRight: '10px' }} value={this.state.startDate} onChange={this.onChangeStartDate} />
                        </Col>
                        <Col span={4} className='common-col-ul'>
                            <span className='common-span-ui'>{language.OPFindHospitalizedBSAdmendDate && language.OPFindHospitalizedBSAdmendDate.descripts}：</span>
                            <DatePicker className='common-right-ui' allowClear={false} format="YYYY-MM-DD" style={{ marginRight: '10px' }} value={this.state.stopDate} onChange={this.onChangeStopDate} />
                        </Col>
                        <Col span={4} className='common-col-ul'>
                            <span className='common-span-ui'>{language.OPFindHospitalizedBSAdmhBUser && language.OPFindHospitalizedBSAdmhBUser.descripts}：</span>
                            <Select className='common-right-ui' allowClear value={this.state.userID} onChange={this.onChangeUserID} style={{ width: '150px' }}>{hBUser}</Select>
                        </Col>
                        <Col span={4} className='common-col-ul'>
                            <span className='common-span-ui'>{language.OPFindHospitalizedBSAdmUserID && language.OPFindHospitalizedBSAdmUserID.descripts}：</span>
                            <Input className='common-right-ui' value={this.state.patID} onChange={this.onChangePatID} style={{ width: '150px' }} />
                        </Col>
                        <Col span={4} className='common-col-ul'>
                            <span className='common-span-ui'>{language.OPFindHospitalizedBSAdmadmReason && language.OPFindHospitalizedBSAdmadmReason.descripts}：</span>
                                <Select className='common-right-ui' allowClear placeholder={language.OPFindHospitalizedBSAdmadmReason && language.OPFindHospitalizedBSAdmadmReason.message}
                                value={this.state.admReasonID} onChange={this.onChangeAdmReasonID} style={{ width: '150px' }}>{admReason}</Select>
                        </Col>

                        <Col span={4}><Button type="primary" onClick={this.handleQuery}>{language.OPFindHospitalizedBSAdmhandleQuery && language.OPFindHospitalizedBSAdmhandleQuery.descripts}</Button></Col>
                    </Card>

                    <Card style={{ marginTop: 10 }} bodyStyle={{ padding: '10px' }}>
                        {/* <PubilcTablePagination param={param} handelThis={this} onRef={this.onRef} /> */}
                        <Table
                            style={{ height: store.getState().tableHeight.y + 100+ 'px' }}
                            dataSource={this.state.tableData}
                            columns={this.state.columns}
                            className='TableA'
                            //rowClassName="editable-row"
                            pagination={false}
                            scroll={{ x: 900, y: store.getState().tableHeight.y +100 }}
                            bordered
                            loading={this.state.loading}
                        />
                        <CommonPagination size="small" current={this.state.page} total={this.state.total} search='search'
                            onShowSizeChange={this.apponShowSizeChange} complete={true} dataSource={this.state.tableData}
                            onChange={this.appPageChange} />
                        <ColumnAuthoritysModel
                            clientWidth='900px'
                            compontName='OPFindHospitalizedBSAdm'
                            configType="C"
                            onRef={this.onColumnRef}
                            reloadData={this.tableList}
                        />
                    </Card>
                </div>
            </div>
        )
    }
}