//密码收费退费

import React from 'react';
import { Select, Input, Row, Col, Button, Spin, Divider, Popconfirm, message, Icon, Form, DatePicker, Table, Tabs, Modal, Card, Checkbox } from 'antd';
import '../style/style.less'
import { Keup, ArraySome } from '../../../../tools/index';
import { $http } from 'containers/config/https';
import TipHeader from './TipHeader'

// 组件const
const { Option } = Select;
const { RangePicker } = DatePicker;
const { TextArea } = Input;
const dateFormat = 'YYYY-MM-DD' || undefined;
const { Search } = Input;



export default class PassWordModal extends React.Component {

    constructor(props) {
        super(props)
        this.state = {
            userInfo: {}, // 用户数据
            modalShow: false,
            modalPayTitle: '身份验证',
            payButtonNum: 0,
            modalType: '',
            accmanID: '',							//账户ID
            patID: '',						//Pat_Dr 病人ID			       
            accAbleReturn: 0,
            accBlance: 0,
            accDPAllPayAmt: 0,
            accStatusDesc: '',
            modalWidth: '600px',
            otherPayData: [],
            payotherID: '', //支付方式ID 充值
            rechargeMoney: '0.00', //缴款金额 缴款金额
            rechargeReceipts: '0.00', // 实收
            rechargeGive: '0.00', // 找零
            rechargeMark: '',  //备注 保存备注信息(支票号、银行卡(号)等) 充值
            reasonsRefundType: '', // 退费原因
            reasonsRefundData: [], // 退费原因
            refundAmount: '',// 提交 退款金额
            refundChannel: '', // 提交 退款渠道
            refundRemarks: '', //  退费备注
            refundGive: '0.00', // 退款找零
            admID: '',
            currentlocID: '',
            currentwardID: '',
        }



    }

    componentDidMount() {
        this.props.onRef(this)
        this.reasonsRefund() //退费原因
        this.initJuris()  // 权限

        this.setState({
            userInfo: this.props.userInfo || {},
            modalType: this.props.modalType || '',
            payButtonNum: this.props.payButtonNum || 0,
            record: this.props.record || {},
        })

    }

