import React, {Component} from "react"
import './index.less'
import {Button, Card, Col, Input, Row, Select, Switch, Table, Tag, message, Icon} from "antd";
const {Option} = Select;
const {TextArea} = Input;
import { $http } from '../../containers/config/https';
import CommonPagination from "../../components/pagination";

//云HIS用户信息
const useData = sessionStorage?.userData ? JSON.parse(sessionStorage?.userData) :[]

/**
 * @ByYQL: 限制类药品提示信息维护
 * Create 2021-7-1
 */
export default class TipsInfoSet extends Component{

    constructor(props) {
        super(props);
        this.state = {
            //搜索输入框内容
            inputobjDesc:{inputCode:'',inputDesc:''},
            //医嘱子类数据
            subCat:[],
            //医嘱子类id
            subCatID:'',
            //是否查看已维护
            tipState:'0',
            //列表数据
            tipStateData:[],
            //当前选中医嘱内容
            tbaleOnPressItem:'',
            //文本输入框内容
            textAreaValue:'',
            //分页器当前页跟当前条数,总数
            pageSize: 10,
            currentPage: 1,
            total:0
        }
    }

    componentDidMount() {
        const {_subCat} = this.state;
        if(!_subCat || _subCat.length===0){
            this.getDescriptsFun();
        }
    }


