// 发票类型维护
import React from 'react';
//import '../../assets/css/system.css';
import store from '../../store';
import {
    Table, Input, Popconfirm, Row, Col, Button, Pagination, message, Divider, Spin,DatePicker,Select
  } from 'antd';
import { $http } from '../../containers/config/https';
import './style/style.less';
// 主菜单弹框
const Option = Select.Option;
///import { CompositeDisposable } from 'rx';
  
export default  class GroupPrintConfig extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            // 主菜单列表数据
            data: [],
            // 抽屉显示
            visible: false,
            // 是否显示loading
            showLoading: true,
            total:'',
            // 表格loading
            loading: true,
            page:'',
            pageSize: 10,
            currentPage: 1,
            groupConfigID:'',
            groupConfigTID:'',
            // 单据打印模板代码
            templateCode:'',
		    //单据打印模板名字 
		    templateName:'',
		    /// 类型 (门诊收费O，住院收费I，住院押金IP，门诊预交金OP) 
		    printType:'',  
		    /// 打印纸张 
            paper:'', 
            /// 打印机名
            printName:'', 
		    /// 类名
		    className:'',
		    /// 方法名
		    methodName:'', 
		    /// 打印顺序
            printListNo:'',
            typeList:[],
        };
        this.columns = [
            {
                title: '模板代码',
                dataIndex: 'templateCode',
                width: 150,
                align: "center",
                key:'1',
            },
            {
                title: '模板名称',
                dataIndex: 'templateName',
                width: 150,
                align: "center",
                key:'2',
            },
            {
                title: '打印类型',
                dataIndex: 'printType',
                width: 100,
                align: "center",
                key:'3',
            },
            {
                title: '纸张',
                dataIndex: 'paper',
                width: 100,
                align: "center",
                key:'4',
            },
            {
                title: '打印机',
                dataIndex: 'printName',
                width: 100,
                align: "center",
                key:'5',
            },
            {
                title: '类名',
                dataIndex: 'className',
                width: 200,
                align: "center",
                key:'6',
            },
            {
                title: '方法名',
                dataIndex: 'methodName',
                width: 200,
                align: "center",
                key:'7',
            },
            {
                title: '打印顺序',
                dataIndex: 'printListNo',
                width: 100,
                align: "center",
                key:'8',
            },
            {
                title: '操作',
                dataIndex: 'operation',
                width:100,
                align: "center",
                key:'9',
                render: (text,record) => (
                    <span>
                       <Divider type="vertical" />
                        <Popconfirm title="确认删除吗?" onConfirm={this.handleDetele.bind(this, record)} >
                            <span className='span'>删除</span>
                        </Popconfirm>
                    </span>
                )
            },
        ];
    }
    
    //生命周期函数
    componentWillMount(){
        this.setState({   
            groupID: this.props.groupID
        })
    }

    componentDidMount() {
        // 初始化列表数据
        this.handleQuery();
        this.handleTypeList();
        // 初始化设置table的高度以及表体的高度
        document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 'px';
        // 数据异步请求，请求成功之后setState
		this.setState({
			showLoading: false
		})
    }

    componentWillUnmount() {
        this.setState = (state, callback) => {
            return
           }
        }





    //下拉框
    handleTypeList=()=>{
        let data={}
        data={
        params:[{}]
        }
        $http.post('urlS',this,{
            code:'05100177',
            data:data,
            success:function(res){
                if(+res.errorCode===0&&res.data){
                    let typeList=[]
                    for (var i=0;i<res.data.length;i++) {
                        typeList.push(<Option key={res.data[i].id} alias={res.data[i].descripts}>{res.data[i].descripts}</Option>)
                    }
                    this.setState({typeList:typeList})
                }
            }
        })
    }
    

    // 查询
    handleQuery = () => {
        let {groupID,templateCode,templateName,printType,page } = this.state;
        //获取sessionStorage的数据
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let data = {}
        
            data = {
                params: [{
                    groupID:groupID,
                    hospID:sessionInfo.hospID,
                    templateCode:templateCode,
                    templateName:templateName,
                    printType:printType,
                }],
                pagination:[{
                    pageSize: 10,
                    currentPage: page,
                    sortColumn: "Code",
                    sortOrder: "asc"
                }]
            }
        $http.post('urlS', this, {
          code: '05100156',
          data: data,
          success: function (res) {
            if (+res.errorCode === 0 && res.data) {
              for(var i = 0; i < res.data.length; i++) {
                res.data[i].key = i;
              }
              this.setState({data: res.data, total: res.data.length, loading:false,groupConfigID:res.groupConfigID})
            } 
          }
        })
    }

    ///保存
    handelSaveFunction=()=> {
        let { 
            groupConfigID,
            groupConfigTID,
            templateCode,
            templateName,
            printType,  
            paper, 
            printName, 
            className,
            methodName, 
            printListNo,
            page,
           } = this.state;
        //获取sessionStorage的数据
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let updateUserID=sessionInfo.userID;
        let data = {}
        data = {
               params: [{
                groupConfigID:groupConfigID,
                groupConfigTID:groupConfigTID,
                templateCode:templateCode,
                templateName:templateName,
                printType:printType,  
                paper:paper, 
                printName:printName, 
                className:className,
                methodName:methodName, 
                printListNo:printListNo,
                updateUserID:updateUserID,
               }],
               pagination:[{
                   pageSize: 10,
                   currentPage: page,
                   sortColumn: "Code",
                   sortOrder: "asc"
               }]
           }
           console.log("data", data);
            $http.post('urlADS', this, {
                code: "05100158",
                data: data,
                success: function (res) { 
                if (+res.errorCode === 0) {
                    // 提示
                     message.success('保存成功');
                    // 刷新列表数据
                    this.handleQuery();   
                }
            }
        })
    }




    // 删除
    handleDetele(record) {
        let sessionInfo = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let data = {
            params: [{
                groupConfigTID: record.groupConfigTID,
                updateUserID:sessionInfo.userID
            }]
        }
        $http.post('urlADS', this, {
            code: '05100159',
            data: data,
            success: function (res) { 

                if (+res.errorCode === 0) {
                    message.success('删除成功');
                    // 刷新列表数据
                    this.handleQuery()
                } 
            }
        })
    }
     
    
    onClicklRow= (record) => {
        //给右边的编辑form传值
        return {
            onClick: () => {
                if(this.state.groupConfigTID===""){
                    this.setState({
                        groupConfigTID:record.groupConfigTID,
                        // 单据打印模板代码
                        templateCode:record.templateCode,
                        //单据打印模板名字 
                        templateName:record.templateName,
                        /// 类型 (门诊收费O，住院收费I，住院押金IP，门诊预交金OP) 
                        printType:record.printType,  
                        /// 打印纸张 
                        paper:record.paper, 
                        /// 打印机名
                        printName:record.printName, 
                        /// 类名
                        className:record. className,
                        /// 方法名
                        methodName:record.methodName, 
                        /// 打印顺序
                        printListNo:record.printListNo,
                    });      
                }else{
                    if(this.state.groupConfigTID!==record.groupConfigTID){
                        this.setState({
                            groupConfigTID:record.groupConfigTID,
                             // 单据打印模板代码
                             templateCode:record.templateCode,
                             //单据打印模板名字 
                             templateName:record.templateName,
                             /// 类型 (门诊收费O，住院收费I，住院押金IP，门诊预交金OP) 
                             printType:record.printType,  
                             /// 打印纸张 
                             paper:record.paper, 
                             /// 打印机名
                             printName:record.printName, 
                             /// 类名
                             className:record. className,
                             /// 方法名
                             methodName:record.methodName, 
                             /// 打印顺序
                             printListNo:record.printListNo,
                          });
                    }else{
                        this.setState({
                            groupConfigTID:'',
                            // 单据打印模板代码
                            templateCode:'',
                            //单据打印模板名字 
                            templateName:'',
                            /// 类型 (门诊收费O，住院收费I，住院押金IP，门诊预交金OP) 
                            printType:'',  
                            /// 打印纸张 
                            paper:'', 
                            /// 打印机名
                            printName:'', 
                            /// 类名
                            className:'',
                            /// 方法名
                            methodName:'', 
                            /// 打印顺序
                            printListNo:'',
                        });
                    }
                } 
            },
        };
    }
    handelClean= () => {
        this.setState({
            groupConfigTID:'',
            // 单据打印模板代码
            templateCode:'',
            //单据打印模板名字 
            templateName:'',
            /// 类型 (门诊收费O，住院收费I，住院押金IP，门诊预交金OP) 
            printType:'',  
            /// 打印纸张 
            paper:'', 
            /// 打印机名
            printName:'', 
            /// 类名
            className:'',
            /// 方法名
            methodName:'', 
            /// 打印顺序
            printListNo:'',
        });
    }

   
    // 单据打印模板代码
    onChangeTemplateCode = (e) => {
        this.setState({ templateCode:e.target.value });
    }
    //单据打印模板名字 
    onChangeTemplateName = (e) => {
        this.setState({ templateName:e.target.value });
    }
    /// 类型 (门诊收费O，住院收费I，住院押金IP，门诊预交金OP) 
    onChangePrintType = (value) => {
        this.setState({ printType:value });
    }
    
    /// 打印纸张 
    onChangePaper = (e) => {
        this.setState({ paper:e.target.value });
    }
    /// 打印机名
    onChangePrintName = (e) => {
        this.setState({ printName:e.target.value });
    }
    /// 类名
    onChangeClassName= (e) => {
        this.setState({ className:e.target.value });
    }
    /// 方法名
    onChangeMethodName = (e) => {
        this.setState({ methodName:e.target.value });
    }
    /// 打印顺序
    onChangePrintListNo= (e) => {
        this.setState({ printListNo:e.target.value });
    }

    // 页码改变的回调，参数是改变后的页码及每页条数
    onChange = (page, pageSize)=> {
        this.setState({page: page, pageSize: pageSize}, () => {
            // 重新调用查询方法
            this.handleQuery();
        });
    }
    setRowClassName = (record) => {
        return record.groupConfigTID === this.state.groupConfigTID ? 'clickRowStyle' : '';
    }

    render() {
        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}>
                                模板代码：<Input placeholder="请输入" value={this.state.templateCode} style={{width: '150px', marginRight: '30px'}} onChange={this.onChangeTemplateCode} />
                                模板名称：<Input placeholder="请输入" value={this.state.templateName} style={{width: '150px', marginRight: '30px'}} onChange={this.onChangeTemplateName} />
                                {/*模板类型:<Select
                                    showSearch
                                    allowClear={true} 
                                    placeholder="请选择"
                                    optionFilterProp="children"
                                    value={this.state.printType}
                                    style={{ width: '150px' }}
                                    onChange={this.onChangePrintType}
                                    filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                                >
                                    <Option value="O">门诊收费</Option>
                                    <Option value="I">住院收费</Option>
                                    <Option value="IP">住院押金</Option>
                                    <Option value="OP">门诊预交金</Option>
                                </Select>*/}
                                 模板类型： <Select
                                            showSearch
                                            allowClear={true} 
                                            placeholder="请选择"
                                            optionFilterProp="children"
                                            value={this.state.printType}
                                            style={{ width: '150px' }}
                                            onChange={this.onChangePrintType}
                                            filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
                                            >
                                            {this.state.typeList}
                                        </Select>
                                纸张规格：<Input placeholder="请输入" value={this.state.paper} style={{width: '150px', marginRight: '30px'}} onChange={this.onChangePaper} />
                                打印机名：<Input placeholder="请输入" value={this.state.printName} style={{width: '150px', marginRight: '30px'}} onChange={this.onChangePrintName} />
                                </Col>
                            </Row >
                            <Row style={{ marginTop: '10px' }}>
                                <Col>
                                调用类名：<Input placeholder="请输入" value={this.state.className} style={{width: '150px', marginRight: '30px'}} onChange={this.onChangeClassName} />
                                方法名称：<Input placeholder="请输入" value={this.state.methodName} style={{width: '150px', marginRight: '30px'}} onChange={this.onChangeMethodName} />
                                打印顺序：<Input placeholder="请输入" value={this.state.printListNo} style={{width: '150px', marginRight: '30px'}} onChange={this.onChangePrintListNo} />
                                <Button type="primary" style={{ marginLeft: '30px'}} className="margiRight16" onClick={this.handleQuery}>查询</Button> 
                                <Button type="primary" style={{ marginLeft: '30px'}} className="margiRight16" onClick={this.handelSaveFunction}>保存</Button>
                                <Button type="primary" style={{ marginLeft: '30px'}} className="margiRight16" onClick={this.handelClean}>清除</Button>  
                                </Col> 
                            </Row>
                        </div>
                        <div>
                            <Table
                                style={{height: store.getState().tableHeight.y + 45 + 'px'}}
                                dataSource={this.state.data}
                                columns={this.columns}
                                rowClassName={this.setRowClassName}
                                className='TableA'
                                onRow={this.onClicklRow}
                                pagination={false}
                                scroll={{ x: 1200, y: store.getState().tableHeight.y }}
                                bordered
                                loading={this.state.loading}
                            />
                        </div>
                    </div>
                </div>
            </Spin>
        );
    }
  }
  