'use strict';

/**
 * 变更令-审批页js
 *
 * @author EllisRan.
 * @date 2018/11/22
 * @version
 */

$(document).ready(() => {
    $('#sp-back input[name="status"]').click(function (e) {
        if($(e.target).is('label')){
            return;
        }
        if (parseInt($(this).val()) === 4) {
            $('.change-approval-stop').show();
            $('.change-approval-back').hide();
        } else {
            $('.change-approval-stop').hide();
            $('.change-approval-back').show();
        }
    });

    // 清单输入监控并更新
    $('body').on('valuechange', '.clist input', function (e, previous) {
        const amount = $(this).val();
        const lid = $(this).parents('tr').data('lid');
        const tr = $('#list tr[data-lid="' + lid + '"]').eq(0);
        const unitprice = tr.children('td[data-site="4"]').text();
        tr.children('.amount_cost').text(amount != '' ?
            roundnum(parseFloat(unitprice).mul(parseFloat(amount)),totalPriceUnit) : '');

        // 统计总金额
        let totalcost = 0;
        $('.clist').each(function(){
            const utotal = $(this).find('.amount_cost').text();
            totalcost = utotal != '' ? parseFloat(totalcost)+parseFloat(utotal) : parseFloat(totalcost);
        });
        $('.amount_totalcost').eq(1).text(totalcost !== 0 ? roundnum(totalcost, totalPriceUnit) : '');
    });

    // 审批提交与判断
    $('.approval-btn').on('click', function () {
        // 判断审批状态
        let returnflag = true;
        if ($(this).hasClass('btn-success')) {
            const sdesc = $('#success-approval').find('textarea').val();
            if (sdesc === '') {
                toastr.error('审批意见不能为空!');
                returnflag = false;
            }
            if ($('#w_code').val() === '') {
                toastr.error('批复文号不能为空!');
                returnflag = false;
            } else {
                $('input[name="w_code"]').val($('#w_code').val());
            }
            // 判断并提交变更清单表格数据到表单中
            const clist = [];
            $('.clist input').each(function(k, v){
                const value = $(this).val();
                const lid = $(this).parents('tr').data('lid');
                if (value === '') {
                    toastr.error('清单第' + (k+1) + '行审批变更数量不能为空');
                    returnflag = false;
                }
                clist.push(lid+'_'+value);
            });
            $('#change-list-approval').val(clist.join(','));

            if(returnflag) {
                $('#success-approval').submit();
            }
        } else {
            const sdesc = $('#fail-approval').find('textarea').val();
            if (sdesc === '') {
                toastr.error('审批意见不能为空!');
                returnflag = false;
            }
            const type = $('#fail-approval').find('input[name="status"]:checked').val();
            if (type === undefined) {
                toastr.error('请选择退回类型!');
                returnflag = false;
            }
            if(returnflag) {
                $('#fail-approval').submit();
            }
        }
    })
});
