  <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新建</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form" action="" lay-filter="formDemo">
    <div class="layui-form layuimini-form" style="padding-bottom: 100px">
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 15%">报账名称</label>
            <div class="layui-input-inline" style="width: 70%">
                <input type="hidden" id="id" name="id" class="layui-input">
                <input type="text" id="mc" name="mc" th:value="'报账'+' - '+${budgetapproval.xmmc}+' - '+${budgetapproval.fylbmc}" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <input type="hidden" id="fysq" name="fysq" class="layui-input" th:value="${budgetapproval.id}">
            <label class="layui-form-label" style="width: 15%">费用类别</label>
            <div class="layui-input-inline" style="width: 70%">
                <input type="hidden" id="fylb" name="fylb" class="layui-input" th:value="${budgetapproval.fylb}">
                <input type="text" id="fylbmc" name="fylbmc" class="layui-input" th:value="${budgetapproval.fylbmc}"
                       readonly>
            </div>

        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label" style="width: 15%">申请金额(元)</label>
            <div class="layui-input-inline" style="width: 70%">
                <input type="number" id="sqje" name="sqje" class="layui-input" th:value="${budgetapproval.sqje}"
                       readonly>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 15%">报账金额(元)</label>
            <div class="layui-input-inline" style="width: 25%">
                <input type="text" id="bzje" name="bzje" class="layui-input" autocomplete="off" lay-verify="required" placeholder="请填写报账金额...">
            </div>
            <label class="layui-form-label required" style="width: 10%">税率(%)</label>
            <div class="layui-input-inline" style="width: 30%">
                <input style="position:absolute;z-index:2;width:90%;" id="slval" name="slval" class="layui-input" lay-verify="required" placeholder=“请填入或选择税率...”>
                <select id="sl" name="sl" lay-filter="sl">
                    <option th:each="item:${dictList}" th:text="${item.name}" th:value="${item.name}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 15%;">不含税金额(元)</label>
            <div class="layui-input-inline" style="width: 25%">
                <input type="number" id="bhsje" name="bhsje" class="layui-input" readonly lay-verify="required" placeholder="请填入不含税金额...">
            </div>
            <label class="layui-form-label required" style="width: 10%">税费(元)</label>
            <div class="layui-input-inline" style="width: 30%">
                <input type="number" id="sf" name="sf" class="layui-input" readonly lay-verify="required" placeholder="请填入税费...">
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%"  id="hiddenDiv">
            <label class="layui-form-label" style="width: 140px">合同</label>
            <div class="layui-input-inline" style="width:25%"  >
                <input type="hidden" id="htmc" name="htmc" class="layui-input"
                       >
                <input type="hidden" id="htes" name="htes" class="layui-input">
                <select name="ht" id="ht" lay-filter="ht"  lay-search=“”>
                    <option value="">-- 请选择合同 --</option>
                    <option value="-1">[浏览]</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item layui-inline" style="width: 100%">
            <div class="layui-input-block" style="margin-left: 8%; width: 85%;">
                <p style="color: red;">注:报账金额可以超过当前申请金额，但不能超出当前年度出资额!!</p>
            </div>
        </div>

        <div id="cl">
            <div class="layui-form-item" >
                <label class="layui-form-label" style="width: 110px">实际花费差旅费</label>
            </div>
            <div class="layui-form-item" style="margin-left: 20px; margin-right: 20px; margin-top: -30px">
                <div class="layuimini-container">
                    <div class="layui-btn-group" style="margin-bottom: 0.5%">
                        <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subAdd"><i
                                class="layui-icon"></i>新建
                        </button>
                        <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subEdit"><i
                                class="layui-icon"></i>编辑
                        </button>
                        <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subView"><i
                                class="layui-icon">&#xe63c;</i>查看
                        </button>
                        <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="subDelete"><i
                                class="layui-icon"></i>删除
                        </button>
                    </div>
                    <table class="layui-hide" id="currentTableId1" name="currentTableId1" lay-filter="currentTableFilter1"
                           style="padding-top: 0px;margin-top: 0px"></table>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 70px">报销凭证</label>
        </div>
        <div class="layui-form-item" style="margin-left: 40px; margin-right: 40px; margin-top: -30px">
            <div class="layuimini-container">
                <input type="hidden" id="bxpz" name="bxpz" class="layui-input">
                <div class="layui-btn-group" style="margin-bottom: 0.5%">
                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm upload" lay-filter="upLoad"
                            lay-data="{fileType:'reimburse'}"><i class="layui-icon"></i>上传
                    </button>
                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="deleteFile"><i
                            class="layui-icon"></i>删除
                    </button>
                </div>
                <span style="background-color: red; font-size: 20px;">仅支持以下格式 png|jpeg|jpg|gif|pdf </span>
                <table class="layui-hide" id="currentTableId2" name="currentTableId2" lay-filter="currentTableFilter2"
                       style="padding-top: 0px;margin-top: 0px"></table>
            </div>
        </div>

        <!-- 右侧悬浮按钮 -->
        <div class="right-bottom-btn">
            <button type="button" class="layui-btn" lay-filter="viewFysqBtn">
                <i class="layui-icon layui-icon-ok"></i>查看费用申请
            </button>
            <button type="button" class="layui-btn" lay-submit lay-filter="saveBtn">
                <i class="layui-icon layui-icon-ok"></i>保存
            </button>
            <button type="button" class="layui-btn" lay-filter="cancleBtn">
                <i class="layui-icon layui-icon-close"></i>取消
            </button>
        </div>
    </div>
