//互联网设备资源信息维护
import React from 'react';
import { Table, Popconfirm, Icon, message, Modal, Form, Divider, Select, Row, Col, Button, Input, DatePicker, InputNumber } from 'antd';
import { $http } from '../../containers/config/https';
import moment from 'moment';
import store from '../../store';

import CommonPagination from 'components/pagination/index';

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

class OPHBHLWtinsdevice extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false, //加载状态标志
            visible: false, //弹窗显示状态标志
            motalTitle: '', //弹窗标题
            modalState: '', //弹窗点击提交时判断是否修改或增加标志
            total: 0,  // 表格数据总条数
            page: 1, //当前页数
            pageSize: 10, //每页显示条数
            tableData: [],
            hospitaData: [], //医院下拉框数据
            updateID: "",    //修改ID
            delID: "",      //删除ID
            hospID: "",
            SBDH: "",
            SBMC: "",
        };
        this.columns = [
            {
                title: '医院',
                dataIndex: 'hosp',
                key: 'hosp',
                width: '100px',
                align: "center",
            },
            {
                title: '设备代号',
                dataIndex: 'SBDH',
                key: 'SBDH',
                width: '100px',
                align: "center",
            },
            {
                title: '设备名称',
                dataIndex: 'SBMC',
                key: 'SBMC',
                width: '100px',
                align: "center",
            },
            {
                title: '设备台数',
                dataIndex: 'TPSBTS',
                key: 'TPSBTS',
                width: '100px',
                align: "center",
            },
            {
                title: '产地',
                dataIndex: 'CDDesc',
                key: 'CDDesc',
                width: '100px',
                align: "center",
            },
            {
                title: '生产厂家',
                dataIndex: 'SCCJ',
                key: 'SCCJ',
                width: '100px',
                align: "center",
            },
            {
                title: '设备型号',
                dataIndex: 'SBXH',
                key: 'SBXH',
                width: '100px',
                align: "center",
            },
            {
                title: '设备参数',
                dataIndex: 'SBCS',
                key: 'SBCS',
                width: '100px',
                align: "center",
            },
            {
                title: '设备类型',
                dataIndex: 'SBLXDesc',
                key: 'SBLXDesc',
                width: '100px',
                align: "center",
            },
            {
                title: '购买日期',
                dataIndex: 'GMRQ',
                key: 'GMRQ',
                width: '100px',
                align: "center",
            },
            {
                title: '用途',
                dataIndex: 'YT',
                key: 'YT',
                width: '100px',
                align: "center",
            },
            {
                title: '设备价值金额/万元',
                dataIndex: 'SBJZJE',
                key: 'SBJZJE',
                width: '100px',
                align: "center",
            },
            {
                title: '购进时新旧情况',
                dataIndex: 'GJSXQKDesc',
                key: 'GJSXQKDesc',
                width: '100px',
                align: "center",
            },
            {
                title: '理论设计寿命/年',
                dataIndex: 'LLSJSM',
                key: 'LLSJSM',
                width: '100px',
                align: "center",
            },
            {
                title: '使用情况',
                dataIndex: 'SYQKDesc',
                key: 'SYQKDesc',
                width: '100px',
                align: "center",
            },
            {
                title: '备注',
                dataIndex: 'BZ',
                key: 'BZ',
                width: '100px',
                align: "center",
            },
            {
                title: '生成日期',
                dataIndex: 'SJSCSJ',
                key: 'SJSCSJ',
                width: '100px',
                align: "center",
            },
            {
                title: '填报日期',
                dataIndex: 'TBRQSJ',
                key: 'TBRQSJ',
                width: '100px',
                align: "center",
            },
            {
                title: '撤销标志',
                dataIndex: 'CXBZDesc',
                key: 'CXBZDesc',
                width: '100px',
                align: "center",
            },
            {
                title: '撤销原因',
                dataIndex: 'FAILREASON',
                key: 'FAILREASON',
                width: '100px',
                align: "center",
            },
            {
                title: '上传标志',
                dataIndex: 'Uploadflag',
                key: 'Uploadflag',
                width: '100px',
                align: "center",
            },
            {
                title: '操作',
                key: 'op',
                align: "center",
                width: '200px',
                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.uploadHLWtinsdevice(record)} ><Icon type="to-top" />上传</span>
                    </span>
                ),
            },
        ];
    }


    componentDidMount() {
        //初始化列表数据
        this.handleQuery();
        //医院下拉框数据
        this.getHospitalData();
    }

    //医院下拉框数据
    getHospitalData = () => {
        var data = {
            params: [{
                logonGroupID: JSON.parse(window.sessionStorage.userData).groupID,
                logonHospID: JSON.parse(window.sessionStorage.userData).hospID,
                dataType: "combo",
            }]
        }
        let hospital = []
        $http.post("urlS", this, {
            code: '05100046',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.Hosp) {
                    for (var i = 0; i < res.Hosp.length; i++) {
                        hospital.push(<Option value={String(res.Hosp[i].id)} key={i + "H"} >{res.Hosp[i].descripts}</Option>)
                    }
                    this.setState({ hospitaData: hospital })
                }
            },
            error: function (err) {
                this.setState({ errMsg: "数据加载失败，请重试！" })
            }
        })
    }


    //初始化列表数据
    handleQuery = () => {
        let { page, pageSize, hospID, SBDH, SBMC } = this.state;
        let data = {}
        data = {
            params: [{
                hospID: hospID,
                SBDH: SBDH,
                SBMC: SBMC
            }],
            pagination: [{
                pageSize: pageSize,
                currentPage: page,
                sortColumn: 'code',
                sortOrder: 'asc'
            }]
        }
        $http.post('urlS', this, {
            code: '05100332',
            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 })
                }
            }
        })
    }

    // 页码改变的回调，参数是改变后的页码及每页条数
    appPageChange = (page, pageSize) => {
        this.setState({
            page: page,
            pageSize: pageSize,
        }, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }

    // 条数改变的回调
    apponShowSizeChange = (current, size) => {
        this.setState({
            page: current,
            pageSize: size,
        }, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }

    //弹窗显示
    showModal = (params) => {
        if (params === 'add') {
            this.setState({
                visible: true,
                motalTitle: "新增",
                updateID: "",
                modalState: 'add'
            });
            this.props.form.setFieldsValue({
                'pubAccount': null,
                'hospID': null,
                'SBDH': null,
                'SBMC': null,
                'TPSBTS': null,
                'CD': null,
                'SCCJ': null,
                'SBXH': null,
                'SBCS': null,
                'SBLX': null,
                'GMRQ': null,
                'YT': null,
                'SBJZJE': null,
                'GJSXQK': null,
                'LLSJSM': null,
                'SYQK': null,
                'BZ': null,
                'CXBZ': null,
                'FAILREASON': null,
                'Uploadflag': null,
            });
        } else {
            this.setState({
                updateID: params.id,
                visible: true,
                motalTitle: "修改",
                modalState: 'update'
            });
            this.props.form.setFieldsValue({
                'hospID': params.hospID.toString(),
                'SBDH': params.SBDH,
                'SBMC': params.SBMC,
                'TPSBTS': params.TPSBTS,
                'CD': params.CD,
                'SCCJ': params.SCCJ,
                'SBXH': params.SBXH,
                'SBCS': params.SBCS,
                'SBLX': params.SBLX,
                'GMRQ': params.GMRQ ? moment(params.GMRQ, 'YYYY-MM-DD') : null,
                'YT': params.YT,
                'SBJZJE': params.SBJZJE,
                'GJSXQK': params.GJSXQK,
                'LLSJSM': params.LLSJSM,
                'SYQK': params.SYQK,
                'BZ': params.BZ,
                'CXBZ': params.CXBZ,
                'FAILREASON': params.FAILREASON,
                'Uploadflag': params.Uploadflag
            });
        }
    };

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

    //弹窗点击确定
    handleOk = () => {
        this.props.form.validateFields((err, values) => {
            if (err) {
                return;
            }
            let data = {};
            let code = '';
            if (this.state.modalState === 'add') {
                code = '05100333';
            } else if (this.state.modalState === 'update') {
                code = '05100334';
            }
            data = {
                params: [{
                    id: this.state.updateID,
                    userID: JSON.parse(window.sessionStorage.userData).userID,
                    hospID: values.hospID,
                    SBDH: values.SBDH,
                    SBMC: values.SBMC,
                    TPSBTS: values.TPSBTS,
                    CD: values.CD,
                    SCCJ: values.SCCJ,
                    SBXH: values.SBXH,
                    SBCS: values.SBCS,
                    SBLX: values.SBLX,
                    GMRQ: values.GMRQ && values.GMRQ != "" ? moment(values.GMRQ).utcOffset(480).format('YYYY-MM-DD') : '',
                    YT: values.YT,
                    SBJZJE: values.SBJZJE,
                    GJSXQK: values.GJSXQK,
                    LLSJSM: values.LLSJSM,
                    SYQK: values.SYQK,
                    BZ: values.BZ,
                    CXBZ: values.CXBZ,
                    FAILREASON: values.FAILREASON,
                }]
            }
            $http.post('urlS', this, {
                code: code,
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        message.success(res.errorMessage);
                        this.handleQuery();
                        this.setState({ loading: false, visible: false });
                    }
                }
            })
        })
    };

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

    //删除操作
    handleDel = () => {
        let data = {
            params: [{
                id: this.state.delID,
                userID: JSON.parse(window.sessionStorage.userData).userID
            }]
        };
        $http.post('urlS', this, {
            code: "05100335",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    message.success(res.errorMessage);
                    this.handleQuery() // 查询
                }
            }
        });
    }

    onChangeHosp = (e) => {
        this.setState({ hospID: e });
    }

    onChangeSBDH = (e) => {
        this.setState({ SBDH: e.target.value })
    }

    onChangeSBMC = (e) => {
        this.setState({ SBMC: e.target.value })
    }

    //互联网设备信息上传
    uploadHLWtinsdevice = (record) => {
        let data = {};
        data = {
            params: [{
                deviceID: record.id,
                userID: JSON.parse(window.sessionStorage.userData).userID,
            }]
        }
        $http.post('urlS', this, {
            code: '05100340',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    message.success(res.errorMessage);
                    this.handleQuery();
                    this.setState({ loading: false, visible: false });
                }
            }
        })
    };

    render() {
        let { hospitaData } = this.state;
        const { getFieldDecorator } = this.props.form;
        return (
            <div>
                <div style={{ width: '100%', height: '100%' }}>
                    <Row style={{ minHeight: '80px', padding: '24px 24px 24px 24px' }}>
                        <Col span={4}>
                            <span>医院：</span>
                            <Select style={{ width: '200px' }} allowClear placeholder="请选择医院" onChange={this.onChangeHosp} >
                                {hospitaData}
                            </Select>
                        </Col>
                        <Col span={4}>
                            <span>设备代号：</span>
                            <Input allowClear placeholder="请输入设备代号" style={{ width: '180px', }} onChange={this.onChangeSBDH} />
                        </Col>
                        <Col span={4}>
                            <span>设备名称：</span>
                            <Input allowClear placeholder="请输入设备名称" style={{ width: '180px', }} onChange={this.onChangeSBMC} />
                        </Col>
                        <Col span={8}></Col>
                        <Col span={2} style={{ textAlign: 'right' }}>
                            <Button type="primary" onClick={() => this.handleQuery()} icon="search" >查询</Button>
                        </Col>
                        <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='1000px'
                                    footer={[
                                        <Button key="back" onClick={this.handleCancel}>返回</Button>,
                                        <Button key="submit" type="primary" loading={this.state.loading} onClick={this.handleOk}>提交</Button>,
                                    ]}
                                >
                                    <Row>
                                        <Col span={12}>
                                            <Form.Item label="医院" {...formItemLayout}>
                                                {getFieldDecorator("hospID", {
                                                    initialValue: null,
                                                    rules: [{ required: true, message: '医院不能为空' }]
                                                })(
                                                    <Select style={{ width: '200px' }} allowClear placeholder="请选择医院">
                                                        {hospitaData}
                                                    </Select>
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="设备代号" {...formItemLayout}>
                                                {getFieldDecorator("SBDH", {
                                                    initialValue: null,
                                                    rules: [{ required: true, message: '设备代号不能为空' }]
                                                })(
                                                    <Input allowClear style={{ width: '200px', }} placeholder="请输入设备代号" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="设备名称" {...formItemLayout}>
                                                {getFieldDecorator("SBMC", {
                                                    initialValue: null,
                                                    rules: [{ required: true, message: '设备名称不能为空' }]
                                                })(
                                                    <Input allowClear style={{ width: '200px', }} placeholder="请输入设备名称" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="设备台数" {...formItemLayout}>
                                                {getFieldDecorator("TPSBTS", {
                                                    initialValue: null
                                                })(
                                                    <InputNumber min={0} />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="产地" {...formItemLayout}>
                                                {getFieldDecorator("CD", {
                                                    initialValue: null,
                                                })(
                                                    <Select style={{ width: '200px' }} allowClear placeholder="请选择产地">
                                                        <Option key="cd1" value='1'>进口</Option>
                                                        <Option key="cd2" value='2'>国产</Option>
                                                        <Option key="cd3" value='3'>合资</Option>
                                                        <Option key="cd9" value='9'>未知</Option>
                                                    </Select>
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="生产厂家" {...formItemLayout}>
                                                {getFieldDecorator("SCCJ", {
                                                    initialValue: null,
                                                })(
                                                    <Input allowClear style={{ width: '200px', }} placeholder="请输入生产厂家" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="设备型号" {...formItemLayout}>
                                                {getFieldDecorator("SBXH", {
                                                    initialValue: null,
                                                })(
                                                    <Input allowClear style={{ width: '200px', }} placeholder="请输入设备型号" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="设备参数" {...formItemLayout}>
                                                {getFieldDecorator("SBCS", {
                                                    initialValue: null,
                                                })(
                                                    <Input allowClear style={{ width: '200px', }} placeholder="请输入设备参数" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="设备类型" {...formItemLayout}>
                                                {getFieldDecorator("SBLX", {
                                                    initialValue: null,
                                                })(
                                                    <Select style={{ width: '200px' }} allowClear placeholder="请选择设备类型">
                                                        <Option key="SBLX1" value='1'>服务器</Option>
                                                        <Option key="SBLX2" value='2'>网络设备</Option>
                                                        <Option key="SBLX3" value='3'>软件</Option>
                                                    </Select>
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="购买日期" {...formItemLayout}>
                                                {getFieldDecorator("GMRQ", {
                                                    initialValue: null,
                                                    rules: [{ required: true, message: '购买日期不能为空' }]
                                                })(
                                                    <DatePicker allowClear style={{ width: '200px', }} placeholder="请选择购买日期" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="用途" {...formItemLayout}>
                                                {getFieldDecorator("YT", {
                                                    initialValue: null,
                                                })(
                                                    <Input allowClear style={{ width: '200px', }} placeholder="请输入用途" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="设备价值金额/万元" {...formItemLayout}>
                                                {getFieldDecorator("SBJZJE", {
                                                    initialValue: 0,
                                                    rules: [{ required: true, message: '金额不能为空' }]
                                                })(
                                                    <InputNumber min={0} />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="购进时新旧情况" {...formItemLayout}>
                                                {getFieldDecorator("GJSXQK", {
                                                    initialValue: null,
                                                })(
                                                    <Select style={{ width: '200px' }} allowClear placeholder="请选择情况">
                                                        <Option key="GJSXQK1" value='1'>新设备</Option>
                                                        <Option key="GJSXQK2" value='2'>二手设备</Option>
                                                    </Select>
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="理论设计寿命/年" {...formItemLayout}>
                                                {getFieldDecorator("LLSJSM", {
                                                    initialValue: 0,
                                                })(
                                                    <InputNumber min={0} />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="使用情况" {...formItemLayout}>
                                                {getFieldDecorator("SYQK", {
                                                    initialValue: null,
                                                    rules: [{ required: true, message: '使用情况不能为空' }]
                                                })(
                                                    <Select style={{ width: '200px' }} allowClear placeholder="请选择情况">
                                                        <Option key="SYQK1" value='1'>启用</Option>
                                                        <Option key="SYQK2" value='2'>未启用</Option>
                                                        <Option key="SYQK3" value='3'>报废</Option>
                                                    </Select>
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="备注" {...formItemLayout}>
                                                {getFieldDecorator("BZ", {
                                                    initialValue: null,
                                                })(
                                                    <Input allowClear style={{ width: '200px', }} placeholder="请输入备注" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="撤销标志" {...formItemLayout}>
                                                {getFieldDecorator("CXBZ", {
                                                    initialValue: null,
                                                })(
                                                    <Select style={{ width: '200px' }} allowClear placeholder="请选择情况">
                                                        <Option key="CXBZ1" value='1'>正常</Option>
                                                        <Option key="CXBZ2" value='2'>撤销该记录</Option>
                                                    </Select>
                                                )}
                                            </Form.Item>
                                        </Col>
                                        <Col span={12}>
                                            <Form.Item label="撤销原因" {...formItemLayout}>
                                                {getFieldDecorator("FAILREASON", {
                                                    initialValue: null,
                                                })(
                                                    <Input allowClear style={{ width: '200px', }} placeholder="请输入撤销原因" />
                                                )}
                                            </Form.Item>
                                        </Col>
                                    </Row>
                                </Modal>
                            </Form>
                        </Col>
                    </Row>
                </div>
                <div style={{ widht: '100%', height: '10px', background: '#f0f2f5' }}></div>
                <div style={{ padding: 24 }}>
                    <Table
                        style={{ height: store.getState().tableHeight.y + 65 + 'px' }}
                        dataSource={this.state.tableData}
                        columns={this.columns}
                        className='TableA'
                        //rowClassName="editable-row"
                        pagination={false}
                        scroll={{ x: 900, y: store.getState().tableHeight.y }}
                        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} />
                </div>
            </div>
        )
    }
}

const OPHBHLWtinsdeviceForm = Form.create()(OPHBHLWtinsdevice);
export default OPHBHLWtinsdeviceForm;