{layout name="layout1" /}
<style>
    .layui-table-cell {
        height:auto;
    }
    .goods-content>div:not(:last-of-type) {
        border-bottom:1px solid #DCDCDC;
    }
    .goods-data::after{
        display: block;
        content: '';
        clear: both;
    }
    .goods_name_hide{
        overflow:hidden;
        white-space:nowrap;
        text-overflow: ellipsis;
    }
    .operation-btn {
        margin: 5px;
    }
    .table-operate{
        text-align: left;
        font-size:14px;
        padding:0 5px;
        height:auto;
        overflow:visible;
        text-overflow:inherit;
        white-space:normal;
        word-break: break-all;
    }
</style>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
        <div class="layui-collapse like-layui-collapse" lay-accordion="" style="border:1px dashed #c4c4c4">
            <div class="layui-colla-item">
                <h2 class="layui-colla-title like-layui-colla-title" style="background-color: #fff">操作提示</h2>
                <div class="layui-colla-content layui-show">
                    <p>*客户购买套餐等待时间创建订单的。</p>
                    <p>*订单的状态又执行成功，等待执行，请勿删除执行</p>
                </div>
            </div>
        </div>
        </div>
        <div class="layui-tab layui-tab-card" lay-filter="tab-all">

            <ul class="layui-tab-title">
                <li data-type='' class="layui-this">全部状态</li>
                <li data-type="0">待预约</li>
                <li data-type="1">进行中</li>
                <li data-type="3">暂停中</li>
                <li data-type="2">已完成</li>
            </ul>
            <div class="layui-tab-item layui-show">
                <div class="layui-card">
                    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                        <div class="layui-form-item">
                            <div class="layui-row">
                                <div class="layui-inline" style="width: 24%;">
                                    <label class="layui-form-label">执行编号:</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="id" id="id" placeholder="请输入订单编号"
                                               autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                
                                <div class="layui-inline" style="width: 24%;">
                                    <label class="layui-form-label">客户名称:</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="name" id="name" placeholder="请输入客户名称"
                                               autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                
                                <div class="layui-inline" style="width: 24%;">
                                    <label class="layui-form-label">手机号码:</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="phone" id="phone" placeholder="请输入客户手机号码"
                                               autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline" style="width: 24%;">
                                    <label class="layui-form-label">订单地区:</label>
                                    <div class="layui-input-block">
                                        <select name="area_id" id="order_type">
                                            <option value="">全部</option>
                                            <option value="520102">南明区</option>
                                            <option value="520103">云岩区</option>
                                            <option value="520111">花溪区</option>
                                            <option value="520112">乌当区</option>
                                            <option value="520113">白云区</option>
                                            <option value="520115">观山湖区</option>
                                            <option value="520122">息烽县</option>
                                            <option value="520123">修文县</option>
                                            <option value="520181">清镇市</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-inline" style="width: 24%;">
                                    <label class="layui-form-label">订单来源:</label>
                                    <div class="layui-input-block">
                                        <select name="pid" id="pid">
                                            <option value="">全部</option>
                                            {foreach $ordertypelist as $item }
                                            <option value="{$item.id}">{$item.name}</option>
                                            {/foreach}
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline" style="width: 24%;">
                                    <label class="layui-form-label">客服管家:</label>
                                    <div class="layui-input-block">
                                        <select name="admin" id="admin">
                                            <option value="">全部</option>
                                            {foreach $admin as $item }
                                            <option value="{$item.id}">{$item.name}</option>
                                            {/foreach}
                                        </select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">上单时间:</label>
                                    <div class="layui-input-inline">
                                        <div class="layui-input-inline" >
                                            <input type="text"  name="start_time" class="layui-input" id="start_time"
                                                   placeholder="" autocomplete="off">
                                        </div>
                                    </div>
                                    <div class="layui-input-inline" style="margin-right: 5px;width: 20px;">
                                        <label class="layui-form-mid">至</label>
                                    </div>
                                    <div class="layui-input-inline">
                                        <input type="text" name="end_time" class="layui-input" id="end_time"
                                               placeholder="" autocomplete="off">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <button class="layui-btn layui-btn-sm layuiadmin-btn-ad {$view_theme_color}" lay-submit
                                            lay-filter="order-search">查询
                                    </button>
                                    <button class="layui-btn layui-btn-sm layuiadmin-btn-ad layui-btn-primary " lay-submit
                                            lay-filter="order-clear-search">清空查询
                                    </button>
                                    <button class="layui-btn layui-btn-sm layuiadmin-btn-ad layui-btn-primary " lay-submit
                                            lay-filter="export-file">导出
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-card-body">
                        <table id="order-lists" lay-filter="order-lists"></table>
           
                        <script type="text/html" id="order-operation" >
                            <div class="table-operate">
                                <a class=" layui-btn-primary layui-btn-sm " lay-event="reservation" style="display: inline-block;">派单预约</a>
                                <a class=" layui-btn-primary layui-btn-sm " lay-event="orderinfolist" style="display: inline-block;margin-top:10px">查看预排</a>
                                {{# if(d.status == 1){ }}
                                <a class="layui-btn-primary layui-btn-sm " lay-event="prentsd" style="display: inline-block; margin-top:10px;color: #FF5722;">取消暂停</a>
                                {{# }else{ }}
                                <a class="layui-btn-primary layui-btn-sm " lay-event="print" style="display: inline-block; margin-top:10px">暂停服务</a>
                                {{#} }}
                                <a class=" layui-btn-primary layui-btn-sm " lay-event="delnumber" style="display: inline-block;margin-top:10px">划卡次数</a>
                                <a class="layui-btn layui-btn-danger layui-btn-sm " lay-event="log" style="display: inline-block;margin-top:10px">操作日志</a>
                            </div>
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
         <!--订单信息-->
            <script type="text/html" id="order">
                <div style="text-align: left">
                        <div style="font-size:16px;color:#1E9FFF">{{d.goods_name}}</div>
                        <p style="height:10px"></p>
                        <p>订单编号:{{d.order_sn}}</p>
                        <p>订单类型:{{d.brand}}</p>
                        <p>下单时间:{{d.create_time}}</p>
                        <p>订单来源:{{d.channel}}</p>
                    </div>
           </script>
           <script type="text/html" id="userinfo">
            <div style="text-align:left">
                    <img src="{{d.avatar}}" style="height:80px;width: 80px" class="image-show">
                    <p>客户姓名:{{d.consignee}}</p>
                    <p>联系电话:{{d.mobile}}</p>
                    <p>联系地址:{{d.address}}</p>
                    <p>{{d.wx}}</p>
                </div>
       </script>

       <script type="text/html" id="adder">
        <div style="text-align:left">
                <p>客户姓名:{{d.consignee}}</p>
                <p>联系电话:{{d.mobile}}</p>
                <p>上门地址:{{d.address}}</p>
                <p>房屋面积:{{d.areas}}平方
                    {{# if(d.pet==1){}}
                    有宠物
                    {{# }else{ }}
                    无宠物
                    {{# } }}
                    {{# if(d.hbl==1){}}
                    有超厚玻璃
                    {{# }else{ }}
                    无超厚玻璃
                    {{# } }}   
                </p>
                <p style="height:20px"></p>
                <div style="display: flex; justify-content: space-between; width:90%;font-weight: 550;"> 
                 <div>总次数:{{d.number}}</div>
                 <div>待使用:{{d.code}}</div>
                 <div>已使用：{{d.sycs}}</div>
                </div>
        </div>
        <div style="margin-top: 20px;">
            <a  lay-event="admin_remarks" style="display: inline-block;">
            {{# if(d.type==1){}}
           <p style="color: #FF5722;">  服务类型：固定{{d.order_remarks}}</p> 
            {{# }else{ }}
            服务类型：非固定
            {{# } }}  
           
            </a>
        </div>
   </script>


   <script type="text/html" id="reak">
    <div style="text-align:left">
    <a class="" lay-event="remarks" style="display: inline-block;">客户叮嘱:{{d.order_remarks}}</a> <br>

    <a  lay-event="admin_remarks" style="display: inline-block;">管理员备注：
        {{# if(d.admin_remarks){}}
        {{d.admin_remarks}}
        {{# }else{ }}
        -
        {{# } }}  
      </a><br>
    <a  lay-event="staff_remarks" style="display: inline-block;">保洁师叮嘱：
        {{# if(d.staff_remarks){}}
        {{d.staff_remarks}}
        {{# }else{ }}
        -
        {{# } }}  
      </a>
    </div>
</script>

<script type="text/html" id="static">
    <div style="text-align:left">
        {{# if(d.static==0){}}
         待预约
        {{# }
          else if(d.static==2) { }}
         已完成
          {{# }   
         else if(d.static==3) { }}
          暂停中
          {{# } 
        else{ }}
          服务中
        {{# } }}
    </div>
</script>

<script>
    layui.config({
        version:"{$front_version}",
        base: '/static/plug/layui-admin/dist/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'like', 'laydate'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , like = layui.like
            , element = layui.element
            , laydate = layui.laydate;
        var listType = '';
        //监听搜索
        form.on('submit(order-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('order-lists', {
                where: field,
                page: {
                    curr: 1
                }
            });
        });
    
        //清空查询
        form.on('submit(order-clear-search)', function () {
            $('#keyword').val('');
            $('#order_status').val('');
            $('#goods_name').val('');
            $('#pay_way').val('');
            $('#order_type').val('');
            $('#order_source').val('');
            $('#start_time').val('');
            $('#end_time').val('');
            $('#delivery_type').val('');
            form.render('select');
            //刷新列表
            table.reload('order-lists', {
                where: [],
                page: {
                    curr: 1
                }
            });
        });

        
        // 导出
        form.on('submit(export-file)', function(data){
            var field = data.field;
            $.ajax({
                url: '{:url("order/exportFile")}?type=' + listType,
                type: 'get',
                data: field,
                dataType: 'json',
                error: function() {
                    layer.msg('导出超时，请稍后再试!');
                },
                success: function(res) {
                    table.exportFile(res.data.exportTitle,res.data.exportData, res.data.exportExt, res.data.exportName);
                },
                timeout: 15000
            });
            layer.msg('导出中请耐心等待~');
        });

        //日期时间范围
        laydate.render({
            elem: '#start_time'
            ,type: 'datetime'
            ,trigger: 'click'
        });


        laydate.render({
            elem: '#end_time'
            ,type: 'datetime'
            ,trigger: 'click'
        });



        //获取列表
        getList(listType);
        //切换列表
        element.on('tab(tab-all)', function (data) {
            $('#keyword').val('');
            $('#order_status').val('');
            $('#goods_name').val('');
            $('#pay_way').val('');
            $('#order_type').val('');
            $('#start_time').val('');
            $('#end_time').val('');
            $('#delivery_type').val('');
            form.render('select');
            listType = $(this).attr('data-type');
            getList(listType);
            if (listType !== ''){
                $('.order_status').hide();
            }else {
                $('.order_status').show();
            }
        });
        function getDay(day = 0,datetime = '',num = 30){
            let data = []
            
            let date = new Date(datetime)
            //今日星期几
            let nowday = date.getDay()
            //需要加几天
            let addday = 0
            //计算的是星期几 - 获取到那天的时间戳
            if(day == 0  && nowday != day){
                addday = 7 - nowday
            }else{
                if(day != nowday){
                    if(day > nowday){
                        addday = day - nowday
                    }else{
                        addday = 7 - nowday + day
                    }
                } 
            }
            
            //获取当前 星期几的时间戳
            let nowtime = date.setDate(date.getDate()+addday)
            
            //映射星期几
            const weeks = ['日','一','二','三','四','五','六']
            
            for(let i = 1 ; i<= num ; i++){
                let _date = new Date(nowtime)
                nowtime = date.setDate(_date.getDate()+7)
                //时间戳
                data.push({
                    'autotime':parseInt(nowtime/1000),//时间戳 
                    'date':_date.getFullYear()+'-'+(_date.getMonth()+1)+'-'+_date.getDate(),
                    xq:'星期'+weeks[_date.getDay()]
                    
                })
            }
            return data
         //   console.log(data,addday,nowtime);
            
        }
        function getList(type) {
            table.render({
                elem: '#order-lists'
                , url: '{:url("Orderautomatic/lists")}?type=' + type
                , cols: [[
                    {field:'id',title: 'id',width:80,align: 'center'} 
                    , {field: 'phone', title: '用户信息', width:240, align: 'center',templet:'#userinfo'}
                    , {field: 'order', title: '订单商品信息', align: 'center', templet:'#order'}
                    , {field: 'adder', title: '服务人员及客户地址',templet:'#adder',width:320,}  
                    ,{field: 'autotime', title: '备注信息', align: 'center',templet:'#reak',}
                    , {field: 'static', title: '订单状态',width:120, align: 'center',templet:'#static',}
                    , {fixed: 'right', title: '操作', width: 120, align: 'center', toolbar: '#order-operation'}
                ]]
                , page: true 
                , text: {none: '暂无数据！'}
                , parseData: function (res) {
                    return {
                        "code": res.code,
                        "msg": res.msg,
                        "count": res.data.count,
                        "data": res.data.lists,
                    };
                }
                ,done: function(res, curr, count){
                    // 解决操作栏因为内容过多换行问题
                    $(".layui-table-main tr").each(function (index, val) {
                        $($(".layui-table-fixed-l .layui-table-body tbody tr")[index]).height($(val).height());
                        $($(".layui-table-fixed-r .layui-table-body tbody tr")[index]).height($(val).height());
                    });
                }
            });
        };
  
        

        //监听工具条
        table.on('tool(order-lists)', function (obj) {
            var id = obj.data.id;
            if(obj.event === 'detail'){
                layer.open({
                    type: 2
                    ,title: '订单详情'
                    ,content: '{:url("Ordersel/Orderinfo")}?id='+id
                    ,area: ['85%', '80%']
                    ,yes: function(index, layero){
                        table.reload('order-lists');
                    }
                })
            }

            //删除订单
            if(obj.event === 'del'){
                layer.confirm('删除后订单将消失，确认删除订单吗?', {
                    btn: ['确认','取消'] //按钮
                }, function(){
                    like.ajax({
                        url: '{:url("Orderautomatic/del")}?id='+id
                        , data: {'order_id': id}
                        , type: 'post'
                        , success: function (res) {
                            if (res.code == 1) {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                },function () {
                                    table.reload('order-lists');
                                });
                            }
                        },
                    });
                });
            }
           //查看预排订单
            if(obj.event === 'log'){
                var order_sn = obj.data.id;
               
                layer.open({
                    type: 2
                    ,title: '订单的操作记录'
                    ,content: '{:url("Orderautomatic/log")}?id='+order_sn
                    ,area: ['65%', '65%']
                    ,yes: function(index, layero){

                    }
                })
            }
            //编辑订单
            if(obj.event === 'reservation'){
               
                var order_sn = obj.data.order_sn;
                if(obj.data.type==1){
                    layui.layer.msg('固定单无法预约派单', {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                         })
                    return 
                }
                if(obj.data.code==0){
                    layui.layer.msg('订单已经完成', {
                            offset: '15px'
                            , icon: 2
                            , time: 1000
                         })
                    return 
                }


                var index = layer.open({
                    type: 2
                    ,title: '增加排单预约'
                    ,content: '{:url("Orderautomatic/add")}?id='+order_sn
                    ,area: ['55%', '80%']
                    ,btn: ['保存', '取消']
                    ,maxmin: true
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'add-user_level-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field;
                            if(data.field.end_time==""){
                                layui.layer.msg('服务日期不能为空', {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                }); 
                               return;
                            }
                            if(obj.data.code<data.field.number){
                                layui.layer.msg('订单服务次数不足', {
                                            offset: '15px'
                                            , icon: 2
                                            , time: 1000
                                }); 
                               return;
                            }
 
                            if(data.field.type==2){
                            let keys = Object.keys(field)
                            let arr1 = []
                            let arr2 = []
                            for(let d of keys){
                                if(d.indexOf('arr1[') >= 0){
                                    arr1.push(parseInt(d.slice(5,6)))
                                }
                            }
                            for(let d of keys){
                                if(d.indexOf('arr2[') >= 0){
                                    arr2.push(parseInt(d.slice(5,6)))
                                }
                            } 
                            console.log('获取到数组1的长度',arr1.length)
                            if(arr1.length){
                                let arr1_data = []
                                for(let d of arr1){
                                    console.log(d)
                                    arr1_data.push(getDay(d,field.end_time,obj.data.code)) 
                                }
                                field.arr1_data = arr1_data
                            }
                            if(arr2.length){
                                let arr2_data = []
                                for(let d of arr2){
                                    arr2_data.push(getDay(d,field.end_time,obj.data.code)) 
                                }
                                field.arr2_data = arr2_data
                            }
                            console.log(arr1,arr2,field)
                        }
                            // return

                            like.ajax({
                                url:'{:url("Orderautomatic/add")}?id='+order_sn,
                                data:field,
                                type:"post",
                                success:function(res)
                                {
                                    if(res.code == 1)
                                    {
                                        layui.layer.msg(res.msg, {
                                            offset: '15px'
                                            , icon: 1
                                            , time: 1000
                                        });
                                        layer.close(index); //关闭弹层
                                        table.reload('user_level-lists'); //数据刷新
                                    }
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
            //查看预排订单
            if(obj.event === 'orderinfolist'){
                var order_sn = obj.data.order_sn;
                layer.open({
                    type: 2
                    ,title: '查看预排的订单'
                    ,content: '{:url("Orderautomatic/infolist")}?id='+order_sn
                    ,area: ['90%', '90%']
                    ,yes: function(index, layero){

                    }
                })
            }

            //后台扣除划卡
            if(obj.event === 'delnumber'){
                var id = obj.data.id;
                like.ajax({
                    url: '{:url("order/delnumber")}'
                    , data: {'id': id}
                    , type: 'get'
                    , success: function (res) {
                        if (res.code === 1) {
                            layer.prompt({title: '请输入剩余次数', formType: 2,  value: res.data.code}, function(value, index){
                                if (isNaN(value)) {
                                    layui.layer.msg('请输入一个整数', {
                                        offset: '15px'
                                        , icon: 2
                                        , time: 1000
                                     })
                                    return;
                                }

                                layer.close(index);
                                like.ajax({
                                    url: '{:url("order/delnumber")}'
                                    , data: {'id': id, "code": value }
                                    , type: 'post'
                                    , success: function (res) {
                                        if (res.code === 1) {
                                            layui.layer.msg(res.msg, {
                                                offset: '15px'
                                                , icon: 1
                                                , time: 1000
                                            });
                                          table.reload('order-lists');
                                         layer.close(index); //关闭弹层
                                      
                                        }
                                    }
                                });

                            });
                        }
                    }
                });
            }



            //保洁人员备注信息
            if(obj.event === 'remark'){
                var id = obj.data.id;
                like.ajax({
                    url: '{:url("order/remarks")}'
                    , data: {'id': id}
                    , type: 'get'
                    , success: function (res) {
                        if (res.code === 1) {
                            layer.prompt({title: '备注信息', formType: 2, value: res.data.order_remarks}, function(value, index){
                                layer.close(index);
                                like.ajax({
                                    url: '{:url("order/remarks")}'
                                    , data: {'id': id, "order_remarks": value }
                                    , type: 'post'
                                    , success: function (res) {
                                        if (res.code === 1) {
                                            layui.layer.msg(res.msg, {
                                                offset: '15px'
                                                , icon: 1
                                                , time: 1000
                                            });
                                          table.reload('order-lists');
                                         layer.close(index); //关闭弹层
                                      
                                        }
                                    }
                                });

                            });
                        }
                    }
                });
            }

            //商家备注
            if(obj.event === 'remarks') {
                like.ajax({
                    url: '{:url("order/remarks")}'
                    , data: {'id': id}
                    , type: 'get'
                    , success: function (res) {
                        if (res.code === 1) {
                            layer.prompt({title: '备注信息', formType: 2, value: res.data.order_remarks}, function(value, index){
                                layer.close(index);
                                like.ajax({
                                    url: '{:url("order/remarks")}'
                                    , data: {'id': id, "order_remarks": value }
                                    , type: 'post'
                                    , success: function (res) {
                                        if (res.code === 1) {
                                            layui.layer.msg(res.msg, {
                                                offset: '15px'
                                                , icon: 1
                                                , time: 1000
                                            });
                                            table.reload('order-lists');
                                         layer.close(index); //关闭弹层
                                        table.reload('user_level-lists'); //数据刷新
                                        }
                                    }
                                });

                            });
                        }
                    }
                });

            }


            if(obj.event ==='print'){
                layer.confirm('确定要暂停服务订单吗？', function(index){
                    like.ajax({
                        url:'{:url("Orderautomatic/prent")}',
                        data:{id:id},
                        type:"post",
                        success:function(res)
                        {
                            if(res.code == 1)
                            {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                                layer.close(index); //关闭弹层
                                table.reload('order-lists'); //数据刷新
                            }
                        }
                    });
                    layer.close(index);


                })

            }


            if(obj.event ==='prentsd'){
                layer.confirm('确定要取消暂停服务吗？', function(index){
                    like.ajax({
                        url:'{:url("Orderautomatic/prentsd")}',
                        data:{id:id},
                        type:"post",
                        success:function(res)
                        {
                            if(res.code == 1)
                            {
                                layui.layer.msg(res.msg, {
                                    offset: '15px'
                                    , icon: 1
                                    , time: 1000
                                });
                                layer.close(index); //关闭弹层
                                table.reload('order-lists'); //数据刷新
                            }
                        }
                    });
                    layer.close(index);


                })

            }
        });
    });
</script>