/**
 * 一个项目管理成本详情的字段
 */
//COST_PROJECT_MANAGE_DETAIL_ARR=["type","name","detail","unit","amount","price","total","predictMethod","predictWhy","remark","deletable"];
COST_PROJECT_MANAGE_DETAIL={
    "num":[false,"序号","string"],
    "type":[true,"财务费用类别","string"],
    "name1":[false,"业务项目","string"],
    "name2":[false,"业务项目","string"],
    "detail":[true,"项目明细","string"],
    "unit":[true,"单位","string"],
    "amount":[true,"数量","string"],
    "price":[true,"单价","price"],
    "total":[true,"总金额","number"],
    "predictMethod":[false,"预估计算方法","string"],
    "predictWhy":[false,"预估依据","string"],
    "remark":[false,"备注","string"],
    "deletable":[true,"是否可删除","string"]
};

SELECT_TYPE_CATEGORY_MAP_DATA_MANAGER={};

SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER={};

/*$(function () {
    $("#cost-project-manage-detail").click(function () {
        $('#my-prompt-cost-project-manage-detail').modal({
            relatedTarget: this,
            closeOnConfirm:false,
            onConfirm: function(e) {
                //不能使用e.data，因为无法获取动态添加的
                var data = collectData("am-modal-prompt-input-cost-project-manage");
                //data = prepareAjaxData(data, COST_PROJECT_MANAGE_DETAIL_ARR, $("#id").val(),true);
                data = prepareAjaxDataVerify(data, COST_PROJECT_MANAGE_DETAIL, $("#id").val());
                if(data){
                    postAjax(base+"/project/budgetEditSaveCostProjectManageDetail", data, updateCostProjectManageData);
                }
            },
            onCancel: function(e) {
            }
        });
    });
    $("#costProjectManageAddBtn").click(function () {
        appendTrCostProjectManage();
    });
    bindTypeSelectChangeManager();
    //绑定项目管理明细输入框
    bindChangeableInputProjectManage();

    initTypeCategoryManage();
});*/

$(function () {
    $("#cost-project-manage-detail").click(function () {
        $('#my-prompt-cost-project-manage-detail').modal({
            relatedTarget: this,
            closeOnConfirm:false,
            onConfirm: function(e) {
                //不能使用e.data，因为无法获取动态添加的
                var data = collectData("am-modal-prompt-input-cost-project-manage");
                //data = prepareAjaxData(data, COST_PROJECT_MANAGE_DETAIL_ARR, $("#id").val(),true);
                data = prepareAjaxDataVerify(data, COST_PROJECT_MANAGE_DETAIL, $("#id").val());
                if(data){
                    postAjax(base+"/project/budgetEditSaveCostProjectManageDetail", data, updateCostProjectManageData);
                }
            },
            onCancel: function(e) {
            }
        });
    });
});


//保存项目管理成本表
$(function () {
    $("#costProjectManageTableSave").click(function () {
        //不能使用e.data，因为无法获取动态添加的
        var data = collectData("am-modal-prompt-input-cost-project-manage");
        //data = prepareAjaxData(data, COST_PROJECT_MANAGE_DETAIL_ARR, $("#id").val(),true);
        data = prepareAjaxDataVerify(data, COST_PROJECT_MANAGE_DETAIL, $("#id").val());
        if(data){
            postAjax(base+"/project/budgetEditSaveCostProjectManageDetail", data, updateCostProjectManageData);
        }
    });

    $("#costProjectManageAddBtn").click(function () {
        appendTrCostProjectManage();
    });
    bindTypeSelectChangeManager();
    //绑定项目管理明细输入框
    bindChangeableInputProjectManage();

    initTypeCategoryManage();
});
/**
 * 采购成本增加一行
 */
