/**
 * 体征需测规则列表
 */
import React from 'react';
import {
    Input, Row, Col, Button, Spin, Divider, Popconfirm, message, Icon, Select
} from 'antd';
import { $http } from '../../../containers/config/https';
import PubilcTablePagination from '../../common/PubilcTablePagination.jsx'; // 公共table组件
import AddRule from './AddRule.jsx';


// 组件const
const { Option } = Select;
let user;
class NeedTestRule extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            hospID: "",
            code: "",
            descripts: "",
            ruleInfo: {},
            tableData: [],
            hospital: [],
            // 表格数据总条数
            total: 0,
            page: 1,
            pageSize: 20,
            flag: "",
            title: "",
        }
        this.columns = [
            {
                key: 1,
                title: '代码',
                dataIndex: 'ruleCode',
                align: "center",
                width: '7%'
            },
            {
                key: 2,
                title: '描述',
                dataIndex: 'ruleDesc',
                align: "center",
                width: '7%'
            },
            {
                key: 3,
                title: '类型',
                dataIndex: 'ruleType',
                align: "center",
                width: '5%',
            },
            {
                key: 4,
                title: '体征项目',
                dataIndex: 'ruleItemDesc',
                align: "center",
                width: '12%',
                render: (text, record) => (
                    record.ruleItemDesc.length > 1 ?
                        <Select defaultValue={record.ruleItemDesc[0].desc} style={{ width: '100%' }}>
                            {
                                record.ruleItemDesc.map((item) => {
                                    return (
                                        <Option disabled key={item.id}>{item.desc}
                                        </Option>
                                    )
                                })
                            }
                        </Select> : record.ruleItemDesc[0] ? record.ruleItemDesc[0].desc : ""
                ),
            },
            {
                key: 5,
                title: '条件项目',
                dataIndex: 'conditionItemDesc',
                align: "center",
                width: '8%',
            },
            {
                key: 6,
                title: '条件',
                dataIndex: 'condition',
                align: "center",
                width: '8%',
            },
            {
                key: 7,
                title: '当日时间点',
                dataIndex: 'todayTimes',
                align: "center",
                width: '10%',
                render: (text, record) => (
                    record.todayTimes.length > 1 ?
                        <Select defaultValue={record.todayTimes[0]} style={{ width: '100%' }}>
                            {
                                record.todayTimes.map((item) => {
                                    return (
                                        <Option disabled key={item}>{item}
                                        </Option>
                                    )
                                })
                            }
                        </Select> : record.todayTimes[0] ? record.todayTimes[0] : ""
                ),
            },
            {
                key: 8,
                title: '非当日时间点',
                dataIndex: 'otherTimes',
                align: "center",
                width: '10%',
                render: (text, record) => (
                    record.otherTimes.length > 1 ?
                        <Select defaultValue={record.otherTimes[0]} style={{ width: '100%' }}>
                            {
                                record.otherTimes.map((item) => {
                                    return (
                                        <Option disabled key={item}>{item}
                                        </Option>
                                    )
                                })
                            }
                        </Select> : record.otherTimes[0] ? record.otherTimes[0] : ""
                ),
            },
            {
                key: 9,
                title: '测量天数',
                dataIndex: 'ruleDays',
                align: "center",
                width: '5%',

            },
            {
                key: 10,
                title: '生效科室',
                dataIndex: 'locDesc',
                align: "center",
                width: '10%',
                render: (text, record) => (
                    record.locDesc.length > 1 ?
                        <Select defaultValue={record.locDesc[0].desc} style={{ width: '100%' }}>
                            {
                                record.locDesc.map((item) => {
                                    return (
                                        <Option disabled key={item.id}>{item.desc}
                                        </Option>
                                    )
                                })
                            }
                        </Select> : record.locDesc[0] ? record.locDesc[0].desc : ""
                ),
            },
            {
                key: 11,
                title: '不可用科室',
                dataIndex: 'shieldLocDesc',
                align: "center",
                width: '10%',
                render: (text, record) => (
                    record.shieldLocDesc.length > 1 ?
                        <Select defaultValue={record.shieldLocDesc[0].desc} style={{ width: '100%' }}>
                            {
                                record.shieldLocDesc.map((item) => {
                                    return (
                                        <Option disabled key={item.id}>{item.desc}
                                        </Option>
                                    )
                                })
                            }
                        </Select> : record.shieldLocDesc[0] ? record.shieldLocDesc[0].desc : ""
                ),
            },
            {
                title: '操作',
                key: 12,
                align: "center",
                width: 130,
                render: (text, record) => (
                    <span>
                        <Icon type="edit" style={{ color: '#108EE9' }}></Icon>
                        <span className='span' onClick={() => this.handleCompile(record)}>编辑</span>
                        {/* 分割线 */}
                        <Divider type="vertical" />
                        <Icon type="delete" style={{ color: 'rgba(240,65,52,1)' }}></Icon>
                        <Popconfirm title="删除不可恢复，你确定要删除吗?" onConfirm={() => this.handleDelete(record)} >
                            <span className='span' style={{ color: 'rgba(240,65,52,1)' }}>删除</span>
                        </Popconfirm>

                    </span>
                ),
            }
        ]
        user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
    }
    componentDidMount() {
        // 获取医院列表
        this.getHospital();
        // 初始化列表数据
        this.handleQuery(user.hospID)
    }
    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }
    handleQuery = (hospID) => {
        // 数据加载成功关闭loading
        if (hospID.type !== undefined) {
            hospID = this.state.hospID;
        }
        let { page, pageSize } = this.state;
        let data = {
            "params": [{
                hospID: hospID,    //医院ID
                code: this.state.code,
                descripts: this.state.descripts,
            }],
            pagination: [{
                pageSize: pageSize,
                currentPage: page,
            }]
        };
        $http.post('urlADS', this, {
            code: "04040018",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    this.setState({ tableData: res.result.rule, total: res.result.TotalCount, hospID: hospID })
                }
            }
        });
        this.commonChild.handleLoading()
    }

    // 提供修改page和pageSize的回调函数
    compilePage(page, pageSize) {
        // 修改page，或者pageSize
        this.setState({ page: page, pageSize: pageSize }, () => {
            // 重新调用查询方法
            this.handleQuery(this.state.hospID);
        })
    }
    // 查询条件 --- 项目代码
    onChangeCode = (e) => {
        this.setState({ code: e.target.value })
    }
    // 查询条件 --- 医院
    onChangehosp = (value) => {
        this.handleQuery(value);
    }

    // 查询条件 --- 项目描述
    onChangeDescripts = (e) => {
        this.setState({ descripts: e.target.value })
    }
    // 重置信息
    resetRuleInfo() {
        this.setState({ ruleInfo: {} })
    }
    // 编辑信息
    handleCompile = (record) => {
        // 打开抽屉
        this.child.showDrawer();
        // 初始化数据
        this.setState({ ruleInfo: record, flag: 'edit', title: '编辑' })
    }
    // 删除信息
    handleDelete = (record) => {
        let data = {
            "params": [{
                userID: user.userID,    //医院ID
                ID: record.ruleID,
            }]
        };
        $http.post('urlADS', this, {
            code: "04040019",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    message.success("删除成功!");
                    this.handleQuery(this.state.hospID);
                }
            }
        });

    }
    //新增配置
    handleAdd = () => {
        // 打开抽屉
        this.child.showDrawer();
        // 初始化数据
        this.setState({ flag: 'add', title: '新增' })
    }
    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }
    // 调用公共组件的连接事件
    onRef = (ref) => {
        this.child = ref
    }
    handleOnRef = (ref) => {
        this.commonChild = ref
    }

    ///获取医院列表 (本院及下级医院)
    getHospital = () => {
        let data = {
            params: [{
                hospID: user.hospID,
                logonGroupID: user.groupID,
                logonHospID: user.hospID,
            }]
        }

        $http.post('urlDeault', this, {
            code: "01040001",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    this.setState({ hospital: res.depDoctor })
                } else {
                    message.error('保存失败' + res.errorMessage);
                }
            }
        })
    }
    render() {
        // 组件所需参数
        const param = {
            // 表头配置
            columns: this.columns,
            // 表格数据
            data: this.state.tableData,
            // 数据总条数
            total: this.state.total,
            // 数据页数
            page: this.state.page,
            // 表格的宽度
            x: 900
        }
        const hospital = [];
        this.state.hospital.map((item) => {
            hospital.push(
                <Option key={item.id} value={item.id + ""} title={item.descspell} >{item.descripts}</Option>
            )
        })
        return (
            <div style={{ width: '100%', height: '100%' }}>
                <div className="table-operations">
                    <Row style={{ minHeight: '80px', padding: '0 24px 24px 24px' }}>
                        <Col span={20}>
                            医院：<Select style={{ margin: '23px 15px 0 0', width: '180px' }} defaultValue={user.hospID + ""} onChange={this.onChangehosp}>
                                {hospital}
                            </Select>
                            规则代码：<Input allowClear={true} onPressEnter={this.handleQuery} value={this.state.code} onChange={this.onChangeCode} placeholder="项目代码(Enter)" style={{ width: '160px', margin: '23px 15px 0 0' }} />
                            规则描述：<Input allowClear={true} onPressEnter={this.handleQuery} value={this.state.descripts} onChange={this.onChangeDescripts} placeholder="项目描述(Enter)" style={{ width: '160px', margin: '23px 15px 0 0' }} />
                            <Button onClick={this.handleQuery} style={{ margin: '23px 15px 0 0', background: 'rgba(255,255,255,1)' }}>查询</Button>
                        </Col>
                        <Col span={4} style={{ textAlign: 'right' }}>
                            <Button icon="plus" onClick={this.handleAdd} style={{ background: 'rgba(247,128,93,1)', color: '#fff', margin: '23px 15px 0 0' }}>新增规则</Button>
                        </Col>
                    </Row>
                </div>
                <div style={{ widht: '100%', height: '10px', background: '#f0f2f5' }}></div>
                <div style={{ padding: 24 }}>
                    {/* handelThis将夫组件的属性与方法给子组件访问 */}
                    <PubilcTablePagination param={param} handelThis={this} onRef={this.handleOnRef} />
                </div>
                <AddRule onRef={this.onRef} handelThis={this} ruleInfo={this.state.ruleInfo} hospID={this.state.hospID} flag={this.state.flag} title={this.state.title} />
            </div>

        );
    }
}

export default NeedTestRule;
