<!--##############################指派快递员共享模块####################################-->
<!--选择快递员模态框-->
<div>
    <div class="modal fade" id="id_order_model_quite" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
         data-backdrop="static">
        <div class="modal-dialog" role="document">
            <div class="modal-content" style="background-color: #f5f6fa">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel_quite">快捷下单</h4>
                </div>
                <div class="modal-body">
                    <form id="id_form_model_quick">
                        <div class="form-group">
                            <label class="control-label">选择订单模板<span style="color: red">*</span></label>
                            <select id="id_order_model_select_quite" class="selectpicker form-control"
                                    data-live-search="true">
                                <option value="" selected="selected">请选择</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label class="control-label">选择下单用户<span style="color: red">*</span></label>
                            <select id="id_order_model_select_quite_player" class="selectpicker form-control"
                                    data-live-search="true">
                                <option value="" selected="selected">请选择</option>
                            </select>
                        </div>
                        <div class="panel panel-primary">
                            <div class="panel-heading" role="tab" id="headingOne">
                                <h4 class="panel-title">
                                    <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne"
                                       aria-expanded="true" aria-controls="collapseOne">
                                        寄件人信息
                                    </a>
                                </h4>
                            </div>
                            <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel"
                                 aria-labelledby="headingOne">
                                <div class="panel-body">
                                    <div class="form-group">
                                        <label class="control-label">寄件人姓名<span style="color: red">*</span></label>
                                        <input id="id_model_quick_mailingName" type="text" class="form-control"
                                               placeholder="请输入寄件人姓名">
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label">寄件电话<span style="color: red">*</span></label>
                                        <input id="id_model_quick_mailingPhone" type="text" class="form-control"
                                               placeholder="请输入寄件电话">
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label">寄件地址<span style="color: red">*</span></label>
                                        <input type="hidden" id="id_address_model_mailingAddress_new">
                                        <input id="id_address_model_mailingAddress_new_show" type="text"
                                               class="form-control" placeholder="请输入寄件地址">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="panel panel-cyan">
                            <div class="panel-heading" role="tab" id="headingTwo">
                                <h4 class="panel-title">
                                    <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion"
                                       href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
                                        收件人信息
                                    </a>
                                </h4>
                            </div>
                            <div id="collapseTwo" class="panel-collapse collapse in" role="tabpanel"
                                 aria-labelledby="headingTwo">
                                <div class="panel-body">
                                    <div class="form-group">
                                        <label class="control-label">收件人姓名<span style="color: red">*</span></label>
                                        <input id="id_model_quick_receiptName" type="text" class="form-control"
                                               placeholder="请输入收件人姓名">
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label">收件电话<span style="color: red">*</span></label>
                                        <input id="id_model_quick_receiptPhone" type="text" class="form-control"
                                               placeholder="请输入收件电话">
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label">收件地址<span style="color: red">*</span></label>
                                        <input type="hidden" id="id_address_model_receiptAddress_new">
                                        <input id="id_address_model_receiptAddress_new_show" type="text"
                                               class="form-control" placeholder="请输入收件地址">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="control-label">物品重量(KG)：</label>
                            <input id="id_address_model_cargoWeight" type="text" class="form-control"
                                   placeholder="请输入物品重量" value="1">
                        </div>
                        <div class="form-group">
                            <label class="control-label">订单备注：</label>
                            <textarea id="id_model_quick_orderRemarks" type="text" class="form-control"
                                      placeholder="请输入订单备注(200字以内)" value="1" maxlength="220"></textarea>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button id="id_button_model_quick_ones" type="button" class="btn btn-info" data-toggle="tooltip" data-placement="top"
                            title="下单后清空输入，且关闭下单窗口" onclick="onesOrder()" data-loading-text="处理中...">单次下单
                    </button>
                    <button id="id_button_model_quick_money" type="button" class="btn btn-cyan" data-toggle="tooltip" data-placement="top"
                            title="下单后保持输入内容，且不关闭下单窗口" onclick="manyOrder()" data-loading-text="处理中...">连续下单
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    /**
     * 指派订单
     */
    function quiteOrder() {
        //getTableSelectOne();
        $("#id_order_model_quite").modal("show");
    }

    /**
     * 为选择订单模板绑定搜索输入事件
     */
    $("#id_order_model_select_quite").on('shown.bs.select', function (e) {
        //获取输入框对象
        let search_input = $(this).next().next().find("input");
        search_input.attr('placeholder', "请输入模板号或模板名称");
        if ($("search_input option").length == 0) {
            $.ajax({
                data: {key: search_input.val()},
                type: 'get',
                url: "${ctx}/admin/order/template/get/list",
                success: function (data) {
                    if (data.status != 200) {
                        lightyear.notify("系统繁忙，请尝试重新登陆。", 'danger');
                        return;
                    }
                    $("#id_order_model_select_quite").empty();
                    if (data.body.length > 0) {
                        console.log(data);
                        $.each(data.body, function (i, item) {
                            let tempId = '<option  value="' + item.id + '">模板名称：' + item.templateName + '&nbsp;&nbsp;&nbsp;&nbsp;模板号：' + item.templateNum + '</option>';
                            $("#id_order_model_select_quite").append(tempId);
                        });
                    } else {
                        lightyear.notify("没有可选择的订单模板，请先添加订单模板！", 'warning');
                    }
                    $('#id_order_model_select_quite').selectpicker('refresh');
                }
            });
        }
        search_input.keyup(function () {
            setTimeout(function () {
                $.ajax({
                    data: {key: search_input.val()},
                    type: 'get',
                    url: "${ctx}/admin/order/template/get/list",
                    success: function (data) {
                        if (data.status != 200) {
                            lightyear.notify("系统繁忙，请尝试重新登陆。", 'danger');
                            return;
                        }
                        $("#id_order_model_select_quite").empty();
                        if (data.body.length > 0) {
                            $.each(data.body, function (i, item) {
                                let tempId = '<option  value="' + item.id + '">模板名称：' + item.templateName + '&nbsp;&nbsp;&nbsp;&nbsp;模板号：' + item.templateNum + '</option>';
                                $("#id_order_model_select_quite").append(tempId);
                            });
                        }
                        $('#id_order_model_select_quite').selectpicker('refresh');
                    }
                });
            }, 1500);

        });
    });
    /**
     * 为选择下单用户绑定搜索输入事件
     */
    $("#id_order_model_select_quite_player").on('shown.bs.select', function (e) {
        //获取输入框对象
        let search_input = $(this).next().next().find("input");
        search_input.attr('placeholder', "请输入客户姓名或客户电话");
        if ($("search_input option").length == 0) {
            $.ajax({
                data: {key: search_input.val()},
                type: 'get',
                url: "${ctx}/customer/search/list",
                success: function (data) {
                    if (data.status != 200) {
                        lightyear.notify("系统繁忙，请尝试重新登陆。", 'danger');
                        return;
                    }
                    $("#id_order_model_select_quite_player").empty();
                    if (data.body.length > 0) {
                        console.log(data);
                        $.each(data.body, function (i, item) {
                            let tempId = '<option  value="' + item.id + '">用户名称：' + item.customerName + '&nbsp;&nbsp;&nbsp;&nbsp;客户电话：' + item.customerPhone + '</option>';
                            $("#id_order_model_select_quite_player").append(tempId);
                        });
                    } else {
                        lightyear.notify("没有匹配的用户，请重新搜索！", 'warning');
                    }
                    $('#id_order_model_select_quite_player').selectpicker('refresh');
                }
            });
        }
        search_input.keyup(function () {
            setTimeout(function () {
                $.ajax({
                    data: {key: search_input.val()},
                    type: 'get',
                    url: "${ctx}/customer/search/list",
                    success: function (data) {
                        if (data.status != 200) {
                            lightyear.notify("系统繁忙，请尝试重新登陆。", 'danger');
                            return;
                        }
                        $("#id_order_model_select_quite_player").empty();
                        if (data.body.length > 0) {
                            $.each(data.body, function (i, item) {
                                let tempId = '<option  value="' + item.id + '">用户名称：' + item.customerName + '&nbsp;&nbsp;&nbsp;&nbsp;客户电话：' + item.customerPhone + '</option>';
                                $("#id_order_model_select_quite_player").append(tempId);
                            });
                        }
                        $('#id_order_model_select_quite_player').selectpicker('refresh');
                    }
                });
            }, 1500);

        });
    });

    /**
     * 单次下单
     */
    function onesOrder() {
        const data = checkForm();
        if(data){
            $('#id_button_model_quick_ones').button('loading');
            runAjax("${ctx!}/order/table/model/form", "POST", data, function (res) {
                $('#id_button_model_quick_ones').button('reset');
                if (res.status != 200) {
                    lightyear.notify(res.message, 'danger');
                    return;
                }
                //tableReset('id_form_model_quick');
                $("#id_order_model_quite").modal("hide");
                lightyear.notify("下单完成！", 'success');
                search();
            });
        }
    }

    /**
     * 连续下单
     */
    function manyOrder() {
        const data = checkForm();
        if(data){
            $('#id_button_model_quick_money').button('loading');
            runAjax("${ctx!}/order/table/model/form", "POST", data, function (res) {
                $('#id_button_model_quick_money').button('reset');
                if (res.status != 200) {
                    lightyear.notify(res.message, 'danger');
                    return;
                }
                lightyear.notify("下单完成！", 'success');
                search();
            });
        }


    }

    function checkForm() {
        let data = {
            templateId: $("#id_order_model_select_quite").val(),
            playerId: $("#id_order_model_select_quite_player").val(),
            mailingName: $("#id_model_quick_mailingName").val(),
            mailingPhone: $("#id_model_quick_mailingPhone").val(),
            mailingAddress: $("#id_address_model_mailingAddress_new").val(),

            receiptName: $("#id_model_quick_receiptName").val(),
            receiptPhone: $("#id_model_quick_receiptPhone").val(),
            receiptAddress: $("#id_address_model_receiptAddress_new").val(),
            cargoWeight: $("#id_address_model_cargoWeight").val(),
            orderRemarks: $("#id_model_quick_orderRemarks").val(),
        }
        console.log(data);
        if (!data.templateId) {
            lightyear.notify("请选择订单模板！", 'danger');
            return false;
        }
        if (!data.playerId) {
            lightyear.notify("请选择下单用户！", 'danger');
            return false;
        }
        if (!data.mailingName) {
            lightyear.notify("请输入寄件人姓名！", 'danger');
            return false;
        }
        if (!data.mailingPhone) {
            lightyear.notify("请输入寄件电话！", 'danger');
            return false;
        }
        if (!data.mailingAddress) {
            lightyear.notify("请输入寄件地址！", 'danger');
            return false;
        }
        if (!data.receiptName) {
            lightyear.notify("请输入收件人姓名！", 'danger');
            return false;
        }
        if (!data.receiptPhone) {
            lightyear.notify("请输入收件电话！", 'danger');
            return false;
        }
        if (!data.receiptAddress) {
            lightyear.notify("请输入收件地址！", 'danger');
            return false;
        }
        return data;
    }