    //获取医嘱子类
    getDescriptsFun = async () => {
        let data = {
            params: [{
                logonHospID: useData.hospID, logonGroupID: useData.groupID, subCatFlag: "Y", statusFlag: "Y"
            }]
        };
        $http.post('urlADS', this, {
            code: "03010037",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (+errorCode === 0) {
                    this.setState({
                        subCat:res.result?.subCat
                    })
                }else{
                    message.error('请求失败：'+res.errorMessage)
                }
            }
        });
    };

    onShowSizeChange = (current, size) => {
        this.setState({currentPage: current, pageSize: size}, () => {
            // 重新调用查询方法
            const {inputobjDesc,subCatID,tipState} = this.state;
            this.getTipsInfoListFun(inputobjDesc,subCatID,tipState)
        })
    };

    // 药品提示数据列表查询
    getTipsInfoListFun = async (inputobjDesc,subCatID,tipState) => {
        this.setState({loading:true});
        let data = {
            params: [{
                "medCode": inputobjDesc.inputCode,
                "medDesc": inputobjDesc.inputDesc,
                "subCatID": subCatID,
                "tipState": tipState
            }],
            pagination: [{
                "pageSize": this.state.pageSize,
                "currentPage": this.state.currentPage,
                "sortColumn": "",
                "sortOrder": ""
            }]
        };
        $http.post('urlADS', this, {
            code: "pr01002",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (+errorCode === 0) {
                    this.setState({
                        tipStateData:this.tipsListAddKey(res.data),
                        total:res.total
                    })
                }else{
                    message.error('请求失败：'+res.errorMessage)
                }
                this.setState({loading:false})
            }
        });
    };

    //列表添加key健
    tipsListAddKey = (dataList) => {
        const _dataList = dataList;
        for(let i=0;i<_dataList.length;i++){
            _dataList[i].key = _dataList[i].medItemID
        }
        return _dataList
    };

    //药品提示数据更新请求
    updateDrugsTipsFun = async () => {
        let data = {
            params: [{
                "medItemID": this.state.tbaleOnPressItem?.medItemID,
                "tipDesc": this.state.textAreaValue
            }]
        };
        $http.post('urlADS', this, {
            code: "pr01003",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (+errorCode === 0) {
                    message.success('更新成功');
                    const {inputobjDesc,subCatID,tipState} = this.state;
                    this.getTipsInfoListFun(inputobjDesc,subCatID,tipState)
                }else{
                    message.error('请求失败：'+res.errorMessage)
                }
            }
        });
    };


    // 输入框事件
    inputOnChange = (type,e) => {
        let _inputobjDesc = this.state.inputobjDesc;
        if(type === 'code'){
            _inputobjDesc.inputCode = e.target.value
        }else{
            _inputobjDesc.inputDesc = e.target.value
        }
        this.setState({inputobjDesc: _inputobjDesc})
    };

    // 医嘱子类选择器
    onhosSelectChange =(e) => {
        this.setState({subCatID:e})
    };

    // 是否仅查看已经维护的
    switchOnChange = (e) => {
        this.setState({
            tipState: e ? '1' : '0'
        });
    };

    //分页变化监听
    pageChangeFun = (page, pageSize) => {
        this.setState({
            pageSize: pageSize,
            currentPage: page,
        },function () {
            const {inputobjDesc,subCatID,tipState} = this.state;
            this.getTipsInfoListFun(inputobjDesc,subCatID,tipState)
        })
    };

    // 查询
    onClickFindChange = () =>{
        this.setState({
            currentPage: 1
        },function () {
            const {inputobjDesc,subCatID,tipState} = this.state;
            this.getTipsInfoListFun(inputobjDesc,subCatID,tipState)
        });
    };


    //文本域输入框监听
    textAreaOnChange = (e) => {
        this.setState({
            textAreaValue:e.target.value
        })
    };

    // 更新
    onClickTipsUpdate = () => {
        this.updateDrugsTipsFun();
    };

    render() {
        // 表格字段设置
        const columns = [
            {title: '代码', dataIndex: 'medCode', key: 'medCode'},
            {title: '描述', dataIndex: 'medDesc', key: 'medDesc'},
            {title: '批准文号', dataIndex: 'approvalNum', key: 'approvalNum'},
            {title: '规格', dataIndex: 'medSpec', key: 'medSpec'},
            {title: '单位', dataIndex: 'medUnitDesc', key: 'medUnitDesc'},
            {title: '价格', dataIndex: 'medPrice', key: 'medPrice'},
            {title: '状态', dataIndex: 'tipState', key: 'tipState',render:(text,render,index) =>
                    text === '1' ? <Tag color="blue"><Icon type="check-circle" /> 已维护</Tag> : <Tag color="gold"><Icon type="info-circle" /> 空</Tag> }
        ];

        return (
            <Row gutter={10}>
                <Col span={12}>
                    <Card>
                        <Row justify='space-between'>
                            <Col span={20}>
                                <Row align="middle">
                                    <Input style={{ width: 150 ,marginLeft: "10px"}} placeholder='医嘱代码' onChange={(e)=>this.inputOnChange('code',e)}/>
                                    <Input style={{ width: 180 ,marginLeft: "10px"}} placeholder='医嘱名称' onChange={(e)=>this.inputOnChange('desc',e)}/>

                                    <Select
                                        showSearch
                                        allowClear
                                        style={{ width: 180,marginLeft: "10px" }}
                                        placeholder="医嘱子类"
                                        optionFilterProp="children"
                                        onChange={this.onhosSelectChange}
                                        filterOption={(input, option) =>
                                            option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                        {this.state.subCat.map((item,index) => {
                                            return (
                                                <Option key={item.id} value={item.id}>{item.descripts}</Option>
                                            )
                                        })}
                                    </Select>

                                    <span style={{marginLeft: "20px"}}>仅看已维护</span>
                                    <Switch
                                        style={{marginLeft: "10px"}}
                                        onChange={this.switchOnChange}
                                        checkedChildren={<Icon type="check" />}
                                        unCheckedChildren={<Icon type="close" />}/>
                                </Row>
                            </Col>
                            <Col span={4}>
                                <Button onClick={this.onClickFindChange} type='primary'>查询</Button>
                            </Col>
                        </Row>
                        <Table className='tableTop'  columns={columns} dataSource={this.state.tipStateData} size="small"
                            pagination={false}
                            onRow={(record) => {
                                return {
                                    onClick:event=>{
                                        this.setState({
                                            tbaleOnPressItem:record,
                                            textAreaValue:record.tipDesc
                                        })
                                    }
                                }
                            }}
                           rowClassName={(record) => {
                               return record.medCode == this.state.tbaleOnPressItem?.medCode ? 'clickRowStyl' : ''
                           }}
                        />
                        <CommonPagination size='small' current={this.state.currentPage} total={this.state.total} search= 'search'
                                          onShowSizeChange={this.onShowSizeChange.bind(this)} complete={true} dataSource={this.state.tipStateData}
                                          loading={this.state.loading} onChange={this.pageChangeFun.bind(this)} />
                    </Card>
                </Col>
                <Col span={12}>
                    <Card>
                        <Row justify='space-between'>
                            <Col span={22}>
                                <Row gutter={50}>
                                    <span style={{marginLeft: "20px"}}>{this.state.tbaleOnPressItem?.medCode}</span>
                                    <span style={{marginLeft: "15px"}}>{this.state.tbaleOnPressItem?.medDesc}</span>
                                </Row>
                            </Col>
                            <Col span={2}>
                                <Button onClick={this.onClickTipsUpdate} type='primary'>保存</Button>
                            </Col>
                        </Row>
                        <TextArea className='tableTop' rows={20} value={this.state.textAreaValue}  onChange={(e) => this.textAreaOnChange(e)}/>
                    </Card>
                </Col>
            </Row>
        );
    }
}
