/*
 * Creator:     ML
 * CreatDate:   2020-02-18
 * descripts：  黑名单配置
 * */
import React from 'react';
import { Form, Input, Select, Button, message, Row, Icon, Divider, Popconfirm, DatePicker, Table, Col, Card } from 'antd';
import { $http } from 'containers/config/https';
import moment from 'moment';
import store from 'store';
import './style/index.less';
import CommonPagination from 'components/pagination';
import BlackListConfigDetail from './component/BlackListConfigDetail';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';


const { Option } = Select;

class BlackListConfiguration extends React.Component {
    constructor() {
        super();
        var userData = JSON.parse(sessionStorage.getItem('userData'))
        this.state = {
            userData: userData,
            tableData: [], // 列表数据
            total: 0,
            page: 1,
            pageSize: 50,
            size: "small",
            search: "",
            ICD: "",
            diaID: ""
        };
    }

    componentDidMount() {
        document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 124 + 'px';
        if (this.state.size == "small") {
            this.setState({
                pageSize: 10
            })
        } else {
            this.setState({
                pageSize: 50
            })
        }
        this.getFindDate()
        this.getHopSelect()
        this.getMainTableColumns()
    }

    componentWillReceiveProps(nextProps) {

    }
    //调子组件
    onAttRef = (ref) => {
        this.childAtt = ref
    }
    //国际化
    setMessage(name, message) {
        return this.state.localeProvider ? this.state.localeProvider[name][message] : ''
    }
   //动态表头
   onColumnRef = (ref) => {
    this.columnRef = ref;
}
// 获取主列表columns
getMainTableColumns = () => {
    let data = {
        params: [{
            compontName: "BlacklistConfigurationTableInt",
            reactCode: ['BlacklistConfigurationTableInt'],
            type: 'C',
        }]
    }
    $http.post('urlS', this, {
        code: '01040073',
        data: data,
        success: function (res) {
            if (+res.errorCode === 0) {
                let arr = res.result && res.result.C.length > 0 ? JSON.parse(JSON.stringify(res.result.C)).map(item => {
                    return {
                        align: item.align,
                        widthNum: parseInt(item.width),
                        width: item.width,
                        dataIndex: item.dataIndex,
                        title: <div onDoubleClick={() => this.columnRef.onOpenModel()}>{this.state.userData.language == 'EN' ? item.descripts:item.title}</div>,
                        display: item.display,
                        key: item.seqNo,
                    }
                }) : '';
                let filterArr = arr.filter(function (item) { return item.display === 'Y' });
                this.setState({ localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN,columns: filterArr, widthTotal: res.totalWidth })
                // console.log("res.totalWidth",res.totalWidth)
            }
        }
    })
}
    // 点击查询
    handleQuery = () => {
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.setState({
                    values,
                }, () => {
                    this.onShowSizeChange("1", this.state.pageSize)
                    this.newAdd()
                })
            }
        })
    }
    //获取医院下拉框
    getHopSelect = () => {
        let data = {
            params: [{
                hospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
                logonGroupID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).groupID : "",
                logonHospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
            }],
        }
        $http.post('urlS', this, {
            code: '01040001',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        hospArr: res.result
                    })
                }
            }
        })
    }
    //查询
    getFindDate = (values, e) => {
        let { page, pageSize, } = this.state;
        let data = {
            params: [{
                hospID: values&&values.hospID||"",
            }],
            pagination: [{
                pageSize: pageSize,
                currentPage: page,
            }]
        }
        $http.post('urlS', this, {
            code: '03050181',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        search: e,
                        tableData: res.result.data,
                        total: res.result.TotalCount,
                    })
                }
            }
        })
    }
    // 修改page和pageSize 跳转至---
    onChange = (e) => {
        this.setState({ page: e, pageSize: this.state.pageSize, }, () => {
            // 重新调用查询方法
            let { values } = this.state;
            this.getFindDate(values);
        })
    }
    //下拉选择页数
    onShowSizeChange = (page, pageSize) => {
        // console.log("page",page,"pagesize",pageSize)
        this.setState({ page: 1, pageSize: pageSize }, () => {
            // 重新调用查询方法
            let { values } = this.state;
            this.getFindDate(values, "search");
        })
    }
    // 显示分页总条数
    showTotal(total) {
        return `共 ${total} 条`;
    }

    //点击Row
    onClickRow = (record) => {
        return {
            onClick: () => {
                this.setState({
                    parms: record,
                })
                this.childAtt.clearInfo()
                if (this.state.rowIds === "") {
                    this.setState({
                        rowIds: record.ID,
                    })
                    this.childAtt.getInfo(record)
                } else {
                    if (this.state.rowIds !== record.ID) {
                        this.setState({
                            rowIds: record.ID,
                        });
                        this.childAtt.getInfo(record)
                    } else {
                        this.newAdd()
                    }
                }
            },
        };
    }
    newAdd = ()=>{
        this.setState({
            rowIds: "",
        });
        this.childAtt.clearInfo()
    }
    //点击查询后 表格行
    setRowClassNames = (record) => {
        return record.ID === this.state.rowIds ? 'clickRowStyle' : '';
    }
    render() {
        const { tableData, total, page, rowData,columns } = this.state;

        const { getFieldDecorator } = this.props.form;
        //医院
        let hosOp = []
        this.state.hospArr && this.state.hospArr.map((item) => {
            hosOp.push(<Option key={item.id}  value={String(item.id)}>{item.descripts}</Option>)
        })
        const formLayout = {
            labelCol: { span: 5 },
            wrapperCol: { span: 14 },
        }
        return (
            <div className="card-dom">
                <div>
                    <Form layout={'horizontal'}>
                        <Row>
                            <Col span={4}>
                                <Form.Item  label={this.state.localeProvider && this.state.localeProvider.BlacklistConfigurationTableIntHospital ? this.setMessage('BlacklistConfigurationTableIntHospital', 'descripts') : '医院'} {...formLayout}>
                                    {getFieldDecorator('hospID', {
                                        rules: [{ required: false, message: '' }],
                                    })(
                                        <Select
                                            allowClear
                                        >
                                            {hosOp}
                                        </Select>
                                    )}
                                </Form.Item>
                            </Col>
                            <Col span={4}>
                                    <Button style={{ margin: '3px 0 0 10px' }} type="primary" onClick={this.handleQuery}>{ this.state.localeProvider && this.state.localeProvider.BlacklistConfigurationTableIntSearch ? this.setMessage('BlacklistConfigurationTableIntSearch', 'descripts') : '查询'}</Button>
                            </Col>
                        </Row>
                    </Form>
                </div>
                <Row>
                    <Col span={8}>
                        <Card>
                            <Table
                                columns={columns}
                                dataSource={this.state.tableData}
                                pagination={false}
                                rowKey={record => record.key}
                                onRow={this.onClickRow}
                                rowClassName={this.setRowClassNames}
                                bordered
                                style={{ height: store.getState().tableHeight.y + 126 +'px', overflow: "auto" }}
                            />
                            <CommonPagination size={this.state.size} current={this.state.page} total={this.state.total} search={this.state.search}
                                onShowSizeChange={this.onShowSizeChange.bind(this)} complete={true} dataSource={this.state.tableData}
                                onChange={this.onChange.bind(this)} />

                        </Card>
                    </Col>
                    <Col span={16} style={{ marginLeft: "-1px" }}>
                        <Card>
                            <BlackListConfigDetail localeProvider={this.state.localeProvider} newAdd={this.newAdd} onAttRef={this.onAttRef} handleQuery={this.handleQuery}></BlackListConfigDetail>
                        </Card>
                    </Col>
                </Row>
                <ColumnAuthoritysModel
                    clientWidth='800px'
                    compontName='BlacklistConfigurationTableInt'
                    configType="C"
                    onRef={this.onColumnRef}
                    reloadData={this.getMainTableColumns}
                />
            </div>
        )
    }
};
const BlackListConfig = Form.create()(BlackListConfiguration);
export default BlackListConfig;