</script>
<script>
    $(document).ready(function () {
        //加载高德输入提示插件
        AMap.plugin('AMap.Autocomplete', function () {
            let autoComplete = new AMap.Autocomplete({});
            $("#id_address_model_mailingAddress_new_show").typeahead({
                //设置输入提示数据源
                source: function (query, process) {
                    let locationStr = $("#id_address_model_mailingAddress_new").val();
                    if (locationStr) {
                        let address = JSON.parse(locationStr).address;
                        if (address.length > query.length) {
                            $("#id_address_model_mailingAddress_new").val("");
                            return;
                        }
                        //如输入地址中不包含pio地址，清除定位信息
                        if (query.indexOf(address) < 0) {
                            $("#id_address_model_mailingAddress_new").val("");
                        }
                    }
                    return autoComplete.search(query, function (status, result) {
                        if ("complete" != status) {
                            return;
                        }
                        //返回数据
                        return process(result.tips);
                    });
                },
                //搜索匹配规则，在这里直接返回true，展示高德查询到的所有数据
                matcher: function (item) {
                    return true;
                },
                //格式化输入数据源
                displayText: function (item) {
                    return item.district + item.name;
                },
                //选择选中项目触发
                updater: function (item) {
                    //使用高德pio构建地址对象
                    let pio = {};
                    pio.location = {};
                    pio.address = item.district + item.name;
                    pio.location = item.location;
                    //设置到hidden输入框
                    $("#id_address_model_mailingAddress_new").val(JSON.stringify(pio));
                    return item.district + item.name;
                },
                //延迟查询
                delay: 300
            });
        });
        //失去焦点时候更新地址
        $("#id_address_model_mailingAddress_new_show").blur(function () {
            let pioStr = $("#id_address_model_mailingAddress_new").val();
            let inputStr = $("#id_address_model_mailingAddress_new_show").val();
            //输入为空，则删除位置信息
            if (!inputStr) {
                $("#id_address_model_mailingAddress_new").val("");
                return;
            }
            let pio = {}
            if (!pioStr) {
                pio.address = inputStr;
            } else {
                pio = JSON.parse(pioStr);
                pio.address = inputStr;
            }
            $("#id_address_model_mailingAddress_new").val(JSON.stringify(pio));
        });
    });


    $(document).ready(function () {
        //加载高德输入提示插件
        AMap.plugin('AMap.Autocomplete', function () {
            let autoComplete = new AMap.Autocomplete({});
            $("#id_address_model_receiptAddress_new_show").typeahead({
                //设置输入提示数据源
                source: function (query, process) {
                    let locationStr = $("#id_address_model_receiptAddress_new").val();
                    if (locationStr) {
                        let address = JSON.parse(locationStr).address;
                        if (address.length > query.length) {
                            $("#id_address_model_receiptAddress_new").val("");
                            return;
                        }
                        //如输入地址中不包含pio地址，清除定位信息
                        if (query.indexOf(address) < 0) {
                            $("#id_address_model_receiptAddress_new").val("");
                        }
                    }
                    return autoComplete.search(query, function (status, result) {
                        if ("complete" != status) {
                            return;
                        }
                        //返回数据
                        return process(result.tips);
                    });
                },
                //搜索匹配规则，在这里直接返回true，展示高德查询到的所有数据
                matcher: function (item) {
                    return true;
                },
                //格式化输入数据源
                displayText: function (item) {
                    return item.district + item.name;
                },
                //选择选中项目触发
                updater: function (item) {
                    //使用高德pio构建地址对象
                    let pio = {};
                    pio.location = {};
                    pio.address = item.district + item.name;
                    pio.location = item.location;
                    //设置到hidden输入框
                    $("#id_address_model_receiptAddress_new").val(JSON.stringify(pio));
                    return item.district + item.name;
                },
                //延迟查询
                delay: 300
            });
        });
        //失去焦点时候更新地址
        $("#id_address_model_receiptAddress_new_show").blur(function () {
            let pioStr = $("#id_address_model_receiptAddress_new").val();
            let inputStr = $("#id_address_model_receiptAddress_new_show").val();
            //输入为空，则删除位置信息
            if (!inputStr) {
                $("#id_address_model_receiptAddress_new").val("");
                return;
            }
            let pio = {}
            if (!pioStr) {
                pio.address = inputStr;
            } else {
                pio = JSON.parse(pioStr);
                pio.address = inputStr;
            }
            $("#id_address_model_receiptAddress_new").val(JSON.stringify(pio));
        });
    });

    /**
     * 业务退款
     **/
    function orderRefund(){
        let clm=getTableSelectOne();
        $.confirm({
            title: '退款确认',
            content: '您即将为该订单退款，成功后订单将被置为已退款状态，请确保和用户协商一致，是否继续？',
            type: 'blue',
            typeAnimated: true,
            buttons: {
                tryAgain: {
                    text: '继续',
                    btnClass: 'btn-blue',
                    action: function(){
                        sendAjax("${ctx}/tpay/Wait/order/Refund","get",{orderId: clm.orderId});
                    }
                },
                close: {
                    text: '放弃'
                }
            }
        });
    }
</script>