
import React from 'react';
//import '../../assets/css/system.css';
import store from '../../store';
import {
    Table, Input, Row, Col, Button, Pagination,Spin,Select,Switch,message,Transfer
  } from 'antd';
import { $http } from '../../containers/config/https';
const {Option} = Select;
///import { CompositeDisposable } from 'rx';
  
export default  class GroupPaymodeConfig extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            userInfo: {},
            // 主菜单列表数据
            data: [],
            // 抽屉显示
            visible: false,
            //多选框
            selectedRowKeys: [],
            //输入条件
            groupConfigID:'',
            groupID:"",
            locID:'',
            template:'',
            accTemplate:'',
            //选择条件
            ifChargeFlag:false,
            ifAbortFlag:false,
            ifStrikeFlag:false,
            ifInvNo:false,
            ifPopupWin:false,
            printListFlag:false,
            //支付方式数组
            paymodeNumList:[],
            //安全组下拉框
            groupList:[],
            //科室下拉框
            locList:[],
            //科室转移数据
            targetLocData:[],
            locData:[],
            mockData: [],
            targetKeys: [],
            // 主菜单分页数据
            total: 0,
            page: 1,
            pageSize: 10,
            flag: '',
            title: '',
            check:false,
            // 是否显示loading
            showLoading: true,
            // 表格loading
            loading:false
        };
        this.columns = [
           
            {
                title: '支付方式',
                dataIndex: 'payWay',
                width: '70px',
                align: "center"
            },
            {
                title: '挂号支付方式顺序',
                dataIndex: 'regPayMode',
                align: "center",
                width: "150px",
                render: (text,record,index) => (
                    <Select
                    showSearch
                    placeholder="请输入"
                    optionFilterProp="children"
                    style={{ width: '70px' }}
                    value={record.regPayMode}
                    onChange={this.onChangeRegPayMode.bind(this,record,index)}
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >
                    {this.state.paymodeNumList}
                </Select>
                    
                )
            },
            {
                title: '门诊预交金支付方式顺序',
                dataIndex: 'accDepositPayMode',
                align: "center",
                width: "150px",
                render: (text,record,index) => (
                    <Select
                    showSearch
                    placeholder="请输入"
                    optionFilterProp="children"
                    style={{ width: '70px' }}
                    value={record.accDepositPayMode}
                    onChange={this.onChangeAccDepositPayMode.bind(this,record,index)}
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >
                    {this.state.paymodeNumList}
                </Select>
                    
                )
            },
            {
                title: '门诊收费支付方式顺序',
                dataIndex: 'opChargePayMode',
                align: "center",
                width: "150px",
                render: (text,record,index) => (
                    <Select
                    showSearch
                    placeholder="请输入"
                    optionFilterProp="children"
                    style={{ width: '70px' }}
                    value={record.opChargePayMode}
                    onChange={this.onChangeOpChargePayMode.bind(this,record,index)}
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >
                   {this.state.paymodeNumList}
                </Select>
                    
                )
            },
            {
                title: '住院预交金支付方式顺序',
                dataIndex: 'preDepositPayMode',
                align: "center",
                width: "150px",
                render: (text,record,index) => (
                    <Select
                    showSearch
                    placeholder="请输入"
                    optionFilterProp="children"
                    style={{ width: '70px' }}
                    value={record.preDepositPayMode}
                    onChange={this.onChangePreDepositPayMode.bind(this,record,index)}
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >
                   {this.state.paymodeNumList}
                </Select>
                    
                )
            },
            {
                title: '住院收费支付方式顺序',
                dataIndex: 'ipChargePayMode',
                align: "center",
                width: "150px",
                render: (text,record,index) => (
                    <Select
                    showSearch
                    placeholder="请输入"
                    optionFilterProp="children"
                    style={{ width: '70px' }}
                    value={record.ipChargePayMode}
                    onChange={this.onChangeIpChargePayMode.bind(this,record,index)}
                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                >
                    {this.state.paymodeNumList}
                </Select>
                    
                )
            },

        ];
    }
   
    componentWillMount(){
    let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
    this.setState({ 
       locID:sessionInfo.locID,
       groupID:sessionInfo.groupID
    })}
        

  
    //生命周期函数自动加载
    componentDidMount() {
        // 初始化列表数据
        this.handleQuery();
        this.handlePaymodeNumList();
        this.handleGroupList();
        this.handleLocList();
        this.handleLocData();
        // 初始化设置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);
    }
    
    handleChange = (targetKeys) => {
        this.setState({ targetKeys:targetKeys });
      }


    //transfer使用
    handleLocData=()=>{
        let {locID,groupID} = this.state;
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let data={}
        data={
            params:[{   
                    hospID:sessionInfo.hospID,
                    groupID:groupID,
                    logonLocID:locID,
                   
                  }]
        }
        $http.post('urlS',this,{
            code:'05100043',
            data:data,
            success:function(res){
                let targetKeys = [];
                let mockData = [];
                let tempObj={};
                if(+res.errorCode===0&&res.loc){
                    for (var i=0;i<res.loc.length;i++) {
                        if(res.loc[i].chosen==="Y"){
                            targetKeys.push(res.loc[i].rowid);
                            tempObj=res.loc[i];
                            tempObj.chosen=true;
                            mockData.push(tempObj);
                        }else{
                            tempObj=res.loc[i];
                            tempObj.chosen=false;
                            mockData.push(tempObj);
                        }
                     }
                    this.setState({ mockData:mockData, targetKeys:targetKeys });
                }
                /*else{
                    setTimeout(() => {
                        this.setState({  mockData: [] ,targetKeys:[]})
                    }, 1000)
                }*/
    
            }
    
        })
      }

    //支付方式数量的下拉框
    handlePaymodeNumList = () => {
        let data = {}
        data = {
        params: [{
        }]
         }
        $http.post('urlS', this, {
            code: '05100033',
            data:data ,
            success: function (res) {
                console.log("res",res);
                if (+res.errorCode === 0 && res.payMode) {
                let tempList =this.state.paymodeNumList
                for (var i = 0; i < res.payMode.length+1; i++) {
                    tempList.push(<Option key={i} alias={i}>{i}</Option>)
                }
                this.setState({
                    paymodeNumList: tempList
                })
                 } 
                 /*else {
                    setTimeout(() => {
                        this.setState({ paymodeNumList: [] })
                    }, 1000)
                }*/
            }
        })
    
    }   
    //安全组的下拉框
    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})

            }
            /*else{
                    setTimeout(() => {
                        this.setState({ groupList: [] })
                    }, 1000)
                }*/
            }
        })
    }
    //登陆科室的选择框
    handleLocList=()=>{
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let data={}
        data={
            params:[{hospID:sessionInfo.hospID}]
        }
        $http.post('urlS',this,{
            code:'1008',
            data:data,
            success:function(res){
                if(+res.errorCode===0&&res.hbLoc){
                    let tempLocList=this.state.locList
                    for (var i=0;i<res.hbLoc.length;i++) {
                        tempLocList.push(<Option key={res.hbLoc[i].id} alias={res.hbLoc[i].descspell}>{res.hbLoc[i].descripts}</Option>)
                    }
                    this.setState({locList:tempLocList})

                }
                /*else{
                    setTimeout(() => {
                        this.setState({ locList: [] })
                    }, 1000)
                }*/
            }
        })
    }




  
    //改变table里的下拉框
    onChangeRegPayMode= (record,index,value) => {
        let regPayMode=""
        if(value==0){
            regPayMode=""
        }else{
            regPayMode=value
        }
        var tableDataTmp=[]
        tableDataTmp=this.state.data
        tableDataTmp[index].regPayMode=regPayMode
        this.setState({data:tableDataTmp})
      }
     
    onChangeAccDepositPayMode= (record,index,value) => {
        let accDepositPayMode=""
        if(+value===0){
            accDepositPayMode=""
        }else{
            accDepositPayMode=value
        }
        var tableDataTmp=[]
        tableDataTmp=this.state.data
        tableDataTmp[index].accDepositPayMode=accDepositPayMode
        this.setState({data:tableDataTmp})
      }

    onChangeIpChargePayMode= (record,index,value) => {
        let ipChargePayMode=""
        if(+value===0){
            ipChargePayMode=""
        }else{
            ipChargePayMode=value
        }
        var tableDataTmp=[]
        tableDataTmp=this.state.data
        tableDataTmp[index].ipChargePayMode=ipChargePayMode
        this.setState({data:tableDataTmp})
      }

    onChangeOpChargePayMode= (record,index,value) => {
        let opChargePayMode=""
        if(+value===0){
            opChargePayMode=""
        }else{
            opChargePayMode=value
        }
        var tableDataTmp=[]
        tableDataTmp=this.state.data
        tableDataTmp[index].opChargePayMode=opChargePayMode
        this.setState({data:tableDataTmp})
      }

    onChangePreDepositPayMode= (record,index,value) => {
        let preDepositPayMode=""
        if(+value===0){
            preDepositPayMode=""
        }else{
            preDepositPayMode=value
        }
        var tableDataTmp=[]
        tableDataTmp=this.state.data
        tableDataTmp[index].preDepositPayMode=preDepositPayMode
        this.setState({data:tableDataTmp})
      }
    //条件元素的改变
    // 查询条件 --- 安全组，打印模板，集中打印模板
    onChangeGroupID = (value) => {
        this.setState({ groupID:value },() => {
            this.handleQuery();
            this.handleLocData();
         }
            );
    }
    onChangeLocID = (value) => {
        this.setState({ locID:value },() => {
            this.handleLocData();
         }
            );
    }
    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 });
    }
    onChangeIfPopupWin= (checked) => {
        this.setState({ifPopupWin:checked });
    }
    onChangePrintListFlag= (checked) => {
        this.setState({ printListFlag:checked });
    }
    onChangeIfInvNo= (checked) => {
        this.setState({ ifInvNo:checked });
    }

    

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


    // 查询
    handleQuery = () => {
        let {groupID, page, pageSize} = 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,ifPopupWin=false,printListFlag=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].ifPopupWin==="Y"){ifPopupWin=true}
                if(res.groupConfig[0].printListFlag==="Y"){printListFlag=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,
                ifPopupWin:ifPopupWin,
                printListFlag:printListFlag
            })
            }
          }
        })

      let data1 = {}
        
      data1= {
                params: [{
                    groupID:groupID,
                    hospID:sessionInfo.hospID
                }],
                pagination:[{
                    pageSize: pageSize,
                    currentPage: page,
                    sortColumn: "Code",
                    sortOrder: "asc"
                }]
            }
        $http.post('urlS', this, {
          code: '05100030',
          data: data1,
          success: function (res) {
            if (+res.errorCode === 0 && res.groupConfigPayMode) {
              for(var i = 0; i < res.groupConfigPayMode.length; i++) {
                res.groupConfigPayMode[i].key = i;
              }
              this.setState({data: res.groupConfigPayMode, total: res.groupConfigPayMode.length, loading:false})
            } 
            /*else {
                setTimeout(() => {
                    this.setState({data: [], total: 0, loading: false})
                }, 500)
            }*/
          }
        })
    }


    //保存
    handleSave = () => {
        let {locID,groupConfigID,groupID,template,accTemplate,ifChargeFlag,ifAbortFlag,ifStrikeFlag,ifInvNo,ifPopupWin,printListFlag} = 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(ifPopupWin){ifPopupWin="Y"}else{ifPopupWin="N"}
        if(printListFlag){printListFlag="Y"}else{printListFlag="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,
                    ifPopupWin:ifPopupWin,
                    printListFlag:printListFlag,
                }]
                
            }
        data.subPayMode=this.state.data;
        data.rectLocIDArr=this.state.targetKeys;
        data.logonLocID=locID
        $http.post('urlADS', this, {
                code:"05100045",
                data: data,
                success: function (res) { 
                    if (+res.errorCode === 0) {
                    message.success('更新成功');
                    // 刷新列表数据*/
                    this.handleQuery();
                    }
                },
                error:function (error) {
                    message.error('权限更改失败：' + error);
                }
            })


    }

    render() {
        const {selectedRowKeys } = this.state;
        {/*const rowSelection = {
          selectedRowKeys,
          onChange: this.onSelectChange,
          onSelect: this.onSelect
        };*/}
        return (
            <Spin tip="加载中..." spinning={this.state.showLoading}>
                <div>
                    <div style={{padding: 24,marginBottom: 24 }}>
                        <div className="table-operations" style={{marginBottom: '30px',marginLeft:'10px'}}>
                            <Row>
                                <Col span={24}>
                                    角色： <Select
                                            showSearch
                                            placeholder="请输入"
                                            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>
                                    {/*安全组：<Input placeholder="请输入"  style={{width: '100px', marginRight: '30px'}} value={this.state.group} onChange={this.onChangeGroup} />*/}
                                    打印模板：<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"}}>保存</Button>
                                </Col>
                            </Row>
                            <Row style={{ "marginTop":"20px"}}>
                                <Col span={24}>
                                    <div style={{ display:"inline"}}>门诊收费：<Switch checkedChildren="是" unCheckedChildren="否"  onChange={this.onChangeIfChargeFlag} checked={this.state.ifChargeFlag}/></div>
                                    <div style={{ marginLeft:"130px",display:"inline"}}>结算需要发票：<Switch checkedChildren="是" unCheckedChildren="否" 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>      
                                </Col>    
                            </Row>
                            <Row style={{ "marginTop":"20px"}}> 
                                <Col span={24}>
                                    <div style={{ display:"inline"}}>卡支付不弹出结算框：<Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.ifPopupWin} onChange={this.onChangeIfPopupWin}/></div>
                                    <div style={{ marginLeft:"60px",display:"inline"}}>允许红冲：<Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.ifStrikeFlag} onChange={this.onChangeIfStrikeFlag}/></div>
                                    <div style={{ marginLeft:"80px",display:"inline"}}>允许作废:<Switch checkedChildren="是" unCheckedChildren="否" checked={this.state.ifAbortFlag} onChange={this.onChangeIfAbortFlag}/></div>  
                                </Col>
                            </Row>
                            
                        </div>
                        <div>
                            <Row><Col><font color="blue">注支付方式顺序：0或空为不用此支付方式，1为默认支付方式，其他为非默认支付方式，以从序号排序</font></Col> </Row>
                            <Row> <Col span={15}><Table
                                    style={{height: store.getState().tableHeight.y + 45 + 'px',width:"1000px",display:"inline-block"}}
                                    dataSource={this.state.data}
                                    columns={this.columns}
                                    rowClassName="editable-row"
                                    //rowSelection={rowSelection}
                                    pagination={false}
                                    scroll={{ x: 1000, y: store.getState().tableHeight.y }}
                                    bordered
                                    loading={this.state.loading}
                                    /></Col>
                                    <Col span={9}>
                                        <Row style={{ marginLeft:"30px"}}>登陆科室： <Select
                                            showSearch
                                            placeholder="请输入"
                                            optionFilterProp="children"
                                            style={{ width: '200px',marginLeft:"10px" }}
                                            value={this.state.locID}
                                            onChange={this.onChangeLocID}
                                            filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                                            >
                                            {this.state.locList}
                                        </Select>
                                        </Row>
                                        <Transfer
                                                style={{ marginLeft:"20px",display:"inline-block",marginTop:"20px",}}
                                                dataSource={this.state.mockData}
                                                showSearch
                                                listStyle={{
                                                width: 200,
                                                height: 300,
                                                }}
                                                titles={['选择科室', '执行科室']}
                                                operations={['添加', '移除']}
                                                targetKeys={this.state.targetKeys}
                                                onChange={this.handleChange}
                                                render={item => `${item.descripts}`}
                                                footer={this.renderFooter}
                                            />
                                    </Col>
                            </Row>
                             <Pagination
                                size="small"
                                showQuickJumper
                                showSizeChanger
                                defaultCurrent={1}
                                total={this.state.total}
                                onChange={this.onChange}
                                onShowSizeChange={this.onShowSizeChange}
                                style={{textAlign: "left", marginTop: '13px'}}
                            />
                           
                        </div>
                    </div>
                </div>
            </Spin>
        );
    }
  }
  