<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>备件申请-待出库</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="decorator" content="base" />
    <link rel="stylesheet" type="text/css" href="${ctxPlugin}/static/css/reset.css" />
    <style>
        html{
            background-color: #fff;
        }
    </style>
</head>
<body>
<div class="layui-form">
    <div class="layui-card operate-footer mb-40 ml-15" style="text-align: left;">
        <span id="paigongzhi">指派至：</span>
    </div>
    <div class="layui-form-item operate-footer" style="text-align: center;">
        <input type="button" lay-submit lay-filter="sureSubmit" id="sureSubmit" value="确认指派" class="layui-btn">
    </div>
    <div class="layui-card-body pt-20 mb-70 h-350">
        <div class="" style="position: relative;">
            <div class=" pr-50">
                <input type="text" name="customerMobile" autocomplete="off" placeholder="请输入服务商名称" class="layui-input"  id="filterName">
            </div>
            <button type="button" class="layui-btn f-r chamobi" style="position:absolute;top:0px;right: 0; padding: 0 10px;"><i class="layui-icon fs-16">&#xe615;</i></button>
        </div>
            <table class="layui-table " >
                <colgroup>
                    <col width="30%">
                    <col width="20%">
                    <col width="30%">
                    <col width="20%">
                </colgroup>
                <thead>
                <tr>
                    <th>服务商名称</th>
                    <th>预计里程</th>
                    <th>选择</th>
                </tr>
                </thead>
                <tbody id="zhijiepaidan">
                <c:forEach items="${sites}" var="si">
                    <tr>
                        <td>${si.columns.name}</td>
                        <td>${si.columns.distance_formatted}</td>
                        <td> <div class="layui-input-block"><input type="radio" name="siteId" value="${si.columns.id}" lay-verify="filter"></div></td>
                    </tr>
                </c:forEach>

                </tbody>
            </table>

            <input type="hidden" id="orderId" value="${orderId}" name="orderId">

    </div>

</div>

<script src="${ctxPlugin}/lib/layuiAdmin/dist/layuiadmin/layui/layui.js"></script>
<script>
    var where = '${where}';
    layui.use(['table','layer','form','jquery'], function(){
        $ = layui.$;
        table = layui.table;
        layer = layui.layer;
        form = layui.form;

       /* $('#filterName').keyup(function(){
            $('#zhijiepaidan tr').hide()
                .filter(":contains('" +($(this).val()) + "')").show();
            if(isBlank($(this).val())){
                $('#zhijiepaidan tr').show();
            }
        }).keyup();//DOM加载完时，绑定事件完成之后立即触发*/

        $(document).on('click','#zhijiepaidan td',function(){
            //去除全部选择
            $("#zhijiepaidan").find("input").prop('checked',false);
            $(this).parent("tr").find("input").prop('checked','checked');
            form.render('radio');
           $("#paigongzhi").html("指派至："+$(this).parent("tr").find("td").eq(0).html());
        });
        var checkOrder = false;
        form.on('submit(sureSubmit)', function (data) {
            $("#sureSubmit").attr("disabled","true");
            if(checkOrder){
                return false;
            }
            var dat = data.field;
            console.log(dat)
            var siteId = dat.siteId;
            var orderId = dat.orderId;
            if(isBlank(siteId)){
                layer.msg('请选择服务商', {icon:2});
                return false
            }
            checkOrder = true;
            $.ajax({
                type:"POST",
                url:"${ctx}/dispatch/dispatchOrderSite",
                data:{
                    orderId:orderId,
                    siteId:siteId
                },
                success:function(result){
                    checkOrder = false;
                    if(result.code ==200){
                        parent.layer.msg('操作成功！', {icon:1,time:1000},function(){
                            parent.layer.closeAll();
                            if (where == '1') {
                                parent.parent.location.reload();
                            } else {
                                parent.location.reload();
                            }
                        });
                    }else if(result.code == "505"){
                        layer.msg('保存失败!', {icon:2,time:1000},function(){
                            layer.closeAll()
                        });
                    }else{
                        layer.msg('保存失败,数据有误', {icon:2,time:1000});
                    }
                }
            });
            return false;
        })

        $(".chamobi").on('click',function(){
            var filterName = $("#filterName").val();
            console.log(filterName)
            if(isBlank(filterName)){
                layer.msg("请输入服务商名称");
            }else if(filterName.length >4){
                //查询服务商
                var orderId = $("#orderId").val();
                var html="";
                $.ajax({
                    type:"POST",
                    url:"${ctx}/dispatch/getOrderConditionSiteList",
                    data:{
                        orderId:orderId,
                        siteName:filterName.trim()
                    },
                    success:function(data){
                        console.log(data)
                        if(data.code == '202'){
                            layer.msg("数据有误，请刷新页面");
                        }else{
                            $.each(data,function(index,si){
                                console.log(si)
                                html +='<tr>';
                                html +='<td>'+si.columns.name+'</td>';
                                html +='<td>'+si.columns.distance_formatted+'</td>';
                                html +='<td> <div class="layui-input-block"><input type="radio" name="siteId" value="'+si.columns.id+'" lay-verify="filter"></div></td>';
                                html +=' </tr>';
                            });
                            $("#zhijiepaidan").empty().append(html);
                            form.render('radio');
                        }

                    }
                })
            }else{
                layer.msg("输入服务商名称字符长度不够");
            }

        })

    });



    function isBlank(val) {
        if (val == null || val == '' || val == undefined) {
            return true;
        }
        return false;
    }
</script>
</body>
</html>

