<!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-tab layui-tab-card">
        <ul class="layui-tab-title">
            <li class="layui-this">常规</li>
            <li>其它信息</li>
        </ul>
        <div class="layui-tab-content">
            <!--常规开始-->
            <div class="layui-tab-item layui-show" style="min-height: 550px">
                <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: 75px">申请名称</label>
                            <div class="layui-input-inline" style="width: 855px">
                                <input type="hidden" id="id" name="id" class="layui-input">
                                <input type="text" id="sqmc" name="sqmc" lay-verify="required" lay-reqtext="申请名称不能为空"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label required" style="width: 75px">项目名称</label>
                            <div class="layui-input-inline" style="width: 855px">
                                <input type="hidden" id="xmid" name="xmid" class="layui-input">
                                <input type="hidden" id="xmmc" name="xmmc" class="layui-input">
                                <select name="rws" id="rws" lay-filter="rws" lay-verify="required" lay-reqtext="项目不能为空">
                                    <option value="">[无]</option>
                                    <option value="-1">[浏览]</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 75px">编号</label>
                            <div class="layui-input-inline" style="width: 380px">
                                <input type="text" id="bh" name="bh" class="layui-input" readonly>
                            </div>
                            <label class="layui-form-label" style="width: 85px">年份</label>
                            <div class="layui-input-inline" style="width: 350px">
                                <input type="number" id="nf" name="nf" class="layui-input" autocomplete="off">
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label required" style="width: 75px">费用类别</label>
                            <div class="layui-input-inline" style="width: 380px">
                                <input type="hidden" id="fjmc" name="fjmc" class="layui-input">
                                <input type="hidden" id="fylb" name="fylb" class="layui-input">
                                <input type="text" id="fylbmc" name="fylbmc" lay-verify="required"
                                       lay-reqtext="费用类别不能为空" class="layui-input" readonly>
                            </div>
                            <label class="layui-form-label" style="width: 85px">预算金额(元)</label>
                            <div class="layui-input-inline" style="width: 110px">
                                <input type="text" id="ysje" name="ysje" class="layui-input" lay-verify="required"
                                       lay-reqtext="该费用类别无预算" readonly>
                            </div>
                            <label class="layui-form-label required" style="width: 80px">申请金额(元)</label>
                            <div class="layui-input-inline" style="width: 120px">
                                <input type="number" id="sqje" name="sqje" class="layui-input" lay-verify="required"
                                       lay-reqtext="申请金额不能为空" autocomplete="off">
                            </div>
                        </div>
                        <!--                        <div class="layui-form-item layui-inline" style="width: 100%">-->
                        <!--                            <label class="layui-form-label" style="width: 75px">申请人</label>-->
                        <!--                            <div class="layui-input-inline" style="width: 380px">-->
                        <!--                                <input type="hidden" name="sqr" class="layui-input" th:value="${userInfo.userId}">-->
                        <!--                                <input type="text" name="sqrxm" class="layui-input" th:value="${userInfo.name}" readonly>-->
                        <!--                            </div>-->
                        <!--                            <label class="layui-form-label" style="width: 85px">申请时间</label>-->
                        <!--                            <div class="layui-input-inline" style="width: 350px">-->
                        <!--                                <input type="text" id="sqsj" name="sqsj" class="layui-input" readonly>-->
                        <!--                                <tip>日期格式：yyyy-MM-dd</tip>-->
                        <!--                            </div>-->
                        <!--                        </div>-->
                        <!--                        <div class="layui-form-item layui-inline" style="width: 100%">-->
                        <!--                            <label class="layui-form-label" style="width: 75px">是否备用金</label>-->
                        <!--                            <div class="layui-input-inline">-->
                        <!--                                <input type="checkbox" id="sfbyj" name="sfbyj" lay-skin="primary">-->
                        <!--                            </div>-->
                        <!--                        </div>-->
                        <div class="layui-form-item layui-inline" style="width: 100%; display: none" id="clfDiv">
                            <label class="layui-form-label" style="width: 70px">差旅费</label>
                            <div class="layuimini-container" style="width: 853px; margin-left: 105px">
                                <div class="layui-btn-group" style="margin-bottom: -8px">
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subAdd1"><i class="layui-icon"></i>新建
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subEdit1"><i class="layui-icon"></i>编辑
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subView1"><i class="layui-icon">&#xe63c;</i>查看
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subDelete1"><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 class="layui-form-item layui-inline" style="width: 100%; display: none" id="lsryDiv">
                            <label class="layui-form-label" style="width: 70px">临时人员</label>
                            <div class="layuimini-container" style="width: 853px; margin-left: 105px">
                                <div class="layui-btn-group" style="margin-bottom: -8px">
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subAdd2"><i class="layui-icon"></i>新建
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subEdit2"><i class="layui-icon"></i>编辑
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subView2"><i class="layui-icon">&#xe63c;</i>查看
                                    </button>
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm"
                                            lay-filter="subDelete2"><i class="layui-icon"></i>删除
                                    </button>
                                </div>
                                <table class="layui-hide" id="currentTableId3" name="currentTableId3"
                                       lay-filter="currentTableFilter3"
                                       style="padding-top: 0px;margin-top: 0px"></table>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 75px">附件</label>
                            <div class="layuimini-container" style="width: 853px; margin-left: 105px">
                                <input type="hidden" id="fj" name="fj" class="layui-input">
                                <div class="layui-btn-group" style="margin-bottom: -8px">
                                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm upload"
                                            lay-filter="upLoad" lay-data="{fileType:'budget'}"><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>
                                <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" id="btnContainer">
                            <button type="button" class="layui-btn" lay-filter="viewRwsBtn">
                                <i class="layui-icon layui-icon-file"></i>查看任务书
                            </button>
                            <button type="button" class="layui-btn" lay-filter="viewNdysBtn">
                                <i class="layui-icon layui-icon-file-b"></i>查看年度预算
                            </button>
                            <button type="button" class="layui-btn" lay-submit lay-filter="saveBtn">
                                <i class="layui-icon layui-icon-ok"></i>保存
                            </button>
                            <button class="layui-btn" lay-filter="cancleBtn">
                                <i class="layui-icon layui-icon-close"></i>取消
                            </button>
                        </div>
                    </div>
                </form>
            </div>
            <!--常规结束-->
            <!--其它信息开始-->
            <div class="layui-tab-item" style="height: 550px">
                <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" style="width: 85px">ERP编号</label>
                            <div class="layui-input-inline" style="width: 845px">
                                <input type="text" id="erpbh" name="erpbh" class="layui-input" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 85px">项目类型</label>
                            <div class="layui-input-inline" style="width: 845px">
                                <input type="hidden" id="xmlx" name="xmlx" class="layui-input">
                                <input type="text" id="xmlxmc" name="xmlxmc" class="layui-input" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 85px">项目负责人</label>
                            <div class="layui-input-inline" style="width: 845px">
                                <input type="hidden" id="xmfzr" name="xmfzr" class="layui-input">
                                <input type="text" id="xmfzrxm" name="xmfzrxm" class="layui-input" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 85px">电科院负责人</label>
                            <div class="layui-input-inline" style="width: 845px">
                                <input type="hidden" id="dkyfzr" name="dkyfzr" class="layui-input">
                                <input type="text" id="dkyfzrxm" name="dkyfzrxm" class="layui-input" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item layui-inline" style="width: 100%">
                            <label class="layui-form-label" style="width: 85px">所属部门</label>
                            <div class="layui-input-inline" style="width: 845px">
                                <input type="hidden" id="ssbm" name="ssbm" class="layui-input">
                                <input type="text" id="ssbmmc" name="ssbmmc" class="layui-input" readonly>
                            </div>
                        </div>

                        <!-- 右侧悬浮按钮 -->
                        <div class="right-bottom-btn">
                            <button class="layui-btn" lay-filter="cancleBtn">
                                <i class="layui-icon layui-icon-close"></i>关闭
                            </button>
                        </div>
                    </div>
                </form>
            </div>
            <!--其它信息结束-->
        </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', 'tableSelect', 'laydate', 'upload', 'element', , 'wangEditor'], function () {
        var form = layui.form,
            table = layui.table,
            layer = layui.layer,
            laydate = layui.laydate,
            tableSelect = layui.tableSelect,
            upload = layui.upload,
            element = layui.element,
            wangEditor = layui.wangEditor;


        var fyId = -1;
        var xmId = -1;

        //申请时间
        laydate.render({
            elem: '#sqsj',
            value: new Date()
        });

        //申请金额
        $("#sqje").on("input", function (e) {
            //获取input输入的值
            var sqje = parseFloat(e.delegateTarget.value);
            if (!isNaN(sqje)) {
                var ysje = $("#ysje").val();
                if ($("#fylbmc").val() != '' && ysje == '') {
                    Message.error("该费用类别没有预算!", 1500);
                    return false;
                } else if (sqje > ysje * 1.2) {
                    Message.error("申请金额不能超过预算金额20%!", 2000);
                    return false;
                }
            }
        });

        //项目名称
        form.on('select(rws)', function (data) {
            var len = $("#rws")[0].length;
            while (len > 2)
                $("#rws")[0].options[--len].remove();
            if (data.value == "") {
                $("#rws").val(0);
                $("#xmid").val('');
                $("#xmmc").val('');
                $("#kybg").val('');
                $("#nf").val('');
                $("#erpbh").val('');
                $("#xmlx").val('');
                $("#xmlxmc").val('');
                $("#xmfzr").val('');
                $("#xmfzrxm").val('');
                $("#dkyfzr").val('');
                $("#dkyfzrxm").val('');
                $("#ssbm").val('');
                $("#ssbmmc").val('');
                form.render();
            }

            if (data.value == "-1") {
                parent.layer.open({
                    title: '浏览',
                    type: 2,
                    shade: 0.3,
                    maxmin: true,
                    area: ['1200px', '750px'],
                    content: AjaxUtil.ctx + 'staBudgetapproval/init_project',
                    zIndex: layer.zIndex,
                    end: function (obj) {
                        xmId = localStorage.getItem("xmid");
                        var updateflag = localStorage.getItem("updateflag");
                        if (updateflag == "1") {
                            var xmmc = localStorage.getItem("xmmc");
                            $("#rws").append("<option value=" + xmId + " selected>" + xmmc + "</option>");
                            $("#xmid").val(xmId);
                            $("#xmmc").val(xmmc);

                            $("#kybg").val(localStorage.getItem("kybg"));
                            $("#nf").val(localStorage.getItem("nf"));
                            $("#erpbh").val(localStorage.getItem("erpbh"));
                            $("#xmlx").val(localStorage.getItem("xmlx"));
                            $("#xmlxmc").val(localStorage.getItem("xmlxmc"));
                            $("#xmfzr").val(localStorage.getItem("xmfzr"));
                            $("#xmfzrxm").val(localStorage.getItem("xmfzrxm"));
                            $("#dkyfzr").val(localStorage.getItem("dkyfzr"));
                            $("#dkyfzrxm").val(localStorage.getItem("dkyfzrxm"));
                            $("#ssbm").val(localStorage.getItem("ssbm"));
                            $("#ssbmmc").val(localStorage.getItem("ssbmmc"));
                        } else {
                            $("#rws").val(0);
                            $("#xmid").val();
                            $("#xmmc").val('');
                            $("#kybg").val('');
                            ;
                            $("#nf").val('');
                            $("#erpbh").val('');
                            $("#xmlx").val('');
                            $("#xmlxmc").val('');
                            $("#xmfzr").val('');
                            $("#xmfzrxm").val('');
                            $("#dkyfzr").val('');
                            $("#dkyfzrxm").val('');
                            $("#ssbm").val('');
                            $("#ssbmmc").val('');
                        }
                        form.render();
                    }
                });
            }
            return false;
        });

        element.render('checkbox');
        //费用类别
        tableSelect.render({
            elem: '#fylbmc',
            checkedKey: 'id',
            searchKey: 'mc',
            searchPlaceholder: '搜索费用名称',
            table: {
                // url: AjaxUtil.ctx + 'baseProjectbudget/yslist/' + xmId,
                url: AjaxUtil.ctx + 'prjAnnualbudgetdetail/yslist/' + ysId,
                method: 'post',
                cols: [
                    [
                        {type: "radio", fixed: 'left'},
                        {type: 'numbers', align: 'center'},
                        {field: 'fymc', width: '50%', title: '费用名称'},
                        {field: 'ysje', title: '预算金额(万元)'}
                    ]
                ]
            },
            done: function (elem, data) {
                var NEWJSON = [];
                var NEWJSON1 = [];
                var NEWJSON2 = [];
                var NEWJSON3 = [];
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.mc);
                    NEWJSON1.push(item.id);
                    NEWJSON2.push((item.ysje * 10000).toFixed(1));
                    NEWJSON3.push(item.fjmc);
                })
                elem.val(NEWJSON.join(","));
                $("#fylb").val(NEWJSON1.join(","));
                $("#ysje").val(NEWJSON2.join(","));
                $("#fjmc").val(NEWJSON3.join(","));

                if ($("#fylbmc").val().indexOf("差旅") > -1)
                    document.getElementById('clfDiv').style.display = 'block';
                else
                    document.getElementById('clfDiv').style.display = 'none';

                if ($("#fylbmc").val().indexOf("临时") > -1 && $("#fjmc").val().indexOf("人工费") > -1)
                    document.getElementById('lsryDiv').style.display = 'block';
                else
                    document.getElementById('lsryDiv').style.display = 'none';
            }
        });
        // 申请人
        // tableSelect.render({
        //     elem: '#sqrxm',
        //     checkedKey: 'id',
        //     searchKey: 'xm',
        //     searchPlaceholder: '搜索姓名',
        //     table: {
        //         url: AjaxUtil.ctx + 'basePerson/rylist',
        //         cols: [
        //             [
        //                 {type: "radio", fixed: 'left'},
        //                 {type: 'numbers', align: 'center'},
        //                 {field: 'xm', width: '15%', title: '姓名', align: 'left'},
        //                 {field: 'dwmc', title: '公司/单位', align: 'left'}
        //             ]
        //         ]
        //     },
        //     done: function (elem, data) {
        //         var NEWJSON = [];
        //         var NEWJSON1 = [];
        //         layui.each(data.data, function (index, item) {
        //             NEWJSON.push(item.xm);
        //             NEWJSON1.push(item.id);
        //         })
        //         elem.val(NEWJSON.join(","));
        //         $("#sqr").val(NEWJSON1.join(","));
        //     }
        // });

        // 用于存放表格数据
        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: '18%'},
                    {field: 'qsrq', title: '起始日期', width: '13%'},
                    {field: 'jsrq', title: '结束日期(含)', width: '14%'},
                    {field: 'ccts', title: '出差天数', width: '11%'},
                    {field: 'zj', title: '总计(元)', width: '10%'},
                    {field: 'rpj', title: '日平均(元)'}
                ]
            ],
            autoSort: true,
            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
                };
                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
                };
                layer.open({
                    title: '浏览差旅费',
                    type: 2,
                    maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'staTravelexpenses/forView',
                    end: function (index) {
                        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
            };
            layer.open({
                title: '浏览差旅费',
                type: 2,
                maxmin: true,
                shade: 0.2,
                area: [layerwidth + 'px', layerheight + 'px'],
                content: AjaxUtil.ctx + 'staTravelexpenses/forView',
                end: function (index) {
                    layer.close(index);
                }
            });
        });

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

        //文件上传
        upload.render({
            elem: '.upload' //绑定元素
            , url: AjaxUtil.ctx + 'fileupload/upload'
            , methd: 'post'
            , accept: 'file'
            , data: {}
            , 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
                    };
                    dataFj.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 (dataFj.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 < dataFj.length; i++) {
                var item = dataFj[i];
                if (item.LAY_CHECKED) {//条件：选中
                    dataFj.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            //刷新表格
            currTable2.reload();
        });
        //双击（附件）
        table.on('rowDouble(currentTableFilter2)', function (obj) {
            window.open(obj.data.Url);
        });

        // 监听查看任务书按钮
        $("button[lay-filter='viewRwsBtn']").click(function () {
            if (xmId == -1)
                Message.error("请选择费用申请的项目，然后再查看任务书", 2000);
            else {
                var index = parent.layer.open({
                    title: '查看项目任务书',
                    type: 2,
                    shade: 0.3,
                    // maxmin: true,
                    shadeClose: true,
                    area: ['1100px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjProject/forView/' + xmId,
                    end: function () {
                        layer.close(index);
                    }
                });
            }
        });

        // 监听查看年度预算按钮
        $("button[lay-filter='viewNdysBtn']").click(function () {
            if (xmId == -1)
                Message.error("请选择费用申请的项目，然后再查看任务书", 2000);
            else {
                var index = parent.layer.open({
                    title: '查看年度预算',
                    type: 2,
                    shade: 0.3,
                    // maxmin: true,
                    shadeClose: true,
                    area: ['1010px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'prjAnnualbudget/forNdysView/' + xmId,
                    end: function () {
                        layer.close(index);
                    }
                });
            }
        });

        // 监听确认按钮
        form.on('submit(saveBtn)', function (data) {
            var content = editor.txt.html().replaceAll("\"", "\'\'");
            $("#sqsm").val(content);
            data.field.sqsm = content;

            //附件
            var dataWB = [];
            for (const item of dataFj) {
                var dfWb = {
                    "id": item.id,
                    "wjm": item.wjm,
                    "Url": item.Url,
                    "scrq": item.scrq,
                    "sm": item.sm
                };
                dataWB.push(dfWb);
            }
            $("#fj").val(JSON.stringify(dataWB));
            data.field.fj = JSON.stringify(dataWB);

            //基本信息
            var index = layer.load(0, {shade: 0.1});
            AjaxUtil.post({
                url: AjaxUtil.ctx + "staBudgetapproval/doAdd",
                data: data.field,
                success: function (res) {
                    layer.close(index);
                    console.log(res);
                    if (res.code === 0) {
                        Message.success(1500, res.message, function () {
                            if (fyId == -1) {
                                fyId = res.data.id;
                                $("#id").val(fyId);
                            }
                            parent.setUpdateFlag();
                            console.log($("#fylbmc").val());

                            if ($("#fylbmc").index("差旅费") > -1) {
                                console.log($("#fylbmc").index("差旅费"));
                                //差旅费
                                var travleExpenses = [];
                                var oldExpenses = table.cache["currentTableId1"];
                                for (const item of oldExpenses) {
                                    var expense = {
                                        "zb": fyId,
                                        "xh": item.xh,
                                        "ccr": item.ccr,
                                        "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
                                    };
                                    travleExpenses.push(expense);
                                }
                                //保存差旅费
                                AjaxUtil.post({
                                    url: AjaxUtil.ctx + "staTravelexpenses/batchInsert/" + fyId,
                                    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) {
                                    }
                                });
                            } else if ($("#fylbmc").index("人工费") > -1) {
                                console.log($("#fylbmc").index("人工费"));
                                //差旅费
                                var tempMembers = [];
                                var oldMembers = table.cache["currentTableId3"];
                                for (const item of oldMembers) {
                                    var member = {
                                        "fysq": item.fysq,
                                        "xm": item.xm,
                                        "xl": item.xl,
                                        "gznr": item.gznr,
                                        "qsrq": item.qsrq,
                                        "jsrq": item.jsrq
                                    };
                                    tempMembers.push(member);
                                    AjaxUtil.post({
                                        url: AjaxUtil.ctx + "tempMember/batchInsert",
                                        contentType: "application/json",
                                        data: JSON.stringify(tempMembers),
                                        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) {
                    layer.close(index);
                }
            });
            return false;
        });

        // 项目负责人
        // tableSelect.render({
        //     elem: '#xmfzrxm',
        //     checkedKey: 'id',
        //     searchKey: 'xm',
        //     searchPlaceholder: '搜索姓名',
        //     table: {
        //         url: AjaxUtil.ctx + 'basePerson/rylist',
        //         cols: [
        //             [
        //                 {type: "radio", fixed: 'left'},
        //                 {type: 'numbers', align: 'center'},
        //                 {field: 'xm', width: '15%', title: '姓名', align: 'left'},
        //                 {field: 'dwmc', title: '公司/单位', align: 'left'}
        //             ]
        //         ]
        //     },
        //     done: function (elem, data) {
        //         var NEWJSON = [];
        //         var NEWJSON1 = [];
        //         layui.each(data.data, function (index, item) {
        //             NEWJSON.push(item.xm);
        //             NEWJSON1.push(item.id);
        //         })
        //         elem.val(NEWJSON.join(","));
        //         $("#xmfzr").val(NEWJSON1.join(","));
        //     }
        // });
        // //电科院负责人
        // tableSelect.render({
        //     elem: '#dkyfzrxm',
        //     checkedKey: 'id',
        //     searchKey: 'xm',
        //     searchPlaceholder: '搜索姓名',
        //     table: {
        //         url: AjaxUtil.ctx + 'basePerson/rylist',
        //         cols: [
        //             [
        //                 {type: "checkbox", fixed: 'left'},
        //                 {type: 'numbers', align: 'center'},
        //                 {field: 'xm', width: '15%', title: '姓名', align: 'left'},
        //                 {field: 'dwmc', title: '公司/单位', align: 'left'}
        //             ]
        //         ]
        //     },
        //     done: function (elem, data) {
        //         var NEWJSON = [];
        //         var NEWJSON1 = [];
        //         layui.each(data.data, function (index, item) {
        //             NEWJSON.push(item.xm);
        //             NEWJSON1.push(item.id);
        //         })
        //         elem.val(NEWJSON.join(","));
        //         $("#dkyfzr").val(NEWJSON1.join(","));
        //     }
        // });
        // //所属部门
        // layer.load(0, {shade: 0.1});
        // AjaxUtil.get({
        //     url: AjaxUtil.ctx + 'deptinfo/list',
        //     success: function (res) {
        //         var datalist = [];
        //         for (let item of res.data) {
        //             item.value = item.deptId;
        //             item.label = item.deptName;
        //             datalist.push(item);
        //         }
        //         // 扁平数据转树
        //         var options = arrayToTree(datalist, "0");
        //
        //         // 初始化cascader实例
        //         cascader = layCascader({
        //             elem: '#ssbmmc',
        //             clearable: true,
        //             showAllLevels: false, // 配置仅显示最后一级
        //             props: {
        //                 checkStrictly: true // 配置选择任意一级选项
        //             },
        //             options: options
        //         });
        //
        //         // 监听cascader变化
        //         cascader.changeEvent(function (value, node) {
        //             // 主动关闭面板
        //             cascader.close();
        //             $("#ssbm").val(value);
        //             $("#ssbmmc").val(node.data.label);
        //         });
        //         layer.close(index);
        //     },
        //     error: function (error) {
        //         layer.close(index);
        //     }
        // });

        // 监听确认按钮（其它信息）
        // $("button[lay-filter='saveBtn']").click(function(){
        //     if(fyId == -1) {
        //         Message.error("请先创建费用申请，然后再保存其它信息",2000);
        //     } else {
        //         var fyApproval = {
        //             "id": fyId,
        //             "erpbh": $("#erpbh").val(),
        //             "xmlx": $("#xmlx").val(),
        //             "xmfzr": $("#xmfzr").val(),
        //             "ssbm": $("#ssbm").val()
        //         }
        //         layer.load(0, {shade: 0.1});
        //         AjaxUtil.post({
        //             url: AjaxUtil.ctx + "staBudgetapproval/doUpdate2",
        //             contentType: "application/json",
        //             data: JSON.stringify(fyApproval),
        //             success: function (res) {
        //                 if (res.code === 0) {
        //                     Message.success(1500, res.message, function () {
        //                         parent.setUpdateFlag();
        //                     });
        //                 } else {
        //                     Message.error(res.message, 1000);
        //                 }
        //                 layer.close(index);
        //             },
        //             error: function (error) {
        //                 layer.close(index);
        //             }
        //         });
        //     }
        // });

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

        var editor = new wangEditor('#editor');
        editor.customConfig.uploadImgShowBase64 = true;

        editor.create();
        String.prototype.replaceAll = function (FindText, RepText) {
            return this.replace(new RegExp(FindText, 'g'), RepText);
        }
    });

    /**
     *  扁平数据转树结构
     * @returns {[]}
     * @param data
     * @param firstPid
     */
    function arrayToTree(data, firstPid) {
        const result = [];
        getChildren(data, result, firstPid);
        return result;
    }

    function getChildren(data, result, parentId) {
        for (const item of data) {
            if (item.parentId === parentId) {
                const newItem = {...item, children: []};
                result.push(newItem);
                getChildren(data, newItem.children, item.deptId);
            }
        }
    }

    function setTravelExpense(expense) {
        travelExpense = expense;
    }

    function getTravelExpense() {
        return travelExpense;
    }

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