function appendTrCostProjectManage() {
    var template = '<tr>\n' +
        '        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage input-budget-num" readonly></td>\n' +
        '        <td>\n' +
        '            <select style="width: 180px;float: left;" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage am-modal-prompt-input-cost-project-manage-type">\n' +
        '                <option value="xxxx" disabled selected>--请选择--</option>\n' +
        '                <option value="1" >人工成本</option>\n' +
        '                <option value="2" >业务费用</option>\n' +
        '                <option value="3" >其他费用</option>\n' +
        '            </select>\n' +
        '        </td>\n' +
        '        <td>\n' +
        '            <select style="width: 180px;float: left;" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage am-modal-prompt-input-cost-project-manage-category">\n' +
        '                <option value="xxxx" disabled selected>--请选择--</option>\n' +
        '                <option value="1" >人工费</option>\n' +
        '                <option value="2" >总部支撑人力费用</option>\n' +
        '                <option value="4" >人员差旅费</option>\n' +
        '                <option value="5" >业务费用</option>\n' +
        '                <option value="6" >商务及专项验收费用</option>\n' +
        '                <option value="7" >办公费用</option>\n' +
        '            </select>\n' +
        '            <input type="hidden" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage am-modal-prompt-input-cost-project-manage-category2"/>\n' +
        '        </td>\n' +
        '        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage"></td>\n' +
        '        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage"></td>\n' +
        '        <td><input type="number" min="0" max="99999999" step="1" maxlength="8" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage input-changeable-amount-project-manage"></td>\n' +
        '        <td><input type="text" min="0.00" max="9999999999.99" step="0.01" maxlength="19" class="price am-modal-prompt-input am-modal-prompt-input-cost-project-manage input-changeable-price-project-manage"></td>\n' +
        '        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage input-changeable-total-project-manage" readonly></td>\n' +
        '        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage"></td>\n' +
        '        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage"></td>\n' +
        '        <td><input type="text" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage"></td>\n' +
        '        <td><button type="button" style="margin-top: 10px" class="am-btn am-btn-danger am-btn-xs am-round am-modal-line-delete"><span class="am-icon-minus"></span></button>' +
        '            <input type="hidden" class="am-modal-prompt-input am-modal-prompt-input-cost-project-manage" value="1"></td>\n' +
        '    </tr>';
    $("#manageTotal").before(template);
    //重新绑定删除事件和input修改事件
    bindDeleteBtnCostManage();
    //绑定序号
    bindNum();
    bindChangeableInputProjectManage();
    //绑定总计值
    bindCostManageTotal();
    bindTypeSelectChangeManager();
    //绑定数字输入框保留两位小数
    bindNumberInput();
}

/**
 * 绑定每一行的删除事件，删除当前的一行tr，修改总计值
 */
function bindDeleteBtnCostManage() {
    $(".am-modal-line-delete").click(function () {
        //删除自己对应的tr
        $(this).parent().parent().remove();
        bindNum();
        updateAmount("input-changeable-amount-project-manage", "input-changeable-total-amount-project-manage");
        updateTotal("input-changeable-total-project-manage", "input-changeable-total-total-project-manage");
    });
}

/**
 * 绑定每个可改变的输入框，修改后改变对应输入框的值
 */
function bindCostManageTotal() {
    //数量改变
    $(".input-changeable-amount-project-manage").change(function () {
        updateAmount("input-changeable-amount-project-manage", "input-changeable-total-amount-project-manage");
        updateTotal("input-changeable-total-project-manage", "input-changeable-total-total-project-manage");
    });
    //单价改变
    $(".input-changeable-price-project-manage").change(function () {
        updateTotal("input-changeable-total-project-manage", "input-changeable-total-total-project-manage");
    });
}

/**
 * 大类类别联动效果
 */
function bindTypeSelectChangeManager() {
    //大类变化联动类别
    $(".am-modal-prompt-input-cost-project-manage-type").on('change',function () {
        var categorySelect = $(this).parent().parent().find(".am-modal-prompt-input-cost-project-manage-category");
        var categoryInput = $(this).parent().parent().find(".am-modal-prompt-input-cost-project-manage-category2");
        var type = $(this).val();
        //删除后重新添加对应的类别
        categorySelect.empty();

        if(type == 3) {
            categorySelect.attr("style", "display:none");
            categoryInput.attr("type", "text");
        } else if(SELECT_TYPE_CATEGORY_MAP_DATA_MANAGER[type]){
            categorySelect.attr("style", "width: 180px;float: left;");
            categoryInput.val("");
            categoryInput.attr("type", "hidden");
            categorySelect.append("<option value='xxxx' disabled selected>--请选择--</option>");
            SELECT_TYPE_CATEGORY_MAP_DATA_MANAGER[type].forEach(function (d) {
                categorySelect.append("<option value='"+d.id+"'>"+d.name+"</option>");
            });
        }

    });
    //类别变化联动大类
    $(".am-modal-prompt-input-cost-project-manage-category").on('change',function () {
        var typeSelect = $(this).parent().parent().find(".am-modal-prompt-input-cost-project-manage-type");
        var categoryInput = $(this).parent().find(".am-modal-prompt-input-cost-project-manage-category2");
        var categorySelect = $(this).parent().find(".am-modal-prompt-input-cost-project-manage-category");
        var category = $(this).val();
        //设置他选中类别对应的大类

        if(category == 3 || category == 8) {
            categorySelect.attr("style", "display:none");
            categoryInput.attr("type", "text");
        } else {
            categoryInput.val("");
        }
        typeSelect.val(SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[category]);
    });
}
function initTypeCategoryManage() {
    var temp1 = [];
    temp1.push({
        name:"人工费",
        id:"1"
    },{
        name:"总部支撑人力费用",
        id:"2"
    },{
        name:"自定义",
        id:"3"
    });
    SELECT_TYPE_CATEGORY_MAP_DATA_MANAGER[1] = temp1;
    var temp2 = [];
    temp2.push({
        name:"人员差旅费",
        id:"4"
    },{
        name:"业务费用",
        id:"5"
    },{
        name:"商务及专项验收费用",
        id:"6"
    },{
        name:"办公费用",
        id:"7"
    },{
        name:"自定义",
        id:"8"
    });
    SELECT_TYPE_CATEGORY_MAP_DATA_MANAGER[2] = temp2;

    SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[1] = 1;
    SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[2] = 1;
    SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[3] = 1;
    SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[4] = 2;
    SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[5] = 2;
    SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[6] = 2;
    SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[7] = 2;
    SELECT_CATEGORY_TYPE_MAP_DATA_MANAGER[8] = 2;
}
function bindChangeableInputProjectManage() {
    //数量改变
    $(".input-changeable-amount-project-manage").change(function () {
        var amount = parseInt($(this).val());
        //找到对应的单价
        var price = f5($(this).parent().parent().find(".input-changeable-price-project-manage").val());

        $(this).parent().parent().find(".input-changeable-total-project-manage").val(f2Fixed(amount*price));
    });
    //单价改变
    $(".input-changeable-price-project-manage").change(function () {
        var price = f5($(this).val());
        //找到对应的数量
        var amount = parseInt($(this).parent().parent().find(".input-changeable-amount-project-manage").val());

        $(this).parent().parent().find(".input-changeable-total-project-manage").val(f2Fixed(amount*price));
    });
}
/**
 * 更新页面收入的数据【累加】
 */
