//互联网科室维护
import React from 'react';
import { Table, Form, Select, Row, Col, Card, Input, Button, message, DatePicker, Modal, Icon, Divider, Popconfirm } from 'antd';
import { $http } from '../../containers/config/https';
import moment from 'moment';
import store from '../../store';
import './style/style.less';
import CommonPagination from 'components/pagination/index';

const { Option } = Select;
const dateFormat = 'YYYY-MM-DD'; //定义全局日期格式
const formItemLayout = {    // label 和input宽度
    labelCol: { span: 8 },
    wrapperCol: { span: 16 },
};

class OPHBHLWtinsdepartment extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false, //加载状态标志
            visible: false, //弹窗显示状态标志
            motalTitle: '', //弹窗标题
            modalState: '', //弹窗点击提交时判断是否修改或增加标志
            total: 0,  // 表格数据总条数
            page: 1, //当前页数
            pageSize: 10, //每页显示条数
            tableData: [],
            tableData2: [],
            hospID: '', //医院ID
            chanceID: '', //判断选中id
            departmentCodeData: [], //标准科室代码下拉框数据
        };
        this.columns2 = [
            {
                title: '代码',
                dataIndex: 'code',
                key: 'code',
                width: '100px',
                align: "center",
            },
            {
                title: '描述',
                dataIndex: 'descripts',
                key: 'descripts',
                width: '100px',
                align: "center",
            },
            {
                title: '申请',
                dataIndex: 'uploadflag',
                key: 'uploadflag',
                width: '100px',
                align: "center",
                render: (text, record) => (
                    <span>
                        {record.uploadflag === 'Y' ? <span style={{ color: '#00aeef' }}>已申请</span> : <span style={{ color: '#c00' }}>未申请</span>}
                    </span>
                ),
            },
        ];
        this.columns = [
            // {
            //     title: '机构标识',
            //     dataIndex: 'JGDM',
            //     key: 'JGDM',
            //     width: '100px',
            //     align: "center",
            // },
            {
                title: '科室编码',
                dataIndex: 'KSBM',
                key: 'KSBM',
                width: '100px',
                align: "center",
            },
            {
                title: '科室名称',
                dataIndex: 'KSMC',
                key: 'KSMC',
                width: '100px',
                align: "center",
            },
            {
                title: '标准科室代码',
                dataIndex: 'BZKSDM',
                key: 'BZKSDM',
                width: '100px',
                align: "center",
            },
            {
                title: '医保局代码',
                dataIndex: 'YBJDM',
                key: 'YBJDM',
                width: '100px',
                align: "center",
            },
            {
                title: '科室简介',
                dataIndex: 'KSJS',
                key: 'KSJS',
                width: '100px',
                align: "center",
            },
            {
                title: '数据生成日期时间',
                dataIndex: 'SJSCSJ',
                key: 'SJSCSJ',
                width: '100px',
                align: "center",
            },
            {
                title: '填报日期',
                dataIndex: 'TBRQSJ',
                key: 'TBRQSJ',
                width: '100px',
                align: "center",
            },
            {
                title: '撤销标志',
                dataIndex: 'CXBZ',
                key: 'CXBZ',
                width: '100px',
                align: "center",
            },
            {
                title: '上传标志',
                dataIndex: 'Uploadflag',
                key: 'Uploadflag',
                width: '100px',
                align: "center",
            },
            {
                title: '操作',
                key: 'op',
                align: "center",
                width: '150px',
                render: (text, record) => (
                    <span>
                        <span className='span' onClick={() => this.showModal(record)}><Icon type="edit" />修改</span>
                        <Divider type="vertical" />
                        <Popconfirm
                            title="确定要删除吗?"
                            onConfirm={this.delConfirm}
                            okText="是"
                            cancelText="否"
                        >
                            <span style={{ color: '#f00' }} className='span' onClick={() => this.setState({ delID: record.id })}><Icon type="delete" />删除</span>
                        </Popconfirm>
                        <Divider type="vertical" />
                        <span style={{ cursor: 'pointer' }} onClick={() => this.uploadDepartment(record)}><Icon type="to-top" />上传</span>
                    </span>
                ),
            },
        ];
    }


    componentDidMount() {
        //初始化列表数据
        this.handleQuery();
    }

    //初始化列表数据
    handleQuery = () => {
        let { } = this.state;
        let data = {}
        data = {
            params: [{
            }]
        }
        $http.post('urlS', this, {
            code: '05100296',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.result.data) {
                    this.setState({ tableData2: res.result.data, loading: false })
                } else {
                    this.setState({ tableData2: [], loading: false })
                }
            }
        })
    }

    // 页码改变的回调，参数是改变后的页码及每页条数
    appPageChange = (page, pageSize) => {
        this.setState({
            page: page,
            pageSize: pageSize,
        }, () => {
            // 重新调用查询方法
            let data = {}
            data = {
                params: [{
                    hospID: this.state.hospID,
                }],
                pagination: [{
                    pageSize: pageSize,
                    currentPage: page,
                    sortColumn: 'code',
                    sortOrder: 'asc'
                }]
            }
            $http.post('urlS', this, {
                code: '05100301',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({ tableData: res.result.data, total: res.result.totalCount, loading: false })
                    } else {
                        this.setState({ tableData: [], total: 0, loading: false })
                    }
                }
            })
            this.setState({ loading: false, visible: false });
        });
    }

    // 条数改变的回调
    apponShowSizeChange = (current, size) => {
        this.setState({
            page: current,
            pageSize: size,
        }, () => {
            // 重新调用查询方法
            let data = {}
            data = {
                params: [{
                    hospID: this.state.hospID,
                }],
                pagination: [{
                    pageSize: pageSize,
                    currentPage: page,
                    sortColumn: 'code',
                    sortOrder: 'asc'
                }]
            }
            $http.post('urlS', this, {
                code: '05100301',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({ tableData: res.result.data, total: res.result.totalCount, loading: false })
                    } else {
                        this.setState({ tableData: [], total: 0, loading: false })
                    }
                }
            })
            this.setState({ loading: false, visible: false });
        });
    }

    //弹窗显示
    showModal = (params) => {
        if (params === 'add') {
            this.setState({
                visible: true,
                motalTitle: "新增",
                updateID: "",
                modalState: 'add'
            });
            this.props.form.setFieldsValue({
                KSBM: '', //科室编码
                KSMC: '', //科室名称
                BZKSDM: '', //标准科室代码
                YBJDM: '', //医保局代码
                KSJS: '', //科室简介
                CXBZ: '', //撤销标志
            });
        } else {
            this.setState({
                updateID: params.id,
                visible: true,
                motalTitle: "修改",
                modalState: 'update'
            });
            this.props.form.setFieldsValue({
                KSBM: params.KSBM, //科室编码
                KSMC: params.KSMC, //科室名称
                BZKSDM: params.BZKSDM, //标准科室代码
                YBJDM: params.YBJDM, //医保局代码
                KSJS: params.KSJS, //科室简介
                CXBZ: params.CXBZ, //撤销标志
            });
        }
    };

    //弹窗点击取消
    handleCancel = () => {
        this.setState({ visible: false, });
    };

    //弹窗点击确定
    handleOk = () => {
        let { page, pageSize } = this.state;
        this.props.form.validateFields((err, values) => {
            if (err) {
                return;
            }
            let data = {};
            let code = ''
            if (this.state.modalState === 'add') {
                code = '05100302';
            } else if (this.state.modalState === 'update') {
                code = '05100303';
            }
            data = {
                params: [{
                    id: this.state.updateID,
                    userID: JSON.parse(window.sessionStorage.userData).userID,
                    hospID: this.state.hospID,
                    KSBM: values.KSBM,
                    KSMC: values.KSMC,
                    BZKSDM: values.BZKSDM,
                    YBJDM: values.YBJDM,
                    KSJS: values.KSJS,
                    CXBZ: values.CXBZ,
                }]
            }
            $http.post('urlS', this, {
                code: code,
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        message.success(res.errorMessage);
                        //成功之后继续查询
                        let data = {}
                        data = {
                            params: [{
                                hospID: this.state.hospID,
                            }],
                            pagination: [{
                                pageSize: pageSize,
                                currentPage: page,
                                sortColumn: 'code',
                                sortOrder: 'asc'
                            }]
                        }
                        $http.post('urlS', this, {
                            code: '05100301',
                            data: data,
                            success: function (res) {
                                if (+res.errorCode === 0) {
                                    this.setState({ tableData: res.result.data, total: res.result.totalCount, loading: false })
                                } else {
                                    this.setState({ tableData: [], total: 0, loading: false })
                                }
                            }
                        })
                        this.setState({ loading: false, visible: false });
                    }
                }
            })
        })
    };

    //删除确认
    delConfirm = (e) => {
        this.handleDel();
    }

    //删除操作
    handleDel = () => {
        let { page, pageSize } = this.state;
        let data = {
            params: [{
                id: this.state.delID,
                userID: JSON.parse(window.sessionStorage.userData).userID
            }]
        };
        $http.post('urlS', this, {
            code: "05100304",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    message.success(res.errorMessage);
                    //继续查询
                    let data1 = {}
                    data1 = {
                        params: [{
                            hospID: this.state.hospID,
                        }],
                        pagination: [{
                            pageSize: pageSize,
                            currentPage: page,
                            sortColumn: 'code',
                            sortOrder: 'asc'
                        }]
                    }
                    $http.post('urlS', this, {
                        code: '05100301',
                        data: data1,
                        success: function (res) {
                            if (+res.errorCode === 0 && res.result.data) {
                                this.setState({ tableData: res.result.data, loading: false })
                            } else {
                                this.setState({ tableData: [], loading: false })
                            }
                        }
                    })
                    this.setState({ loading: false, visible: false });
                }
            }
        });
    }

    onClickRow = (record) => {
        return {
            onClick: () => {
                this.OPHospHLW(record)
            },
        }
    }

    //点击查询互联网机构信息
    OPHospHLW = (record) => {
        let { page, pageSize } = this.state;
        if (record.id === this.state.chanceID) {
            this.setState({ tableData: [], chanceID: '', departmentCodeData: [] })
        } else {
            this.setState({ hospID: record.id, chanceID: record.id })
            //查找医院下科室列表
            let data = {}
            data = {
                params: [{
                    hospID: record.id,
                }],
                pagination: [{
                    pageSize: pageSize,
                    currentPage: page,
                    sortColumn: 'code',
                    sortOrder: 'asc'
                }]
            }
            $http.post('urlS', this, {
                code: '05100301',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({ tableData: res.result.data, total: res.result.totalCount, loading: false })
                    } else {
                        this.setState({ tableData: [], total: 0, loading: false })
                    }
                }
            })
            //查询医院科室代码下拉框数据
            data = {
                params: [{
                    hospID: record.id,
                }]
            }
            let departmentCodeData = []
            $http.post('urlS', this, {
                code: '05100306',
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0 && res.result.data) {
                        for (var i = 0; i < res.result.data.length; i++) {
                            departmentCodeData.push(<Option value={String(res.result.data[i].id)} key={i + "Code"} >{res.result.data[i].descripts}:{res.result.data[i].code}</Option>)
                        }
                        this.setState({ departmentCodeData: departmentCodeData })
                    } else {
                        this.setState({ departmentCodeData: [], loading: false })
                    }
                }
            })
        }
    }

    setRowClassName = (record) => {
        return record.id === this.state.chanceID ? 'clickRowStyle' : '';
    }

    //医院代码改变，获取医保代码、科室简介、互联网科室编码、互联网科室名称
    changeKSBM = (locID) => {
        let data = {}
        data = {
            params: [{
                hospID: this.state.hospID,
                locID: locID
            }]
        }
        $http.post('urlS', this, {
            code: '05100307',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.props.form.setFieldsValue({
                        YBJDM: res.result.YBJDM
                    });
                } else {
                    this.props.form.setFieldsValue({
                        YBJDM: ''
                    });
                }
            }
        })
        //获取科室简介、标准科室编码、互联网科室名称
        $http.post('urlS', this, {
            code: '05100325',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.props.form.setFieldsValue({
                        KSJS: res.result.KSJS,
                        BZKSDM: res.result.BZKSDM,
                        KSMC: res.result.KSMC
                    });
                } else {
                    this.props.form.setFieldsValue({
                        KSJS: '',
                        BZKSDM: '',
                        KSMC: ''
                    });
                }
            }
        })
    }

    //互联网科室上传
    uploadDepartment = (record) => {
        let { page, pageSize } = this.state;
        let data = {};
        data = {
            params: [{
                departmentID: record.id,
                hospID: this.state.hospID,
                userID: JSON.parse(window.sessionStorage.userData).userID,
            }]
        }
        $http.post('urlS', this, {
            code: '05100338',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    message.success(res.errorMessage);
                    this.setState({ loading: false, visible: false });
                }
            }
        })
        //更新列表
        data = {
            params: [{
                hospID: this.state.hospID,
            }],
            pagination: [{
                pageSize: pageSize,
                currentPage: page,
                sortColumn: 'code',
                sortOrder: 'asc'
            }]
        }
        $http.post('urlS', this, {
            code: '05100301',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({ tableData: res.result.data, total: res.result.totalCount, loading: false })
                } else {
                    this.setState({ tableData: [], total: 0, loading: false })
                }
            }
        })
    };

    render() {
        let { departmentCodeData } = this.state;
        const { getFieldDecorator } = this.props.form;
        return (
            <div>
                <div style={{ width: '100%', height: '100%' }}>
                    <Row style={{ minHeight: '80px' }}>
                        <Col span={6}>
                            <Card title={<span>互联网医院列表</span>}>
                                <div>
                                    <Table
                                        dataSource={this.state.tableData2}
                                        rowClassName={this.setRowClassName}
                                        columns={this.columns2}
                                        pagination={false}
                                        bordered
                                        rowKey="ID"
                                        onRow={this.onClickRow}
                                        style={{ height: store.getState().tableHeight.y + 137 + 'px' }}
                                        scroll={{ y: store.getState().tableHeight.y, x: 300 }}
                                    />
                                </div>
                            </Card>
                        </Col>
                        <Col span={18}>
                            <Card title={<span>互联网医院科室信息</span>}>
                                <div style={{ width: '100%', height: '100%' }}>
                                    <Row style={{ padding: '0px 0px 24px 0px' }}>
                                        <Col span={2} style={{ textAlign: 'center' }}>
                                            <Button type="primary" onClick={() => this.showModal('add')} icon="plus" style={{ background: '#F7805D', color: '#FFFFFF', borderColor: '#F7805D' }} >
                                                新增
                                            </Button>
                                            <Form layout="inline">
                                                <Modal
                                                    visible={this.state.visible}
                                                    title={this.state.motalTitle}
                                                    onOk={() => this.handleOk()}
                                                    onCancel={this.handleCancel}
                                                    width='600px'
                                                    footer={[
                                                        <Button key="back" onClick={this.handleCancel}>返回</Button>,
                                                        <Button key="submit" type="primary" loading={this.state.loading} onClick={this.handleOk}>提交</Button>,
                                                    ]}
                                                >
                                                    <Row>
                                                        <Col span={24}>
                                                            <Form.Item label="医院科室编码" {...formItemLayout}>
                                                                {getFieldDecorator("KSBM", {
                                                                    initialValue: null,
                                                                    rules: [{ required: true, message: '科室编码不能为空' }]
                                                                })(
                                                                    <Select
                                                                        style={{ width: '220px' }}
                                                                        allowClear
                                                                        onChange={this.changeKSBM}
                                                                        placeholder="输入进行搜索"
                                                                        showSearch
                                                                        optionFilterProp="children"
                                                                    >
                                                                        {departmentCodeData}
                                                                    </Select>
                                                                )}
                                                            </Form.Item>
                                                        </Col>
                                                        <Col span={24}>
                                                            <Form.Item label="医院科室名称" {...formItemLayout}>
                                                                {getFieldDecorator("KSMC", {
                                                                    initialValue: null,
                                                                    rules: [{ required: true, message: '科室名称不能为空' }]
                                                                })(
                                                                    <Input style={{ width: '220px' }} allowClear />
                                                                )}
                                                            </Form.Item>
                                                        </Col>
                                                        <Col span={24}>
                                                            <Form.Item label="标准科室代码" {...formItemLayout}>
                                                                {getFieldDecorator("BZKSDM", {
                                                                    initialValue: null,
                                                                    rules: [{ required: true, message: '代码不能为空' }]
                                                                })(
                                                                    <Input style={{ width: '220px' }} allowClear />
                                                                )}
                                                            </Form.Item>
                                                        </Col>
                                                        <Col span={24}>
                                                            <Form.Item label="医保局代码" {...formItemLayout}>
                                                                {getFieldDecorator("YBJDM", {
                                                                    initialValue: null,
                                                                    rules: [{ required: true, message: '代码不能为空' }]
                                                                })(
                                                                    <Input style={{ width: '220px' }} allowClear />
                                                                )}
                                                            </Form.Item>
                                                        </Col>
                                                        <Col span={24}>
                                                            <Form.Item label="科室简介" {...formItemLayout}>
                                                                {getFieldDecorator("KSJS", {
                                                                    initialValue: null,
                                                                    rules: [{ required: true, message: '科室简介不能为空' }]
                                                                })(
                                                                    <Input style={{ width: '220px' }} allowClear />
                                                                )}
                                                            </Form.Item>
                                                        </Col>
                                                        {/* <Col span={24}>
                                                            <Form.Item label="数据生成日期时间" {...formItemLayout}>
                                                                {getFieldDecorator("SJSCSJ", {
                                                                    initialValue: undefined,
                                                                    rules: [{ required: true, message: '日期不能为空' }]
                                                                })(
                                                                    <DatePicker
                                                                        style={{ width: '220px' }}
                                                                        format={dateFormat}
                                                                        allowClear
                                                                    />
                                                                )}
                                                            </Form.Item>
                                                        </Col>
                                                        <Col span={24}>
                                                            <Form.Item label="填报日期" {...formItemLayout}>
                                                                {getFieldDecorator("TBRQSJ", {
                                                                    initialValue: undefined,
                                                                    rules: [{ required: true, message: '日期不能为空' }]
                                                                })(
                                                                    <DatePicker
                                                                        style={{ width: '220px' }}
                                                                        format={dateFormat}
                                                                        allowClear
                                                                    />
                                                                )}
                                                            </Form.Item>
                                                        </Col> */}
                                                        <Col span={24}>
                                                            <Form.Item label="撤销标志" {...formItemLayout}>
                                                                {getFieldDecorator("CXBZ", {
                                                                    initialValue: undefined,
                                                                    rules: [{ required: true, message: '日期不能为空' }]
                                                                })(
                                                                    <Select style={{ width: '220px' }} allowClear >
                                                                        <Option value='1' key='CXBZ1' >正常</Option>
                                                                        <Option value='2' key='CXBZ2' >撤销该记录</Option>
                                                                    </Select>
                                                                )}
                                                            </Form.Item>
                                                        </Col>
                                                    </Row>
                                                </Modal>
                                            </Form>
                                        </Col>
                                    </Row>
                                </div>
                                <div>
                                    <Table
                                        dataSource={this.state.tableData}
                                        columns={this.columns}
                                        pagination={false}
                                        bordered
                                        rowKey="ID3"
                                        style={{ height: store.getState().tableHeight.y + 43 + 'px' }}
                                        scroll={{ y: store.getState().tableHeight.y, x: 300 }}
                                    />
                                    <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} />
                                </div>
                            </Card>
                        </Col>
                    </Row>
                </div>
            </div>
        )
    }
}

const OPHBHLWtinsdepartmentForm = Form.create()(OPHBHLWtinsdepartment);
export default OPHBHLWtinsdepartmentForm;