    //退费原因
    reasonsRefund = () => {
        var data = {
            params: [{
                "typeCode": "RefundInv"
            }]
        }

        $http.post('urlS', this, {
            code: "05210011",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {
                    this.setState({
                        reasonsRefundData: res.reason,  // 退费原因
                    });
                }
            }
        });
    }
    // 提交 充值金额
    rechargeMoney = (e) => {
        this.setState({ rechargeMoney: Keup.MoneyReplace(e.target.value) });
    }

    // 提交 备注
    rechargeMark = (e) => {
        this.setState({ rechargeMark: e.target.value });
    }
    // 提交 实收
    rechargeReceipts = (e) => {
        let val = Keup.MoneyReplace(e.target.value)
        let { rechargeMoney } = this.state
        this.setState({ rechargeReceipts: val });

        if (parseFloat(Keup.MoneyReplace(val)) > parseFloat(rechargeMoney)) {
            this.setState({
                rechargeGive: parseFloat(parseFloat(Keup.MoneyReplace(val)) - parseFloat(rechargeMoney)).toFixed(2)// 找零
            })

        } else {
            this.setState({
                rechargeGive: '0.00', // 找零
            })
        }
    }

    // 提交 退款金额
    refundAmount = (e) => {
        let val = Keup.MoneyReplace(e.target.value)
        let { accAbleReturn } = this.state

        if (parseFloat(Keup.MoneyReplace(val)) > parseFloat(accAbleReturn)) {
            message.error('申请退款金额不能大于可退金额')
            return false
        }

        if (parseFloat(Keup.MoneyReplace(val)) < parseFloat(accAbleReturn)) {
            this.setState({
                refundGive: parseFloat(parseFloat(accAbleReturn) - parseFloat(Keup.MoneyReplace(val))).toFixed(2)    // 退款找零
            })

        } else {
            this.setState({
                refundGive: '0.00', // 退款找零
            })
        }

        this.setState({ refundAmount: val });
    }

    // 梭哈全部退款
    whole = () => {
        let { accAbleReturn } = this.state
        this.setState({ refundAmount: accAbleReturn, refundGive: '0.00', });

    }

    // 提交 退款渠道
    refundChannel = (e) => {
        this.setState({ refundChannel: e });
    }

    //  退费原因
    reasonsRefundType = (e) => {
        this.setState({ reasonsRefundType: e });
    }

    //  退费备注
    refundRemarks = (e) => {
        this.setState({ refundRemarks: e.target.value });
    }

    // 权限
    initJuris = () => {
        var data = {
            params: [{
                logonHospID: JSON.parse(window.sessionStorage.userData).hospID, //医院id 
                logonGroupID: JSON.parse(window.sessionStorage.userData).groupID,  //角色id
                logonUserID: JSON.parse(window.sessionStorage.userData).userID, //人员id
                invTypeCode: 'O'  // 类型
            }]
        }
        $http.post('urlS', this, {
            code: "05210001",
            data: data,
            success: function (res) {
                let errorCode = res.errorCode;
                if (errorCode === '0') {


                    this.setState({
                        otherPayData: res.groupConfig.HBOPConfigPaymode[0].OPPre || [],  // 其他付款方式
                    });

                }
            }
        });
    }

    //弹框确认
    payButtonOk = () => {
        let { payButtonNum, accmanID, patID, refundRemarks, refundChannel, refundAmount, modalPayTitle, modalType, payotherID, rechargeMoney, rechargeMark, reasonsRefundType, rechargeReceipts, rechargeGive } = this.state
        let data = {};
        if (payButtonNum === 0) {
            // 未执行完
            switch (modalType) {
                case '0':
                    modalPayTitle = '充值'
                    break;
                case '1':
                    modalPayTitle = '请确认病人身份，选择设置类型'
                    break;
                case '2':
                    modalPayTitle = '补打凭条'
                    break;
                case '3':
                    modalPayTitle = '设置退款金额'
                    break;
                case '4':
                    modalPayTitle = '确认取消冻结'
                    break;
                default:
                    break;
            }
            this.setState({
                payButtonNum: payButtonNum + 1,
                modalPayTitle: modalPayTitle,
            })
        } else {
            // 执行
            switch (modalType) {
                case '0':
                    data = {
                        params: [{
                            admID: this.state.record.admID || '',
                            currentlocID: this.state.record.locID || '',
                            currentwardID: this.state.record.admWardID || '',
                            financialflag: 'N',
                            accmanID: this.props.accmanID || '',							//账户ID
                            patID: this.props.patID || '',						//Pat_Dr 病人ID
                            paywID: payotherID || '',					       //支付方式ID
                            paymenttype: "P" || '',         //缴款类型 P 充值,R 退款,F 结算账户的退款(账户不再可用)
                            channel: 'IP' || '', //缴款渠道 OP 门诊窗口充值、SM 自助机充值、IP住院预交金充值
                            payamt: rechargeMoney || '',							//缴款金额 缴款金额
                            remark: rechargeMark || '',        //备注 保存备注信息(支票号、银行卡(号)等)
                        }]
                    };
                    break;
                case '1':

                    break;
                case '2':

                    break;
                case '3':
                    data = {
                        params: [{
                            admID: this.state.record.admID || '',
                            currentlocID: this.state.record.locID || '',
                            currentwardID: this.state.record.admWardID || '',
                            financialflag: 'N',
                            accmanID: this.props.accmanID || '',							//账户ID
                            patID: this.props.patID || '',						//Pat_Dr 病人ID
                            paywID: refundChannel || '',					       //支付方式ID
                            paymenttype: "R" || '',         //缴款类型 P 充值,R 退款,F 结算账户的退款(账户不再可用)
                            channel: 'IP' || '', //缴款渠道 OP 门诊窗口充值、SM 自助机充值、IP住院预交金充值
                            payamt: refundAmount || '',							//缴款金额 缴款金额
                            refundreason: reasonsRefundType || '',				//退款原因 退款原因 CB_Reason 表ID
                            remark: refundRemarks || '',        //备注 保存备注信息(支票号、银行卡(号)等)
                        }]

                    };
                    break;
                case '4':

                    break;
                default:
                    break;
            }

            if (modalType === '0' && parseFloat(rechargeReceipts) + parseFloat(rechargeGive) < parseFloat(rechargeMoney)) {
                message.error('实付金额小于应付金额')
                return false
            }

            if (parseFloat(refundAmount) === 0) {  //parseFloat(rechargeReceipts) === 0 ||

                message.error('金额需要大于0元')
                return false
            }

            $http.post('urlADS', this, {
                code: "05210020",
                data: data,
                success: function (res) {
                    let errorCode = res.errorCode;
                    if (errorCode === '0') {
                        message.success(res.errorMessage)

                        this.props.handOk()
                    }
                }
            });
        }

    }

    otherPay = (code) => {
        this.setState({
            payotherID: code,
        })
    }



    render() {
        return (
            <div>
                <div style={{ display: this.state.payButtonNum === 0 ? 'block' : 'none' }}>
                    <Row >
                        <Col span={3} className='modalLabel' >密码：</Col>
                        <Col span={21} ><Input allowClear placeholder="请输入密码" size="large" className='input' /></Col>
                    </Row>
                </div>
                <div style={{ display: this.state.payButtonNum === 1 && this.state.modalType === '0' ? 'block' : 'none' }}>
                    {/* 充值 */}
                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >充值金额：</Col>
                        <Col span={18} ><Input addonBefore="￥" allowClear placeholder="请输入充值金额" size="large" className='input' maxLength={6} value={this.state.rechargeMoney} onChange={(event) => this.rechargeMoney(event)} onBlur={(event) => { if (event.target.value === '') { event.target.value = '0.00' } this.setState({ rechargeMoney: parseFloat(event.target.value).toFixed(2) }) }} onFocus={(event) => { if (event.target.value === '0.00') { event.target.value = '' } }} /></Col>
                    </Row>
                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >支付方式：</Col>
                        <Col span={18} className='clearfix' >
                            {this.state.otherPayData.map(item => <Col key={item.paywID} className={`payCode mR20 ${item.paywID === this.state.payotherID ? 'selected' : ''}`} onClick={() => this.otherPay(item.paywID)} >{item.paywDesc}</Col>)}
                        </Col>
                    </Row>

                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >备注：</Col>
                        <Col span={18} ><TextArea placeholder="请输入30个字以内" size="large" className='input' rows={2} value={this.state.rechargeMark} onChange={(event) => this.rechargeMark(event)} /></Col>
                    </Row>
                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >实收：</Col>
                        <Col span={18} ><Input addonBefore="￥" allowClear placeholder="实收" size="large" className='input' value={this.state.rechargeReceipts} onChange={(event) => this.rechargeReceipts(event)} onBlur={(event) => { if (event.target.value === '') { event.target.value = '0.00' } this.setState({ rechargeReceipts: parseFloat(event.target.value).toFixed(2) }) }} onFocus={(event) => { if (event.target.value === '0.00') { event.target.value = '' } }} /></Col>
                    </Row>
                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >找零：</Col>
                        <Col span={18} ><Input addonBefore="￥" allowClear placeholder="找零" size="large" className='input' value={this.state.rechargeGive} disabled /></Col>
                    </Row>
                </div>
                <div style={{ display: this.state.payButtonNum === 1 && this.state.modalType === '3' ? 'block' : 'none' }}>
                    {/* 退款 */}
                    <Row span={24} className='mB20' style={{ background: 'rgb(240, 242, 245)', }}>
                        <TipHeader userInfo={this.state.userInfo} key={this.state.userInfo.patID + this.state.userInfo.accTSubID} />
                    </Row>
                    <Row span={24} className='mB20' >
                        <Col span={12} style={{ color: 'rgb(66, 185, 131)' }} >
                            当前卡内余额:{this.state.accBlance}
                        </Col>
                        <Col span={12} style={{ textAlign: 'right', color: 'rgb(66, 185, 131)' }} >
                            可退款金额:{this.state.accAbleReturn}
                        </Col>
                    </Row>
                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >申请退款金额：</Col>
                        <Col span={16} ><Input allowClear placeholder="申请退款金额" size="large" className='input' value={this.state.refundAmount} onChange={(event) => this.refundAmount(event)} onBlur={(event) => { if (event.target.value === '') { event.target.value = '0.00' } this.setState({ refundAmount: parseFloat(event.target.value).toFixed(2) }) }} onFocus={(event) => { if (event.target.value === '0.00') { event.target.value = '' } }} /></Col>
                        <Col span={2} style={{ color: '#1890ff', lineHeight: '40px', textAlign: 'center', cursor: 'pointer', }} onClick={() => this.whole()} >全部</Col>
                        <Col span={16} offset={6} style={{ color: '#f5222d', display: this.state.refundGive === '0.00' ? 'none' : 'block' }} >
                            剩余金额:{this.state.refundGive}
                        </Col>
                    </Row>
                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >退款渠道：</Col>
                        <Col span={18} >
                            <Select allowClear style={{ width: '100%' }} size="large" placeholder="退款渠道" value={this.state.refundChannel} onChange={(event) => { this.refundChannel(event) }}  >
                                <Option value=''>请选择</Option>
                                {this.state.otherPayData.map(item => <Option title={item.paywDesc + '^' + item.paywCode} key={item.paywID} value={item.paywID}  >{item.paywDesc}</Option>)}
                            </Select>
                        </Col>
                    </Row>
                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >退费原因：</Col>
                        <Col span={18} className='clearfix' >
                            <Select allowClear style={{ width: '100%' }} size="large" placeholder="请选择" value={this.state.reasonsRefundType} onChange={(event) => { this.reasonsRefundType(event) }} >
                                <Option value=''>请选择</Option>
                                {this.state.reasonsRefundData.map(item => <Option title={item.descripts + '^' + item.descriptsSPCode} key={item.id} value={item.id}  >{item.descripts}</Option>)}
                            </Select>
                        </Col>
                    </Row>
                    <Row className='mB20'>
                        <Col span={6} className='modalLabel' >备注：</Col>
                        <Col span={18} ><TextArea placeholder="备注" size="large" className='input' rows={2} value={this.state.refundRemarks} onChange={(event) => { this.refundRemarks(event) }} /></Col>
                    </Row>
                </div>
            </div>
        )
    }
}