function updateCostProjectManageData(data,returnData) {
    var details = data.details;
    var total = 0;
    details.forEach(function (t, number, ts) {
        total += f2(t["total"]);
    });

    total = f2(total);

    $("input[name='costProjectManageTaxExclude']").val(f2Fixed(total));
    $("input[name='costProjectManageTaxInclude']").val(f2Fixed(total));

    //资金计划表中的
    $(".input-total-title-project-manage-cost-budget-plan").val(f2Fixed(total));


    var deviceTaxExclude = $("input[name='costPurchaseDeviceTaxExclude']").val();
    var buildTaxExclude = $("input[name='costPurchaseBuildTaxExclude']").val();
    var serviceTaxExclude = $("input[name='costPurchaseServiceTaxExclude']").val();
    var otherTaxExclude = $("input[name='costPurchaseOtherTaxExclude']").val();
    // $(".input-total-title-total-cost-budget-plan").val(f2(f2(deviceTaxExclude)+f2(buildTaxExclude)+f2(serviceTaxExclude)+f2(otherTaxExclude)
    //     +total
    //     +f2($(".input-total-title-earnest-money-cost-budget-plan").val())));

    var deviceTaxInclude = $("input[name='costPurchaseDeviceTaxInclude']").val();
    var buildTaxInclude = $("input[name='costPurchaseBuildTaxInclude']").val();
    var serviceTaxInclude = $("input[name='costPurchaseServiceTaxInclude']").val();
    var otherTaxInclude = $("input[name='costPurchaseOtherTaxInclude']").val();
    $(".input-total-title-total-cost-budget-plan").val(f2Fixed(f2(deviceTaxInclude)+f2(buildTaxInclude)+f2(serviceTaxInclude)+f2(otherTaxInclude)
        +total
        +f2($(".input-total-title-earnest-money-cost-budget-plan").val())));

    /*var costPurchaseDeviceTaxInclude = f2($("input[name='costPurchaseDeviceTaxInclude']").val());
    var costPurchaseBuildTaxInclude = f2($("input[name='costPurchaseBuildTaxInclude']").val());
    var costPurchaseServiceTaxInclude = f2($("input[name='costPurchaseServiceTaxInclude']").val());
    var costPurchaseOtherTaxInclude = f2($("input[name='costPurchaseOtherTaxInclude']").val());

    var costOtherOtherTaxInclude = f2($("input[name='costOtherOtherTaxInclude']").val());

    $("input[name='costTotalTaxInclude']").val(f2(costPurchaseDeviceTaxInclude+costPurchaseBuildTaxInclude+costPurchaseServiceTaxInclude+costPurchaseOtherTaxInclude+total+costOtherOtherTaxInclude));





    var costPurchaseDeviceTaxExclude = f2($("input[name='costPurchaseDeviceTaxExclude']").val());
    var costPurchaseBuildTaxExclude = f2($("input[name='costPurchaseBuildTaxExclude']").val());
    var costPurchaseServiceTaxExclude = f2($("input[name='costPurchaseServiceTaxExclude']").val());
    var costPurchaseOtherTaxExclude = f2($("input[name='costPurchaseOtherTaxExclude']").val());

    var costOtherOtherTaxExclude = f2($("input[name='costOtherOtherTaxExclude']").val());

    $("input[name='costTotalTaxExclude']").val(f2(costPurchaseDeviceTaxExclude+costPurchaseBuildTaxExclude+costPurchaseServiceTaxExclude+costPurchaseOtherTaxExclude+total+costOtherOtherTaxExclude));
    */

    calCostExclude();
    calCostInclude();

    updateProjectContributionProfitRate();

    layuiAlert("保存成功");
    $('#my-prompt-cost-project-manage-detail').modal('close');
}