// 角色收费权限维护
import React from 'react';
import store from '../../store';
import {
    Input, Row, Col, Button, Spin, Select, Switch, message, Tabs
} from 'antd';
import { $http } from '../../containers/config/https';
import GroupChargePaywayConfig from './GroupChargePaywayConfig.jsx';
import GroupChargeLocConfig from './GroupChargeLocConfig.jsx';
import GroupPrintConfig from './GroupPrintConfig.jsx';

import CommonPagination from 'components/pagination/index';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';

const { Option } = Select;
const TabPane = Tabs.TabPane;

export default class GroupChargeConfig extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            userInfo: {},
            // 主菜单列表数据
            data: [],
            // 抽屉显示
            visible: false,
            //输入条件
            groupConfigID: '',
            groupID: "",
            template: '',
            accTemplate: '',
            //选择条件
            ifChargeFlag: false,
            ifAbortFlag: false,
            ifStrikeFlag: false,
            ifInvNo: false,
            ifTFInvNo: false,
            printListFlag: false,
            //住院相关
            ifIPChargeFlag: false,
            ifIPAbortFlag: false,
            ifIPStrikeFlag: false,
            ifIPInvNo: false,
            ifDEPChargeFlag: false,
            ifDEPInvNo: false,
            ifTFDEPInvNo: false,
            //安全组下拉框
            groupList: [],
            // 主菜单分页数据
            key: '1',
            keyID: '1',
            flag: '',
            title: '',


            check: false,
            // 是否显示loading
            showLoading: true,
            // 表格loading
            loading: false,
            logonLocArr: [],
            chosenRectLocArr: [],
            paywayData: [],

            languageTable1: {}, // 国际化Table1
            languageTable2: {}, // 国际化Table2
            languageData: {}, // 国际化语言包
            language: {},   // 国际化语言
            languageCode: JSON.parse(window.sessionStorage.userData).language, // 国际化语言标识

        };
    }
    //生命周期函数自动加载 
    componentWillMount() {
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        this.setState({
            locID: sessionInfo.locID,
            groupID: sessionInfo.groupID
        })
    }

    // 获取主列表columns
    tableList = () => {
        var data = {
            params: [{
                "reactCode": ["GroupChargeConfig"],
                "compontName": "GroupChargeConfig",
                "type": "C"
            }]
        }
        console.log(1, data)
        $http.post('urlS', this, {
            code: '01040073',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        languageData: res.result.language || {},  // 国际化语言包
                    }, () => {
                        this.LanCNEN(this.state.languageCode)
                    })
                }
            }
        })
    }

    LanCNEN = (code) => {
        let { languageData } = this.state
        console.log("lang:" + languageData);
        this.setState({
            language: languageData[code]
        })
    }


    componentDidMount() {
        this.tableList();
        // 初始化列表数据
        this.handleQuery();
        this.handleGroupList();
        // 初始化设置table的高度以及表体的高度
        //document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 'px';
        // 数据异步请求，请求成功之后setState
        this.setState({
            showLoading: false
        })
    }
    componentWillUnmount() {
        this.setState = (state, callback) => { return; };
        // 组件销毁前清除监听
        //window.removeEventListener('resize', this.handelHeight);
    }

    // 调用公共组件的连接事件
    onRef1 = (ref) => {
        this.child1 = ref
    }
    // 调用公共组件的连接事件
    onRef2 = (ref) => {
        this.child2 = ref
    }

    //安全组的下拉框
    handleGroupList = () => {
        let data = {}
        data = {
            params: [{}]
        }
        $http.post('urlS', this, {
            code: '05100034',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.group) {
                    let tempGrouList = this.state.groupList
                    for (var i = 0; i < res.group.length; i++) {
                        tempGrouList.push(<Option key={res.group[i].id} alias={res.group[i].descripts}>{res.group[i].descripts}</Option>)
                    }
                    this.setState({ groupList: tempGrouList })
                }
            }
        })
    }

    //条件元素的改变
    // 查询条件 --- 安全组，打印模板，集中打印模板
    onChangeGroupID = (value) => {
        this.setState({ groupID: value }, () => {
            this.handleQuery();
        }
        );
    }
    onChangeTemplate = (e) => {
        this.setState({ template: e.target.value });
    }
    onChangeAccTemplate = (e) => {
        this.setState({ accTemplate: e.target.value });
    }
    // 查询开关条件
    onChangeIfChargeFlag = (checked) => {
        this.setState({ ifChargeFlag: checked });
    }
    onChangeIfAbortFlag = (checked) => {
        this.setState({ ifAbortFlag: checked });
    }

    onChangeIfStrikeFlag = (checked) => {
        this.setState({ ifStrikeFlag: checked });
    }
    onChangeifTFInvNo = (checked) => {
        this.setState({ ifTFInvNo: checked });
    }
    onChangePrintListFlag = (checked) => {
        this.setState({ printListFlag: checked });
    }
    onChangeIfInvNo = (checked) => {
        this.setState({ ifInvNo: checked });
    }

    //住院收费相关
    onChangeIfIPChargeFlag = (checked) => {
        this.setState({ ifIPChargeFlag: checked });
    }
    onChangeIfIPAbortFlag = (checked) => {
        this.setState({ ifIPAbortFlag: checked });
    }
    onChangeIfIPStrikeFlag = (checked) => {
        this.setState({ ifIPStrikeFlag: checked });
    }
    onChangeIfIPInvNo = (checked) => {
        this.setState({ ifIPInvNo: checked });
    }

    onChangeIfDEPChargeFlag = (checked) => {
        this.setState({ ifDEPChargeFlag: checked });
    }

    onChangeIfDEPInvNo = (checked) => {
        this.setState({ ifDEPInvNo: checked });
    }

    onChangeIfTFDEPInvNo = (checked) => {
        this.setState({ ifTFDEPInvNo: checked });
    }

    // 重置userInfo
    resetUserInfo() {
        this.setState({ userInfo: {} })
    }

    // 点击切换tabs的回调
    callback = (activeKey) => {
        if (+activeKey === 2) {

            this.setState({ key: activeKey, keyID: '2' })
        } else {
            this.setState({ key: activeKey })
        }

    }

    getSubLocData(logonLocArr, chosenRectLocArr) {
        this.setState({ logonLocArr: logonLocArr, chosenRectLocArr: chosenRectLocArr })
    }
    getSubPaywayData = (paywayData) => {
        this.setState({ paywayData: paywayData })
    }



    // 查询
    handleQuery = () => {
        let { groupID } = this.state;
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let data = {}
        data = {
            params: [{
                groupID: groupID,
                hospID: sessionInfo.hospID
            }]
        }
        $http.post('urlS', this, {
            code: '05100028',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0 && res.groupConfig) {
                    let ifChargeFlag = false, ifAbortFlag = false, ifStrikeFlag = false, ifInvNo = false, ifIPChargeFlag = false, ifIPAbortFlag = false, ifIPStrikeFlag = false, ifIPInvNo = false, ifTFInvNo = false, printListFlag = false, ifDEPChargeFlag = false, ifDEPInvNo = false, ifTFDEPInvNo = false
                    if (res.groupConfig[0].ifChargeFlag === "Y") { ifChargeFlag = true }
                    if (res.groupConfig[0].ifAbortFlag === "Y") { ifAbortFlag = true }
                    if (res.groupConfig[0].ifStrikeFlag === "Y") { ifStrikeFlag = true }
                    if (res.groupConfig[0].ifInvNo === "Y") { ifInvNo = true }
                    if (res.groupConfig[0].ifTFInvNo === "Y") { ifTFInvNo = true }
                    if (res.groupConfig[0].printListFlag === "Y") { printListFlag = true }
                    //住院收费相关
                    if (res.groupConfig[0].ifIPChargeFlag === "Y") { ifIPChargeFlag = true }
                    if (res.groupConfig[0].ifIPAbortFlag === "Y") { ifIPAbortFlag = true }
                    if (res.groupConfig[0].ifIPStrikeFlag === "Y") { ifIPStrikeFlag = true }
                    if (res.groupConfig[0].ifIPInvNo === "Y") { ifIPInvNo = true }


                    if (res.groupConfig[0].ifDEPChargeFlag === "Y") { ifDEPChargeFlag = true }
                    if (res.groupConfig[0].ifDEPInvNo === "Y") { ifDEPInvNo = true }
                    if (res.groupConfig[0].ifTFDEPInvNo === "Y") { ifTFDEPInvNo = true }
                    this.setState({
                        groupConfigID: res.groupConfig[0].groupConfigID,
                        groupID: res.groupConfig[0].groupID,
                        template: res.groupConfig[0].printTemplate,
                        accTemplate: res.groupConfig[0].accPrintTemplate,
                        //选择条件
                        ifChargeFlag: ifChargeFlag,
                        ifAbortFlag: ifAbortFlag,
                        ifStrikeFlag: ifStrikeFlag,
                        ifInvNo: ifInvNo,
                        ifTFInvNo: ifTFInvNo,
                        printListFlag: printListFlag,
                        //住院收费相关
                        ifIPChargeFlag: ifIPChargeFlag,
                        ifIPAbortFlag: ifIPAbortFlag,
                        ifIPStrikeFlag: ifIPStrikeFlag,
                        ifIPInvNo: ifIPInvNo,
                        ifDEPChargeFlag: ifDEPChargeFlag,
                        ifDEPInvNo: ifDEPInvNo,
                        ifTFDEPInvNo: ifTFDEPInvNo,
                    })
                }
            }
        })
    }



    //保存
    handleSave = () => {
        let { locID, groupConfigID, groupID, template, accTemplate, ifChargeFlag, ifAbortFlag, ifStrikeFlag, ifInvNo, ifTFInvNo, printListFlag, ifIPChargeFlag, ifIPAbortFlag, ifIPStrikeFlag, ifIPInvNo, ifDEPChargeFlag, ifDEPInvNo, ifTFDEPInvNo } = this.state;
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        if (ifChargeFlag) { ifChargeFlag = "Y" } else { ifChargeFlag = "N" }
        if (ifAbortFlag) { ifAbortFlag = "Y" } else { ifAbortFlag = "N" }
        if (ifStrikeFlag) { ifStrikeFlag = "Y" } else { ifStrikeFlag = "N" }
        if (ifInvNo) { ifInvNo = "Y" } else { ifInvNo = "N" }
        if (ifTFInvNo) { ifTFInvNo = "Y" } else { ifTFInvNo = "N" }
        if (printListFlag) { printListFlag = "Y" } else { printListFlag = "N" }
        //住院收费相关
        if (ifIPChargeFlag) { ifIPChargeFlag = "Y" } else { ifIPChargeFlag = "N" }
        if (ifIPAbortFlag) { ifIPAbortFlag = "Y" } else { ifIPAbortFlag = "N" }
        if (ifIPStrikeFlag) { ifIPStrikeFlag = "Y" } else { ifIPStrikeFlag = "N" }
        if (ifIPInvNo) { ifIPInvNo = "Y" } else { ifIPInvNo = "N" }
        if (ifDEPChargeFlag) { ifDEPChargeFlag = "Y" } else { ifDEPChargeFlag = "N" }
        if (ifDEPInvNo) { ifDEPInvNo = "Y" } else { ifDEPInvNo = "N" }
        if (ifTFDEPInvNo) { ifTFDEPInvNo = "Y" } else { ifTFDEPInvNo = "N" }
        let data = {}
        data = {
            params: [{
                groupConfigID: groupConfigID,
                groupID: groupID,
                userID: sessionInfo.userID,
                hospID: sessionInfo.hospID,
                printTemplate: template,
                accPrintTemplate: accTemplate,
                ifChargeFlag: ifChargeFlag,
                ifAbortFlag: ifAbortFlag,
                ifStrikeFlag: ifStrikeFlag,
                ifInvNo: ifInvNo,
                ifTFInvNo: ifTFInvNo,
                ifDEPChargeFlag: ifDEPChargeFlag,
                ifDEPInvNo: ifDEPInvNo,
                ifTFDEPInvNo: ifTFDEPInvNo,
                printListFlag: printListFlag,
                //住院收费相关
                ifIPChargeFlag: ifIPChargeFlag,
                ifIPAbortFlag: ifIPAbortFlag,
                ifIPStrikeFlag: ifIPStrikeFlag,
                ifIPInvNo: ifIPInvNo,
                selectLocID: locID,
            }]

        }
        data.subPayMode = this.state.paywayData;
        data.logonLocID = this.state.logonLocArr;
        data.rectLocIDArr = this.state.chosenRectLocArr;
        $http.post('urlADS', this, {
            code: "05100045",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    message.success(this.state.languageCode === "CN" ? '更新成功' : 'Update Completed');
                    // 刷新列表数据*/
                    // 重新调用查询方法
                    this.handleQuery();
                    this.child1.handleQuery();
                    if (this.state.keyID === '2') {
                        this.child2.handleLogonLocQuery();
                        this.child2.handleLocData();
                    }

                }
            },
            error: function (error) {
                message.error(this.state.languageCode === "CN" ? '权限更改失败：' : 'Permission change failed：' + error);
            }
        })


    }

    render() {
        let { language, languageCode } = this.state
        return (
            <Spin tip={this.state.languageCode === "CN" ? "加载中..." : 'Loading...'} spinning={this.state.showLoading}>
                <div>
                    <div style={{ padding: 24, marginBottom: 24 }}>
                        <div className="table-operations" style={{ marginBottom: '30px', marginLeft: '10px' }}>
                            <Row>
                                <Col span={24}>
                                    {language.GroupChargeConfigrole && language.GroupChargeConfigrole.descripts}： <Select
                                        showSearch
                                        placeholder={language.GroupChargeConfigrole && language.GroupChargeConfigrole.message}
                                        optionFilterProp="children"
                                        style={{ width: '200px' }}
                                        defaultValue={this.state.groupID}
                                        onChange={this.onChangeGroupID}
                                        filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                                    >
                                        {this.state.groupList}
                                    </Select>
                                    {/*住院收费相关*/}
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfighospitalizationFee && language.GroupChargeConfighospitalizationFee.descripts}：<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} onChange={this.onChangeIfIPChargeFlag} checked={this.state.ifIPChargeFlag} /></div>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfiginvoiceSettlement && language.GroupChargeConfiginvoiceSettlement.descripts}：<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifIPInvNo} onChange={this.onChangeIfIPInvNo} /></div>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigHRedStamping && language.GroupChargeConfigHRedStamping.descripts}：<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifIPStrikeFlag} onChange={this.onChangeIfIPStrikeFlag} /></div>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigHVoid && language.GroupChargeConfigHVoid.descripts}:<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifIPAbortFlag} onChange={this.onChangeIfIPAbortFlag} /></div>
                                    {/* 打印模板：<Input placeholder="请输入"  style={{width: '100px', marginRight: '30px'}} value={this.state.template} onChange={this.onChangeTemplate} />
                                    集中打印模板：<Input placeholder="请输入" style={{width: '100px', marginRight: '30px'}} value={this.state.accTemplate} onChange={this.onChangeAccTemplate} />*/}
                                    <Button type="primary" onClick={this.handleSave} style={{ marginLeft: "20px" }}>{language.GroupChargeConfigsave && language.GroupChargeConfigsave.descripts}</Button>
                                </Col>
                            </Row>
                            <Row style={{ "marginTop": "20px" }}>
                                <Col span={24}>
                                    <div style={{ display: "inline" }}>{language.GroupChargeConfigoutpatientFee && language.GroupChargeConfigoutpatientFee.descripts}：<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} onChange={this.onChangeIfChargeFlag} checked={this.state.ifChargeFlag} /></div>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigrequiresInvoice && language.GroupChargeConfigrequiresInvoice.descripts}：<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifInvNo} onChange={this.onChangeIfInvNo} /></div>
                                    {/*<div style={{ marginLeft:"50px",display:"inline"}}> 打印列表标记：<Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.printListFlag} onChange={this.onChangePrintListFlag}/></div> */}
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigIfTFInvNo && language.GroupChargeConfigIfTFInvNo.descripts}：<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifTFInvNo} onChange={this.onChangeifTFInvNo} /></div>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigORedStamping && language.GroupChargeConfigORedStamping.descripts}：<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifStrikeFlag} onChange={this.onChangeIfStrikeFlag} /></div>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigOvoid && language.GroupChargeConfigOvoid.descripts}:<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifAbortFlag} onChange={this.onChangeIfAbortFlag} /></div>
                                    {/* <div style={{ marginLeft: "50px", display: "inline" }}>是否取用电子发票:<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifElectedInv} onChange={this.onChangeIfElectedInv} /></div> */}
                                </Col>
                            </Row>
                            <Row style={{ "marginTop": "20px" }}>
                                <Col span={24}>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigIfDEPChargeFlag && language.GroupChargeConfigIfDEPChargeFlag.descripts}:<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifDEPChargeFlag} onChange={this.onChangeIfDEPChargeFlag} /></div>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigIfDEPInvNo && language.GroupChargeConfigIfDEPInvNo.descripts}:<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifDEPInvNo} onChange={this.onChangeIfDEPInvNo} /></div>
                                    <div style={{ marginLeft: "50px", display: "inline" }}>{language.GroupChargeConfigIfTFDEPInvNo && language.GroupChargeConfigIfTFDEPInvNo.descripts}:<Switch checkedChildren={languageCode === 'CN' ? "是" : 'Yes'} unCheckedChildren={languageCode === 'CN' ? "否" : 'No'} checked={this.state.ifTFDEPInvNo} onChange={this.onChangeIfTFDEPInvNo} /></div>
                                </Col>
                            </Row>
                            <Row style={{ marginTop: "30px" }}>
                                <Col span={24} style={{ float: 'left' }}>
                                    <Tabs activeKey={this.state.key} key={this.state.groupID} onChange={this.callback} type="card">
                                        <TabPane tab={language.GroupChargeConfigpaymentConfig && language.GroupChargeConfigpaymentConfig.descripts} key="1" style={{ overflow: 'auto' }}>
                                            <GroupChargePaywayConfig onRef={this.onRef1} handelThis={this} getSubPaywayData={this.getSubPaywayData} groupID={this.state.groupID} />
                                        </TabPane>
                                        <TabPane tab={language.GroupChargeConfigdepartmentConfig && language.GroupChargeConfigdepartmentConfig.descripts} key="2" style={{ overflow: 'auto' }}>
                                            <GroupChargeLocConfig onRef={this.onRef2} handelThis={this} getSubLocData={this.getSubLocData.bind(this)} groupID={this.state.groupID} />
                                        </TabPane>
                                        <TabPane tab={language.GroupChargeConfigprintTemplateConfig && language.GroupChargeConfigprintTemplateConfig.descripts} key="3" style={{ overflow: 'auto' }}>
                                            <GroupPrintConfig handelThis={this} groupID={this.state.groupID} />
                                        </TabPane>
                                    </Tabs>
                                </Col>

                            </Row>
                        </div>
                    </div>
                </div>
            </Spin>
        );
    }
}
