buynow-select-invoice.page.js 6.05 KB
/*
 * @Author: Targaryen
 * @Date: 2017-06-23 11:43:34
 * @Last Modified by: Targaryen
 */
require('scss/cart/select-invoice.page.scss');

let $ = require('yoho-jquery'),
    tip = require('js/plugin/tip'),
    dialog = require('js/plugin/dialog'),
    order = require('js/cart/buynow/order-info');

let $invoiceNotice = $('.invoice-notice'),
    $taxNumber = $('.tax-number'),
    $editFlag = $('.edit-flag'),
    $tel = $('.tel'),
    $company = $('.company'),
    $copyTel = $('.copy-tel');
let $confirmBtn = $('.confirm-btn');
let $buyerTaxNumber = $('#buyerTaxNumber');

let eInvoiceType = $('#eInvoices').data('id');
let orderInfo = order.orderInfo;
let linkUrl = document.referrer;
let isModifyTel = false;
let C_ID = window._ChannelVary[window.cookie('_Channel')] || 1;

require('js/common');
require('js/cart/cartbuynow/select-invoice');

if (window.getUid() !== orderInfo('uid')) {
    order.init();
    window.location.reload();
}

function goToBack() {
    if (linkUrl) {
        window.location.href = linkUrl;
    } else {
        history.go(-1);
    }
}

/**
 * 切换确认按钮激活状态
 */
function switchBtnStatus() {
    if ($('.invoice-top').find('.on').text() === '单位') {
        if ($buyerTaxNumber.val() && $company.val() && $tel.val()) {
            $confirmBtn.addClass('active');
        } else {
            $confirmBtn.removeClass('active');
        }
    } else {
        if ($tel.attr('data-tel')) {
            $confirmBtn.addClass('active');
        } else {
            $confirmBtn.removeClass('active');
        }
    }
}

/**
 * 发票抬头输入监听
 */
$company.bind('input', function() {
    switchBtnStatus();
});

/**
 * 手机号输入监听
 */
$tel.bind('input', function() {
    switchBtnStatus();
});

/**
 * 税号输入监听
 */
$buyerTaxNumber.bind('input', function() {
    switchBtnStatus();
});

// 单选效果
function chooseAction(pDom, dom) {
    if (dom.hasClass('icon-cb-radio')) {
        return;
    } else {
        pDom.find('.choose').removeClass('icon-cb-radio icon-radio').addClass('icon-radio');
        dom.removeClass('icon-radio').addClass('icon-cb-radio');
        dom.parent().addClass('on');
        dom.parent().siblings().removeClass('on');
        $editFlag.val('true');
    }
}

// 确认表单事件
function confirmAction() {
    let title = $('.invoice-top').find('.on').text(),
        buyerTaxNumber = $buyerTaxNumber.val(),
        tel = $tel.attr('data-tel'),
        company = $company.val();

    if ($editFlag.val() === 'true') {
        if (!tel) {
            tip.show('请输入正确手机号');
            $tel.focus();
            return false;
        } else if (company.length === 0) {
            tip.show('请填写发票抬头');
            $company.focus();
            return false;
        } else if (company.length > 30) {
            tip.show('发票抬头不得超过30个汉字');
            $company.focus();
            return false;
        } else if (title === '单位' && !buyerTaxNumber) {
            tip.show('请输入正确的15位或18位纳税人识别号');
            $company.focus();
            return false;
        } else {
            dialog.showDialog({
                dialogText: '确认保存修改内容?',
                hasFooter: {
                    leftBtnText: '取消',
                    rightBtnText: '确定'
                }
            }, function() {
                orderInfo('invoices_type', eInvoiceType);
                orderInfo('receiverMobile', tel);

                if (title === '单位') {
                    orderInfo('invoice_payable_type', 2);
                    orderInfo('invoices_title', company);
                    orderInfo('invoices_title_personal', null);
                    orderInfo('buyerTaxNumber', buyerTaxNumber);
                } else {
                    orderInfo('invoice_payable_type', 1);
                    orderInfo('invoices_title', null);
                    orderInfo('invoices_title_personal', company);
                }

                if (isModifyTel && $copyTel !== tel) {
                    orderInfo('isModifyTel', true);
                } else {
                    orderInfo('isModifyTel', false);
                }

                dialog.showDialog({
                    dialogText: '保存成功',
                    autoHide: true,
                    fast: true
                });
                goToBack();
            }, function() {
                goToBack();
            });
        }
    } else {
        goToBack();
    }
}

// 发票抬头、发票内容选择
$('.invoice-top span, .invoice-cont li').not('.invoice-cont .cont-title').on('touchstart', function() {
    chooseAction($(this).parent(), $(this).find('.choose'));

    if ($(this).text() === '单位') {
        $company.val('');
        $company.removeAttr('disabled');
        $taxNumber.slideDown();
    }

    if ($(this).text() === '个人') {
        $company.val('个人(可修改)');
        $taxNumber.slideUp();
    }
    switchBtnStatus();
});

// 发票须知
$('.invoice-btn').on('touchstart', function() {
    $invoiceNotice.fadeIn();
    return false;
});

// 关闭发票须知弹框
$('.think-ok, .mask-bg').on('touchstart', function() {
    $invoiceNotice.fadeOut();
});

// 电话清空
$('.istel').one('input', function() {
    $(this).val('').removeClass('istel');
});

$tel.on('input', function() {
    $(this).attr('data-tel', $(this).val());
    $editFlag.val('true');
    isModifyTel = true;
});

$company.on('input', function() {
    $editFlag.val('true');
});

// 确认及返回事件
$('.confirm-btn, .nav-back').on('touchstart', function(e) {

    setTimeout(function() {
        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: 'YB_SC_INVOICE_INFO_SAVE',
                param: JSON.stringify({
                    C_ID: C_ID,
                    INVOICE_TYPE: eInvoiceType,
                    INVOICE_TITLE: $('.invoice-top').find('.on').text(),
                    INVOICE_CONTENT: 12
                })
            }, true);
        }
    }, 200);

    e.preventDefault();
    confirmAction();
});