<!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">
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label" style="width: 70px">序号</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="text" id="xh" name="xh" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label required" style="width: 70px">出差人</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="hidden" id="ccr" name="ccr">
                <input type="text" id="ccrxm" name="ccrxm" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label required" style="width: 70px">出差地点</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="text" id="ccdd" name="ccdd" class="layui-input" readonly>
            </div>
            <label class="layui-form-label required" style="width: 70px">出差天数</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="ccts" name="ccts" min="0" 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: 70px">起止时间</label>
            <div class="layui-input-inline" style="width: 122px">
                <input type="text" name="qsrq" id="qsrq" placeholder="起始日期" lay-verify="required" lay-reqtext="开始日期不能为空"
                       autocomplete="off" class="layui-input" readonly>
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline" style="width: 122px">
                <input type="text" name="jsrq" id="jsrq" placeholder="结束日期(含)" lay-verify="required"
                       lay-reqtext="结束日期不能为空" autocomplete="off" class="layui-input" readonly>
            </div>
            <label class="layui-form-label" style="width: 70px">交通费(元)</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="jtf" name="jtf" lay-filter="jtf" min="0" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label" style="width: 70px">住宿费(元)</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="zsf" name="zsf" lay-filter="zsf" min="0" autocomplete="off"
                       class="layui-input">
            </div>
            <label class="layui-form-label" style="width: 70px">差补(元)</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="cb" name="cb" lay-filter="cb" min="0" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-inline" style="width: 100%">
            <label class="layui-form-label" style="width: 70px">总计(元)</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="zj" name="zj" min="0" autocomplete="off" class="layui-input" readonly>
            </div>
            <label class="layui-form-label" style="width: 70px">日平均(元)</label>
            <div class="layui-input-inline" style="width: 270px">
                <input type="number" id="rpj" name="rpj" min="0" autocomplete="off" class="layui-input" readonly>
            </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 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 = /*[[@{/}]]*/'';
    layui.use(['form', 'tableSelect', 'laydate'], function () {
        var form = layui.form,
            laydate = layui.laydate;

        var expense = parent.getTravelExpense();
        $("#xh").val(expense.xh);
        $("#ccr").val(expense.ccr);
        $("#ccrxm").val(expense.ccrxm);
        $("#qsrq").val(expense.qsrq);
        $("#jsrq").val(expense.jsrq);
        $("#ccts").val(expense.ccts);
        $("#ccdd").val(expense.ccdd);
        $("#jtf").val(expense.jtf);
        $("#zsf").val(expense.zsf);
        $("#cb").val(expense.cb);
        $("#zj").val(expense.zj);
        $("#rpj").val(expense.rpj);
        form.render();

        var qsrq = $("#qsrq").val(),
            jsrq = $("#jsrq").val();
        var ccts = $("#ccts").val;
        var jtf = parseFloat($("#jtf").val()),
            zsf = parseFloat($("#zsf").val()),
            cb = parseFloat($("#cb").val()),
            zj = parseFloat($("#zj").val()),
            rpj = parseFloat($("#rpj").val());
        if (isNaN(jtf))
            jtf = 0;
        if (isNaN(zsf))
            zsf = 0;
        if (isNaN(cb))
            cb = 0;


        function setCtrlValue() {
            //总计
            zj = jtf + zsf + cb;
            if (zj == 0) {
                $("#zj").val("");
                $("#rpj").val("");
            } else {
                $("#zj").val(zj.toFixed(2));
                //出差天数
                ccts = parseFloat($("#ccts").val());
                if (isNaN(ccts)) {
                    ccts = 0;
                    $("#rpj").val("");
                } else {//日平均
                    rpj = zj / parseFloat(ccts);
                    $("#rpj").val(rpj.toFixed(2));
                }
            }
        }

        //日期
        laydate.render({
            elem: '#qsrq',
            done: function (value, date, endDate) {
                qsrq = $("#qsrq").val();
                jsrq = $("#jsrq").val();
                if (qsrq != "" && jsrq != "") {
                    if (qsrq > jsrq) {
                        Message.warning("起始日期不能晚于结束日期！", 1500);
                        $("#qsrq").val("");
                    } else {
                        var qsDate = new Date(qsrq);
                        var jsDate = new Date(jsrq);
                        var day = jsDate.getTime() - qsDate.getTime();
                        var time = parseInt(day / (1000 * 60 * 60 * 24)) + 1;
                        if (time < 1) {
                            $("#ccts").val("");
                            $("#rpj").val("");
                        } else {
                            $("#ccts").val(time);
                            setCtrlValue();
                        }
                    }
                }
            }
        });
        laydate.render({
            elem: '#jsrq',
            done: function (value, date, endDate) {
                qsrq = $("#qsrq").val();
                jsrq = $("#jsrq").val();
                if (qsrq != "" && jsrq != "") {
                    if (qsrq > jsrq) {
                        Message.warning("结束日期不能早于起始日期！", 1500);
                        $("#jsrq").val("");
                    } else {
                        var qsDate = new Date(qsrq);
                        var jsDate = new Date(jsrq);
                        var day = jsDate.getTime() - qsDate.getTime();
                        var time = parseInt(day / (1000 * 60 * 60 * 24)) + 1;
                        if (time < 1) {
                            $("#ccts").val("");
                            $("#rpj").val("");
                        } else {
                            $("#ccts").val(time);
                            setCtrlValue();
                        }
                    }
                }
            }
        });
        //出差天数
        $("#ccts").on("input", function (e) {
            setCtrlValue();
        });
        //交通费
        $("#jtf").on("input", function (e) {
            //获取input输入的值
            jtf = parseFloat(e.delegateTarget.value);
            if (isNaN(jtf))
                jtf = 0;

            setCtrlValue();
        });
        //住宿费
        $("#zsf").on("input", function (e) {
            //获取input输入的值
            zsf = parseFloat(e.delegateTarget.value);
            if (isNaN(zsf))
                zsf = 0;

            setCtrlValue();
        });
        //差补
        $("#cb").on("input", function (e) {
            //获取input输入的值
            cb = parseFloat(e.delegateTarget.value);
            if (isNaN(cb))
                cb = 0;

            setCtrlValue();
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            qsrq = $("#qsrq").val();
            jsrq = $("#jsrq").val();
            if (qsrq > jsrq)
                Message.error("结束日期不能早于起始日期！", 1500);
            else {
                zj = parseFloat($("#zj").val());
                if (isNaN(zj))
                    Message.error("差旅费不能为空！", 1500);
                else {
                    parent.setTravelExpense(data.field);
                    parent.setUpdateFlag();
                    parent.layer.close(parent.layer.getFrameIndex(window.name));// 关闭子窗口
                }
            }
        });

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