<!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" style="width: 60px">费用申请</label>
            <div class="layui-input-inline" style="width: 810px">
                <input type="hidden" id="fysq" name="fysq" class="layui-input">
                <input type="text" id="fysqmc" name="fysqmc" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label" style="width: 60px">任务书</label>
            <div class="layui-input-inline" style="width: 810px">
                <input type="hidden" id="rws" name="rws" class="layui-input">
                <input type="text" id="rwsmc" name="rwsmc" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 60px">合同</label>
            <div class="layui-input-inline" style="width: 380px">
                <select name="ht" id="ht" lay-filter="ht" lay-verify="required" lay-reqtext="合同不能为空" lay-search=“”>
                    <option value="">-- 请选择 --</option>
                    <option th:each="item:${htList}" th:text="${item.htmc}" th:value="${item.id}"></option>
                </select>
            </div>
            <label class="layui-form-label required" style="width: 120px">合同金额(万元)</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="htje" name="htje" class="layui-input" lay-verify="required"
                       lay-reqtext="合同金额不能为空" autocomplete="off" readonly>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 60px">付款计划</label>
            <div class="layui-input-inline" style="width: 380px">
                <select name="fkjh" id="fkjh" lay-filter="fkjh" lay-verify="required" lay-reqtext="付款计划不能为空">
                    <option value="">-- 请选择 --</option>
                </select>
            </div>
            <label class="layui-form-label required" style="width: 130px; margin-left: -10px">本次付款金额(万元)</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="bcfkje" name="bcfkje" 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: 60px">合同文件</label>
            <div class="layuimini-container" style="margin-left: 90px; margin-right: 20px; padding-bottom: 10px">
                <input type="hidden" id="htwj" name="htwj" 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" id="upLoad1"
                            lay-data="{fileType:'payhtwj'}"><i class="layui-icon"></i>上传
                    </button>
                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="deleteFile1"><i
                            class="layui-icon"></i>删除
                    </button>
                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="btnView2"><i
                            class="layui-icon">&#xe63c;</i>查看
                    </button>
                </div>
                <table class="layui-hide" id="currentTableId1" name="currentTableId1" lay-filter="currentTableFilter1"
                       style="padding-top: 0px;margin-top: 0px"></table>
                <label>请在合同管理中修改文件</label>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label" style="width: 60px">验收资料</label>
            <div class="layuimini-container" style="margin-left: 90px; margin-right: 20px; padding-bottom: 10px">
                <input type="hidden" id="yszl" name="yszl" 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" id="upLoad2"
                            lay-data="{fileType:'payyszl'}"><i class="layui-icon"></i>上传
                    </button>
                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="deleteFile2"><i
                            class="layui-icon"></i>删除
                    </button>
                    <button type="button" class="layui-btn  layui-btn-primary layui-btn-sm" lay-filter="btnView2"><i
                            class="layui-icon">&#xe63c;</i>查看
                    </button>
                </div>
                <table class="layui-hide" id="currentTableId2" name="currentTableId2" lay-filter="currentTableFilter2"
                       style="padding-top: 0px;margin-top: 0px"></table>
                <label>需上传合同约定的技术报告、测试报告、第三方检测报告、图纸、文宣专利等乙方应提供成果；外委验收意见（包括完成情况表及专家签名）。</label>
            </div>
        </div>


        <!-- 右侧悬浮按钮 -->
        <div class="right-bottom-btn">
            <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', 'upload'], function () {
        var form = layui.form,
            table = layui.table,
            layer = layui.layer,
            upload = layui.upload;
        var yfkje = [[${yfkje}]];
        const fkje = parseFloat(yfkje.fkje)
        const bzje = parseFloat(yfkje.bzje)
        console.log(fkje+bzje)
            console.log(yfkje.fkje)
            console.log(yfkje.bzje)
        var xmApproval = parent.getXmApproval();
        $("#fysq").val(xmApproval.fysq);
        $("#fysqmc").val(xmApproval.fysqmc);
        $("#rws").val(xmApproval.rws);
        $("#rwsmc").val(xmApproval.rwsmc);

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

        // 用于存放合同文件
        var dataHtwj = new Array();

        //监听合同
        form.on('select(ht)', function (data) {

            console.log('这个'+ data);
            var len = $("#fkjh")[0].length;
            while (len > 1) {
                $("#fkjh")[0].options[--len].remove();
            }

            if (data.value == "") {
                $("#htje").val("");
            } else {
                var htList = [[${htList}]];
                for (let ht of htList) {
                    if (ht.id == data.value) {
                        $("#htje").val(ht.htje);
                        var wj = JSON.parse(ht.wj);
                        var yszl = JSON.parse(ht.yszl);
                        dataHtwj.splice(0, dataHtwj.length)
                        dataYszl.splice(0, dataYszl.length)
                        dataHtwj.push.apply(dataHtwj, wj);
                        dataYszl.push.apply(dataYszl, yszl);
                        console.log(currTable1)
                        currTable1.reload();
                        currTable2.reload();
                        break;
                    }
                }

                $.ajax({
                    url: AjaxUtil.ctx + 'prjPayplan/list/' + data.value,
                    type: "post",
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        while (len > 1)
                            $("#fkjh")[0].options[--len].remove();

                        var dataFkjh = result.data.rows;
                        for (let item of dataFkjh) {
                            $("#fkjh").append("<option value=" + item.id + ">" + item.jhmc + " / " + item.zfje + "</option>");
                        }
                    }
                });
            }
            $("#fkjh").val(0);
            form.render('select');

            return false;
        });


        //合同文件
        var currTable1 = table.render({
            elem: '#currentTableId1',
            data: dataHtwj,
            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: 10,
            page: false,
            skin: 'grid',
            done: function (res, curr, count) { // done为数据渲染完的回调

                console.log('currTable1渲染弯沉')
                console.log(dataHtwj)
            }
        });

        //文件上传
        upload.render({
            elem: '#upLoad1' //绑定元素
            , 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
                    };
                    dataHtwj.push(curData);
                    currTable1.reload();
                } else
                    layer.alert("上传文件出现错误！");
            }
            , error: function () {
                layer.alert("上传文件出现错误！");
            }
            , before: function (res) {
                let fileType = this.fileType;
                this.data.fileType = fileType;
            }
        });
        $("button[lay-filter='deleteFile1']").click(function () {
            if (dataHtwj.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 < dataHtwj.length; i++) {
                var item = dataHtwj[i];
                if (item.LAY_CHECKED) {//条件：选中
                    dataHtwj.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            //刷新表格
            currTable1.reload();
        });
        //双击（合同文件）
        table.on('rowDouble(currentTableFilter1)', function (obj) {
            window.open(obj.data.Url);
        });


        //验收资料
        var currTable2 = table.render({
            elem: '#currentTableId2',
            data: dataYszl,
            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: 10,
            page: false,
            skin: 'grid',
            done: function (res, curr, count) { // done为数据渲染完的回调
                console.log('currTable2渲染弯沉')
            }
        });

        //文件上传
        upload.render({
            elem: '#upLoad2' //绑定元素
            , 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
                    };
                    dataYszl.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='deleteFile2']").click(function () {
            if (dataYszl.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 < dataYszl.length; i++) {
                var item = dataYszl[i];
                if (item.LAY_CHECKED) {//条件：选中
                    dataYszl.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            //刷新表格
            currTable2.reload();
        });
        $("button[lay-filter='deleteFile1']").click(function () {
            if (dataHtwj.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 < dataHtwj.length; i++) {
                var item = dataHtwj[i];
                if (item.LAY_CHECKED) {//条件：选中
                    dataHtwj.splice(i, 1);//移除后后造成数组下标索引发生变化，所以下面需要i--
                    i--;
                }
            }
            //刷新表格
            currTable1.reload();
        });
        //双击（验收资料）
        table.on('rowDouble(currentTableFilter2)', function (obj) {
            window.open(obj.data.Url);
        });

        // 监听确认按钮
        form.on('submit(saveBtn)', function (data) {
            // 合同金额
            const  htje = document.getElementById('htje').value;
            var selectElement = document.getElementById("fkjh");
            var selectedText = selectElement.options[selectElement.selectedIndex].text.split('/');
            const bcfkje = parseFloat(document.getElementById('bcfkje').value);
            if (bcfkje > selectedText[1].replace(/\s+/g, '')){
                Message.warning('本次付款金额，不能大于付款计划金额！',2000);
                return ;
            }
            if (fkje + bcfkje > bzje){
                Message.warning('该报账下付款申请已申请总金额已超出报账金额!',2000);
                return ;
            }
            //合同文件
            var dataWB_Htwj = [];
            for (const item of dataHtwj) {
                var dfWb = {
                    "id": item.id,
                    "wjm": item.wjm,
                    "Url": item.Url,
                    "scrq": item.scrq,
                    "sm": item.sm
                };
                dataWB_Htwj.push(dfWb);
            }
            $("#htwj").val(JSON.stringify(dataWB_Htwj));
            data.field.htwj = JSON.stringify(dataWB_Htwj);
            console.log(data.field);

            //验收资料
            var dataWB_Yszl = [];
            for (const item of dataYszl) {
                var dfWb = {
                    "id": item.id,
                    "wjm": item.wjm,
                    "Url": item.Url,
                    "scrq": item.scrq,
                    "sm": item.sm
                };
                dataWB_Yszl.push(dfWb);
            }
            $("#yszl").val(JSON.stringify(dataWB_Yszl));
            data.field.yszl = JSON.stringify(dataWB_Yszl);
            console.log(data.field);

            var index = layer.load(0, {shade: 0.1});
            AjaxUtil.post({
                url: AjaxUtil.ctx + "staPayapproval/doAdd",
                data: data.field,
                success: function (res) {
                    layer.close(index);
                    if (res.code === 0) {
                        parent.setUpdateFlag();
                    } else {
                        // Message.error(res.message, 1000);
                    }
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                },
                error: function (error) {
                    Message.error(error.message, 1000)
                }
            });

            return false;
        });

        // 监听取消按钮
        $("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>