<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" th:href="@{/images/logo.jpg}"/>
    <link rel="stylesheet" th:href="@{/js/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/js/layui/css/admin.css}">
    <script type="text/javascript" th:src="@{/js/jquery-3.3.1.min.js}"></script>
    <script th:src="@{/js/layui/layui.js}" charset="utf-8"></script>
    <style>
        img {
            width: 60px;
        }

    </style>
</head>
<body>
<div class="layui-form" style="padding: 20px 0 0 0;">
    <form id="formId" onsubmit="return false;">

        <div class="layui-form-item">
            <label class="layui-form-label">订单编号</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="orderId" th:value="${obj.orderId}" readonly>
            </div>
            <label class="layui-form-label">订单状态</label>
            <div class="layui-input-inline">
                <select name="orderState" id="orderState" lay-filter="orderState" lay-search>
                    <option value="1">待发货</option>
                    <option value="2">待收货</option>
                    <option value="3">租赁中</option>
                    <option value="4">已完成</option>
                </select>
            </div>

            <label class="layui-form-label">商品总数</label>
            <div class="layui-input-inline">
                <input type="number" class="layui-input" name="goodsNum" th:value="${obj.goodsNum}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单总额</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" class="layui-input" name="totalAmount" th:value="${obj.totalAmount}">
            </div>

            <label class="layui-form-label">已付金额</label>
            <div class="layui-input-inline">
                <input type="number" step="0.01" class="layui-input" name="payAmount" th:value="${obj.payAmount}">
            </div>

            <label class="layui-form-label">快递编号</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="trackingNum" th:value="${obj.trackingNum}">
            </div>
        </div>

        <div class="layui-form-item" id="leaseShow">
            <label class="layui-form-label">租赁天数</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="leaseDay" th:value="${obj.leaseDay}">
            </div>

            <label class="layui-form-label">租赁开始</label>
            <div class="layui-input-inline">
                <input class="layui-input" id="startDate" name="leaseStartDate" type="text" placeholder="yyyy-MM-dd"
                       th:value="${obj.leaseStartDate}">
            </div>

            <label class="layui-form-label">租赁到期</label>
            <div class="layui-input-inline">
                <input class="layui-input" id="leaseEndDate" name="leaseEndDate" type="text" placeholder="yyyy-MM-dd"
                       th:value="${obj.leaseEndDate}">
            </div>

        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">配送方式</label>
            <div class="layui-input-inline">
                <select name="deliveryId" id="deliveryId" lay-filter="delivery">
                    <option th:each="list:${deliveryList}" th:value="${list.id}"
                            th:text="${list.name }"
                            th:selected="${list.id}==${obj.deliveryId}"></option>
                </select>
            </div>
        </div>

        <div id="receiver">

            <div class="layui-form-item">
                <label class="layui-form-label">收货人姓名</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="receiverName"
                           th:value="${obj.receiverName}">
                </div>

                <label class="layui-form-label">收货人电话</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="receiverPhone" th:value="${obj.receiverPhone}">
                </div>

                <label class="layui-form-label">省</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="province" th:value="${obj.province}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">市</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="city"
                           th:value="${obj.city}">
                </div>

                <label class="layui-form-label">区/县</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="county" th:value="${obj.county}">
                </div>

                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" name="receiverAddress" th:value="${obj.receiverAddress}">
                </div>
            </div>
        </div>

        <div class="layui-form-item" id="take">
            <label class="layui-form-label">取货地址</label>
            <div class="layui-input-inline" style="width: 810px">
                <select name="mapCoordinateId" id="mapCoordinateId">
                    <option th:each="list:${mapCoordinateList}" th:value="${list.id}"
                            th:text="${list.address }"
                            th:selected="${list.id}==${obj.mapCoordinateId}"></option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">订单备注</label>
            <div class="layui-input-inline" style="width: 810px">
                <input type="text" class="layui-input" name="orderRemarks" th:value="${obj.orderRemarks}">
            </div>
        </div>


        <button class="layui-btn" style="display: none;" id="saveBtn" onclick="save()"></button>
    </form>
</div>
<script>

    layui.use('form', function () {
        var form = layui.form;
        form.render();
    });


    $(function () {
        var islease = '[[${obj.isLease}]]'
        var deliveryId = '[[${obj.deliveryId}]]'
        if (islease == 0) {
            $("#leaseShow").hide();
        }

        $("#orderState").find("option[value=" + '[[${obj.orderState}]]' + "]").attr("selected", true);

        if (deliveryId == 7) {
            $("#receiver").hide();
            $("#take").show();
        } else {
            $("#receiver").show();
            $("#take").hide();
        }
    })


    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#startDate',
            type: 'date',
            trigger: 'click',
        });
        laydate.render({
            elem: '#endDate',
            type: 'date',
            trigger: 'click',
        });
    });


    //联动下拉菜单
    layui.use('form', function () {
        var form = layui.form;
        form.on('select(delivery)', function (data) {
            var id = data.elem.value;

            if (id == 7) {
                $("#receiver").hide();
                $("#take").show();
            } else {
                $("#receiver").show();
                $("#take").hide();
            }

        });
    });


    var b = true;

    function save() {
        if (!b) {
            return false;
        }
        b = false;
        var form = document.querySelector("#formId");
        var formdata = new FormData(form);
        // formdata.append("orderId", '[[${obj.orderId}]]');
        $.ajax({
            url: "edit",
            data: formdata,
            type: "POST",
            dataType: "json",
            processData: false,
            contentType: false,
            success: function (result) {
                b = true;
                console.log( result.code)
                if (result.code == 100) {
                    layer.msg(result.msg, {icon: 6, time: 1000}, function () {
                        closeLayer();
                        parent.layui.table.reload('tableId');
                    });
                } else {
                    layer.msg(result.msg, {icon: 5});
                }
            }
        });
    }

    function closeLayer() {
        var index = parent.layer.getFrameIndex(window.name);
        //关闭当前frame
        parent.layer.close(index);
    }

</script>
</body>

</html>