</form>

<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/lib/xm-select-v1.2.2/xm-select.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>

<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    var travelExpense = [];//差旅费
    var updateflag = 0;//更新标志

    layui.use(['form', 'table', 'treeTable', 'layCascader', 'upload'], function () {
        var form = layui.form,
            table = layui.table,
            layer = layui.layer,
            upload = layui.upload;
        var fysq = $("#fysq").val();
        var sqje = $("#sqje").val();
        var xmmc = [[${budgetapproval.xmmc}]];
        var fylb = [[${budgetapproval.fylb}]];
        var sfbyj = [[${budgetapproval.sfbyj}]];
        xmmc = xmmc.replace(/[\/\\]/g, '_');
        var htId ;

        var xmzys;//项目总预算
        var byj;//项目备用金
        var zbzje;//项目总报账额
        var kmzys;//科目项目组预算
        var byjbzze;//备用金报账总额
        var kmybzje;//科目已报账金额
        var bzId = -1;

        setTimeout(function() {
            // 在这里可以进行您的加载操作
            layer.msg('加载完成！', { icon: 1 });
            getbzje();
            var fylbmc = document.getElementById('fylbmc').value;
            var clDiv = document.getElementById('cl'); // 获取 div
            if ( fylbmc == '（1）外委研究支出费' || fylbmc == '（3）外协测试试验与加工费'){
                hiddenDiv.style.display = 'block';
            }else {
                hiddenDiv.style.display = 'none';
            }
            if (fylbmc == '（3）会议、差旅及国际合作交流费'){
                clDiv.style.display = 'block';
            }else {
                clDiv.style.display = 'none';
            }
            return  false;
            // 隐藏加载信息

        }, 100); // 2000

        $("#slval").blur(function () {
            // 输入框失去焦点
            if ($("#bzje").val() != '') {
                var bzje = parseFloat($("#bzje").val());
                var sl = $("#slval").val();
                var slInt = parseInt(sl, 10);
                var bhsje = (bzje * 100) / (100 + slInt);
                $("#bhsje").val(bhsje.toFixed(2));
                $("#sf").val((bzje - bhsje).toFixed(2));
            }
        });


        //监听税率
        form.on('select(sl)', function (data) {
            if ($("#bzje").val() != '') {
                var bzje = parseFloat($("#bzje").val());
                var sl = parseInt(data.value, 10);
                var bhsje = (bzje * 100) / (100 + sl);
                $("#bhsje").val(bhsje.toFixed(2));
                $("#sf").val((bzje - bhsje).toFixed(2));
                $("#slval").val(data.value);
            }
        });


        // 用于存放验收资料文件
        var dataBxpz = new Array();

        form.on('select(ht)', function (data) {
            if (data.value == "-1") {
                parent.layer.open({
                    title: '浏览任务书',
                    type: 2,
                    shade: 0.3,
                    maxmin: true,
                    area: ['1200px', '750px'],
                    content: AjaxUtil.ctx + 'staPayapproval/init_projects/'+xmmc,
                    end: function (obj) {
                        rwsId = localStorage.getItem("rws");
                        var updateflag = localStorage.getItem("updateflag");
                        if (updateflag == "1") {
                            var htmc = localStorage.getItem("htmc");
                            var id = localStorage.getItem("id");
                            htId =localStorage.getItem("id")
                            $("#ht").append("<option value=" + id + " selected>" + htmc + "</option>");
                            document.getElementById('htmc').value = localStorage.getItem('htmc');
                            var yszlString = localStorage.getItem("yszl");
                            console.log('yszlString:'+yszlString)
                            var yszl;
                            if (yszlString != null && yszlString != ''){
                                 yszl = JSON.parse(yszlString);
                            }
                            console.log(yszl)

                            dataBxpz.splice(0, dataBxpz.length);
                            dataBxpz.push.apply(dataBxpz, yszl);

                            currTable1.reload();
                            currTable2.reload();

                        } else {
                            $("#ht").val(0);
                            $("#htes").val('');
                            $("#hts").val('');
                            $("#htes").val('');
                        }
                        form.render();
                    }
                });


            }

            return false;
        });


        //gst
        $("#bzje").on("input", function (e) {
            // 当ID为"bzje"的元素上触发 "input" 事件时，执行以下函数。
            // 从事件对象中获取输入值，并将其转换为浮点数。
            var bzje = parseFloat(e.delegateTarget.value);
            // 检查输入值是否为 NaN（不是数字）。
            if (bzje<0) {
                alert("金额不能为负数或无效的数字，请重新输入！");
                e.delegateTarget.value = "";
            }
            // 获取具有ID "sqje" 的另一个输入元素的值，并将其转换为浮点数。
            var sqje = parseFloat($("#sqje").val());
            // 获取具有ID "sl" 的输入元素的值，并将其转换为整数。
            var sl = parseInt($("#sl").val(), 10);
            // 通过将 bzje 除以 (1 + sl/100) 来计算基本金额 "bhsje"。
            var bhsje = (bzje * 100) / (100 + sl);
            // 将具有ID "bhsje" 的输入元素的值设置为计算后的 bhsje，保留2位小数。
            $("#bhsje").val(bhsje.toFixed(2));
            // 通过从 bzje 中减去 bhsje 来计算税费金额 "sf"。
            var sf = bzje - bhsje;
            // 将具有ID "sf" 的输入元素的值设置为计算后的 sf，保留2位小数。
            $("#sf").val(sf.toFixed(2));
        });

        // $("#bzje").on("input",function(e){
        //     //获取input输入的值
        //     var bzje = parseFloat(e.delegateTarget.value);
        //     if(isNaN(bzje)) {
        //         bzje = 0;
        //     } else {
        //         var sqje = parseFloat($("#sqje").val());
        //         if(bzje > sqje)
        //             Message.error("报账金额与申请金额不符!", 1500);
        //         else {
        //             var sl = parseInt($("#sl").val(), 10);
        //             var bhsje = (bzje * 100) / (100 + sl);
        //             $("#bhsje").val(bhsje.toFixed(2));
        //             $("#sf").val((bzje - bhsje).toFixed(2));
        //         }
        //     }
        // });

        // 用于存放表格数据
        var dataExpense = new Array();
        //差旅费
        var currTable1 = table.render({
            elem: '#currentTableId1',
            data: dataExpense,
            cols: [
                [
                    {type: "checkbox", align: 'center'},
                    {field: 'xh', title: '序号', width: '8%'},
                    {field: 'ccrxm', title: '出差人', width: '10%'},
                    {field: 'ccdd', title: '出差地点', width: '8%'},
                    {field: 'qsrq', title: '起始日期', width: '13%'},
                    {field: 'jsrq', title: '结束日期(含)', width: '14%'},
                    {field: 'ccts', title: '出差天数', width: '11%'},
                    {field: 'zj', title: '总计(元)', width: '10%'},
                    {field: 'rpj', title: '日平均(元)'},
                    {field: 'ccsy', title: '出差事由',width: '10%'}
                ]
            ],
            autoSort: false,
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'grid',
            initSort: {
                field: 'xh', //排序字段
                type: 'asc' //排序方式:desc 降序,asc 升序
            }
        });
        // 监听新建按钮（差旅费）
        $("button[lay-filter='subAdd']").click(function () {
            layer.open({
                title: '新建差旅费',
                type: 2,
                maxmin: true,
                shade: 0.2,
                area: [layerwidth + 'px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'staTravelexpenses/forAdd',
                end: function (index) {
                    if (updateflag == 1) {
                        updateflag = 0;

                        //刷新表格
                        dataExpense.push(travelExpense);
                        currTable1.reload();
                    }
                }
            });
        });
        // 监听编辑按钮（差旅费）
        $("button[lay-filter='subEdit']").click(function () {
            if (dataExpense.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要编辑的差旅费！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条记录进行编辑操作！", 1500);
            } else {
                travelExpense = {
                    "xh": data[0].xh,
                    "ccr": data[0].ccr,
                    "ccrxm": data[0].ccrxm,
                    "ccdd": data[0].ccdd,
                    "ccts": data[0].ccts,
                    "qsrq": data[0].qsrq,
                    "jsrq": data[0].jsrq,
                    "jtf": data[0].jtf,
                    "zsf": data[0].zsf,
                    "cb": data[0].cb,
                    "zj": data[0].zj,
                    "rpj": data[0].rpj,
                    "ccsy": data[0].ccsy
                };
                layer.open({
                    title: '编辑差旅费',
                    type: 2,
                    maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'staTravelexpenses/forUpdate',
                    end: function (index) {
                        if (updateflag == 1) {
                            updateflag = 0;

                            for (var i = 0; i < dataExpense.length; i++) {
                                var item = dataExpense[i];
                                if (item.LAY_CHECKED) {//条件：选中
                                    dataExpense[i] = travelExpense;
                                    break;
                                }
                            }
                            //刷新表格
                            currTable1.reload();
                        }
                    }
                });
            }
        });
        // 监听查看按钮（差旅费）
        $("button[lay-filter='subView']").click(function () {
            if (dataExpense.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要浏览的差旅费！", 1500);
            } else if (data.length > 1) {
                Message.warning("请选择一条记录进行浏览！", 1500);
            } else {
                travelExpense = {
                    "xh": data[0].xh,
                    "ccr": data[0].ccr,
                    "ccrxm": data[0].ccrxm,
                    "ccdd": data[0].ccdd,
                    "ccts": data[0].ccts,
                    "qsrq": data[0].qsrq,
                    "jsrq": data[0].jsrq,
                    "jtf": data[0].jtf,
                    "zsf": data[0].zsf,
                    "cb": data[0].cb,
                    "zj": data[0].zj,
                    "rpj": data[0].rpj,
                    "ccsy": data[0].ccsy
                };
                var index = layer.open({
                    title: '浏览差旅费',
                    type: 2,
                    maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'staTravelexpenses/forView',
                    end: function () {
                        layer.close(index);
                    }
                });
            }
        });
        // 监听删除按钮（差旅费）
        $("button[lay-filter='subDelete']").click(function () {
            if (dataExpense.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId1')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要删除的差旅费！", 1000);
                return;
            }
            for (var i = 0; i < dataExpense.length; i++) {
                var item = dataExpense[i];
                if (item.LAY_CHECKED) {//条件：选中
                    dataExpense.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            //刷新表格
            currTable1.reload();
        });
        //双击（差旅费）
        table.on('rowDouble(currentTableFilter1)', function (obj) {
            var data = obj.data;
            travelExpense = {
                "xh": data.xh,
                "ccr": data.ccr,
                "ccrxm": data.ccrxm,
                "ccdd": data.ccdd,
                "ccts": data.ccts,
                "qsrq": data.qsrq,
                "jsrq": data.jsrq,
                "jtf": data.jtf,
                "zsf": data.zsf,
                "cb": data.cb,
                "zj": data.zj,
                "rpj": data.rpj,
                "ccsy": data.ccsy
            };
            var index = layer.open({
                title: '浏览差旅费',
                type: 2,
                maxmin: true,
                shade: 0.2,
                area: [layerwidth + 'px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'staTravelexpenses/forView',
                end: function () {
                    layer.close(index);
                }
            });
        });

        // 用于存放表格数据
        // var dataBxpz = new Array();
        //报销凭证
        var currTable2 = table.render({
            elem: '#currentTableId2',
            data: dataBxpz,
            cols: [
                [
                    {type: "checkbox", align: 'center'},
                    {type: 'numbers', align: 'center'},
                    {field: 'wjm', title: '文件名', width: '30%'},
                    {field: 'scrq', title: '上传日期', width: '15%', align: 'center'},
                    {field: 'sm', title: '说明', edit: 'text'}
                ]
            ],
            autoSort: false,
            limits: [10, 15, 20, 25, 50, 100],
            limit: -1,
            page: false,
            skin: 'grid',
            done: function (res, curr, count) { // done为数据渲染完的回调
            }
        });

        //文件上传
        upload.render({
            elem: '.upload' //绑定元素
            , url: AjaxUtil.ctx + 'fileupload/upload'
            , methd: 'post'
            , accept: 'file'
            , data: {},
            exts: 'png|jpeg|jpg|gif|pdf'
            , done: function (res) {
                if (0 === res.code) {
                    var curData = {
                        "wjm": res.data.fileName,
                        "scrq": res.data.fileDate,
                        "Url": res.data.fileUrl,
                        "sm": res.data.sm
                    };
                    dataBxpz.push(curData);
                    currTable2.reload();
                } else
                    layer.alert("上传文件出现错误！");
            }
            , error: function () {
                layer.alert("上传文件出现错误！");
            }
            , before: function (res) {
                let fileType = this.fileType;
                this.data.fileType = fileType;
            }
        });
        $("button[lay-filter='deleteFile']").click(function () {
            if (dataBxpz.length == 0)
                return;

            var checkStatus = table.checkStatus('currentTableId2')
                , data = checkStatus.data;
            if (data == null || data.length === 0) {
                Message.warning("请选择要删除的报销凭证！", 1000);
                return;
            }
            for (var i = 0; i < dataBxpz.length; i++) {
                var item = dataBxpz[i];
                if (item.LAY_CHECKED) {//条件：选中
                    dataBxpz.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            //刷新表格
            currTable2.reload();
        });
        //双击（报销凭证）
        table.on('rowDouble(currentTableFilter2)', function (obj) {
            window.open(obj.data.Url);
        });

        // 监听查看费用申请按钮
        $("button[lay-filter='viewFysqBtn']").click(function () {
            var index = parent.layer.open({
                title: '查看费用申请',
                type: 2,
                shade: 0.3,
                // maxmin: true,
                shadeClose: true,
                area: ['1030px', '700px'],
                content: AjaxUtil.ctx + 'staBudgetapproval/forApplyView/' + fysq,
                end: function () {
                    layer.close(index);
                }
            });
        });
        function getbzje() {
            AjaxUtil.post({
                url: AjaxUtil.ctx + 'staReimbursement/getZbjje/' + fysq + '/' + fylb,
                success: function (data) {
                    xmzys = data.data.xmzys;
                    byj = data.data.byj;
                    zbzje = data.data.zbzje;
                    kmzys = data.data.kmzys;
                    byjbzze = data.data.byjbzze;
                    kmybzje = data.data.kmybzje;

                }
            })
        }

        // 监听确认按钮
        form.on('submit(saveBtn)', function (data) {
            getbzje()//初始化已报账金额
            var sqje = parseFloat($("#sqje").val());
            var bzje = parseFloat($("#bzje").val());
            //判断是否是备用金
            if (sfbyj){
                //1.判断当前项目是否有备用金
                if(byj == null || byj == ''){
                    alert('该项目没有备用金，如需继续报账，请在年度预算中修改该项目备用金!!');
                    return false;
                }
                //2.判断当前报账金额是否大于项目备用金
                if (bzje>byj){
                    alert('报账金额已超过年度备用金金额，年度备用金金额为:'+byj+'元');
                    return false;
                }
                //3.判断当前项目备用金已创建的报账金额是否超额
                if (byjbzze + bzje>byj){
                    alert('该项目下备用金已申请报账金额加上当前报账金额，已超过年度备用金金额。该项目下备用金已申请报账金额为:'+byjbzze+'元,年度备用金金额为:'+byj+'元');
                    return false;
                }
            }else {
                //校验报账金额是否大于科目项目组预算
                if (kmzys == null || kmzys == '') {
                    alert('该费用类别没有项目组预算，请在年度预算中修改该费用类别预算!!')
                    return false;
                }
                if (bzje > kmzys * 1.2) {
                    alert('报账金额超过该费用类别预算可报账最大值，该费用类别预算最大报账值为:'+kmzys*1.2+'元')
                    return false;
                }
                if (bzje + kmybzje > kmzys*1.2) {
                    alert('该费用类别下已申请报账金额加上当前报账金额，已超过该费用类别预算的报账最大值：'+ kmzys*1.2+'。该费用类别下已申请的报账总金额为:'+kmybzje+'元,该费用类别预算为:'+kmzys+'元')
                    return false;
                }
                // 获取该项目的总报账金额
                if (xmzys !== null && zbzje !== null) {
                    if (zbzje + bzje > xmzys) {
                        alert("该项目下已申请报账金额加上当前报账金额,已超过该项目总预算。该项目下已申请的报账总金额为:"+zbzje+'元,项目总预算为:'+xmzys+'元')
                        return false;
                    }
                }
            }
            // if (bzje!==null && bzje > sqje * 1.2) {
            //     alert("报账金额不能超过申请金额的120%！");
            //     return false; // 阻止表单提交
            // }
            //报销凭证
            var dataWB = [];
            for (const item of dataBxpz) {
                var dfWb = {
                    "id": item.id,
                    "wjm": item.wjm,
                    "Url": item.Url,
                    "scrq": item.scrq,
                    "sm": item.sm
                };
                dataWB.push(dfWb);
            }
            $("#bxpz").val(JSON.stringify(dataWB));
            data.field.bxpz = JSON.stringify(dataWB);
            data.field.sl = data.field.slval;
            var url = AjaxUtil.ctx;
            if (bzId == -1)
                url += "staReimbursement/doAdd";
            else
                url += "staReimbursement/doUpdate";

            if ($("#htmc").val() === null || $("#htmc").val() === "") {
                executePostRequest();
            } else {
                // 弹出提示框
                layer.confirm('保存后合同基础信息将不能修改，你确定要继续吗？', {
                    btn: ['确定', '取消']
                }, function(index){
                    // 确定的回调
                    executePostRequest();
                    layer.close(index); // 关闭确认框
                }, function(index){
                    layer.close(index); // 取消的回调
                });
            }

            function executePostRequest() {
                AjaxUtil.post({
                    url: url,
                    data: data.field,
                    success: function (res) {
                        var project = {
                            "id": htId,
                            "spzt": '锁定中'
                        }
                        AjaxUtil.post({
                            url: AjaxUtil.ctx +"prjContract/updateSpzt",
                            contentType: "application/json",
                            data: JSON.stringify(project),
                            success:function () {
                                console.log(htId + '状态修改成功为' + '锁定中')
                            }
                        })
                        if (res.code === 0) {
                            Message.success(1500, res.message, function () {
                                parent.setUpdateFlag();
                                if (bzId == -1) {
                                    bzId = res.data.id;
                                    $("#id").val(bzId);
                                }
                                // 差旅费
                                var travleExpenses = [];
                                for (const item of dataExpense) {
                                    var expense = {
                                        "zb": fysq,
                                        "xh": item.xh,
                                        "ccr": item.ccr,
                                        "ccsy": item.ccsy,
                                        "ccdd": item.ccdd,
                                        "qsrq": item.qsrq,
                                        "jtf": item.jtf,
                                        "zsf": item.zsf,
                                        "cb": item.cb,
                                        "jsrq": item.jsrq,
                                        "ccts": item.ccts,
                                        "zj": item.zj,
                                        "rpj": item.rpj,
                                        "ccsy": item.ccsy
                                    };
                                    travleExpenses.push(expense);
                                }
                                // 保存差旅费
                                AjaxUtil.post({
                                    url: AjaxUtil.ctx + "staActualexpenses/batchInsert/" + fysq,
                                    contentType: "application/json",
                                    data: JSON.stringify(travleExpenses),
                                    success: function (res) {
                                        if (res.code === 0) {
                                            // Message.success(1500, res.message, function () {
                                            //
                                            // });
                                        } else {
                                            // Message.error(res.message, 1000);
                                        }
                                    },
                                    error: function (error) {
                                    }
                                });

                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            });
                        } else {
                            Message.error(res.message, 1000);
                        }
                    },
                    error: function (error) {
                        Message.error(error.message, 1000)
                    }
                });
            }
        });

        // 监听取消按钮
        $("button[lay-filter='cancleBtn']").click(function () {
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        });
    });


    function setTravelExpense(expense) {
        travelExpense = expense;
    }

    function getTravelExpense() {
        return travelExpense;
    }

    function setUpdateFlag() {
        updateflag = 1;
    }
</script>
</body>
</html>