@extends('admin.layouts.base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto layui-form">
            <form class="layui-form-item" id="search-form">
                <input type="hidden" name="from" value="cloud">
                <div class="layui-inline">
                    <label class="layui-form-label">时间区间</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" placeholder="开始时间" name="start_time" id="start_time" autocomplete="off">
                    </div>
                    <div class="layui-form-mid">至</div>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" placeholder="结束时间" name="end_time" id="end_time" autocomplete="off">
                    </div>
                </div>
                {{--@include('admin.component.department-channel')--}}
                {{--<div class="layui-inline" >
                    <label class="layui-form-label">产品类型</label>
                    <div class="layui-input-inline">
                        <select name="type"  id="type">
                            <option value="">所有产品类型</option>
                            <option value="product">资源产品</option>
                            <option value="credit">资源</option>

                        </select>
                    </div>
                </div>--}}
                <div class="layui-inline">
                    <label class="layui-form-label">申请平台</label>
                    <div class="layui-input-inline">
                        <select name="platform"  id="platform">
                            <option value="">所有申请平台</option>
                            <option value="android">android</option>
                            <option value="ios">ios</option>
                            <option value="pc">pc</option>
                            <option value="wap">wap</option>

                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">订单状态</label>
                    <div class="layui-input-inline">
                        <select name="status"  id="status">
                            <option value="">所有订单</option>
                            <option value="1">待支付</option>
                            <option value="2">已支付/待接单</option>
                            <option value="3">取消/过期</option>
                            <option value="4">执行中</option>
                            <option value="5">已完成</option>
                            <option value="6">退单退款</option>

                        </select>
                    </div>
                </div>
                {{-- <div class="layui-inline">
                     <label class="layui-form-label">负责人</label>
                     <div class="layui-input-inline">
                         <input type="text" name="manager" id="manager"  placeholder="请输入渠道负责人" class="layui-input">
                     </div>
                 </div>--}}
                <div class="layui-inline">
                    <label class="layui-form-label">用户电话</label>
                    <div class="layui-input-inline">
                        <input type="text" name="phone" id="phone"  placeholder="请输入用户电话" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit="" lay-filter="searchBtn">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>


            </form>
            <div>
                <div class="layui-btn-group">
                    @can('admin.member.apply.toExcel')
                        <button class="layui-btn layui-btn-sm layui-btn-container" id="toExcel">导 出</button>
                    @endcan
                    <div style="margin-top: 20px">
                        <div class="layui-btn-group">
                            <button class="layui-btn layui-btn-sm  layui-btn-normal type_tag" data="article">软文订单</button>
                        </div>
                        <div class="layui-btn-group">
                            <button class="layui-btn layui-btn-sm  layui-btn-normal type_tag" data="zimeiti">自媒体订单</button>
                        </div>
                        <div class="layui-btn-group">
                            <button class="layui-btn layui-btn-sm  layui-btn-normal type_tag" data="weixin">公众号订单</button>
                        </div>
                        <div class="layui-btn-group">
                            <button class="layui-btn layui-btn-sm  layui-btn-normal type_tag" data="weibo" >微博订单</button>
                        </div>
                        <div class="layui-btn-group">
                            <button class="layui-btn layui-btn-sm  layui-btn-normal type_tag" data="video">短视频订单</button>
                        </div>
                        <div class="layui-btn-group">
                            <button class="layui-btn layui-btn-sm  layui-btn-normal type_tag" data="redbook">小红书订单</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="show">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="show">查看</a>
                </div>
            </script>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="edit">处理</a>
                    <a class="layui-btn layui-btn-sm" lay-event="cancel">取消</a>
                </div>
            </script>
            <script type="text/html" id="thumb">
                <a href="@{{d.thumb}}" target="_blank" title="点击查看"><img src="@{{d.thumb}}" alt="" width="28" height="28"></a>
            </script>

        </div>
    </div>
@endsection

@section('script')
    @can('admin.member.apply')
        @include('admin.component.department-channel-js')
        <script>
            layui.use(['layer','table','form','laydate','laypage'],function () {
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                var laydate = layui.laydate;
                var laypage = layui.laypage;
                //用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable'
                    // ,height: 500
                    ,url: "{{ route('admin.apply.data') }}?from=cloud" //数据接口
                    ,page: true //开启分页
                    ,cols: [[ //表头
                        {checkbox: true,fixed: true}
                        // ,{field: 'id', title: 'ID', sort: true,width:80}
                        ,{field: 'order_no', title: '订单号'}
                        ,{field: 'status', title: '订单状态',templet: function(d){
                            if(d.status == 1){
                                return '未支付'
                            }else if(d.status == 2){
                                return '已支付/待执行'
                            }else if(d.status == 4){
                                return '执行中'
                            }else if(d.status == 5){
                                return '已完成'
                            }else if(d.status == 6){
                                return '已退单'
                            }else if(d.status == 7){
                                return '媒体主完成'
                            }else{
                                return '取消/过期'
                            }
                        }}
                        ,{field: 'phone', title: '用户电话',templet: function(d){
                            if(d.member != null){
                                return d.member.phone
                            }else{
                                return ''
                            }
                        }}
                        ,{field: 'model_id', title: '资源名称',templet: function(d){
                            if(d.product){
                                return d.product.name
                            }else{
                                return ''
                            }
                        }}
                        ,{field: 'productType', title: '资源类型',templet: function(d){

                            if(d.productType){
                                switch(d.productType){
                                    case 'article':
                                        return '软文';
                                        break;
                                    case 'weixin':
                                        return '公众号';
                                        break;
                                    case 'video':
                                        return '短视频';
                                        break;
                                    case 'weibo':
                                        return '微博';
                                        break;
                                    case 'redbook':
                                        return '小红书';
                                        break;
                                    case 'zimeiti':
                                        return '自媒体';
                                        break;
                                    default :
                                        return '';
                                        break;
                                }
                            }else{
                                return ''
                            }
                        }}
                        ,{field: 'product', title: '资源归属',templet: function(d){
                            if(d.product){
                                if(d.product.created_from == 'media'){
                                    return '媒体主资源'
                                }
                                if(d.product.created_from == 'cloud'){
                                    return '云资源'
                                }
                                return '平台资源'
                            }else{
                                return ''
                            }
                        }}
                        ,{field: 'money', title: '支付金额'}
                        ,{fixed: 'right', title:'云详情',width: 100, align:'center', toolbar: '#show'}
                        ,{field: 'created_at', title: '下单时间'}
                        ,{field: '', title: '执行时间',templet: function(d){
                            if(d.start_time != null){
                                return d.start_time
                            }else{
                                return ''
                            }

                        }}
                        ,{field: '', title: '完成时间',templet: function(d){
                            if(d.complete_time){
                                return d.complete_time
                            }else{
                                return ''
                            }
                        }}
                        ,{field: 'result', title: '完成地址',templet: function(d){
                            if(d.result_url){
                                return d.result_url;
                            }else{
                                return '';
                            }
                        }}
                        ,{fixed: 'right', title:'操作',width: 100, align:'center', toolbar: '#options'}
                    ]]
                });


                //导出
                $("#toExcel").click(function () {
                    var ids = []
                    var hasCheck = table.checkStatus('dataTable')
                    var hasCheckData = hasCheck.data
                    if (hasCheckData.length>0){
                        $.each(hasCheckData,function (index,element) {
                            ids.push(element.id)
                        })
                    }
                    var type = $("#type").val()
                    var platform = $("#platform").val();
                    var start_time = $("#start_time").val();
                    var end_time = $("#end_time").val();
                    var department = $("#department").val();
                    var channel_code = $("#channel_code").val();
                    var manager = $("#manager").val();
                    var phone = $("#phone").val();
                    var u = '{{ route('admin.apply.toExcel') }}';
                    u = u+'?type='+type+'&start_time='+start_time+'&end_time='+end_time+'&platform='+platform+'&ids='+ids+'&department='+department+"&channel_code="+channel_code+'&manager'+manager+'&phone='+phone+'&from=cloud';
                    window.location.href=u;
                })
                //监听搜索
                form.on('submit(searchBtn)', function(data){
                    var field = data.field;
                    //执行重载
                    table.reload('dataTable', {
                        where: field
                        ,page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    return false;
                });
                //监听工具条
                table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                    var data = obj.data //获得当前行数据
                        ,layEvent = obj.event; //获得 lay-event 对应的值
                    if(layEvent === 'edit'){
                        location.href = '/admin/apply/'+data.id+'/edit';
                    }else if(layEvent === 'show'){
                        $.post('{{route('admin.apply.cloud.show')}}',{id:data.id},function(res){
                            if(res.code == 200){
                                var show_html = '<div style="background-color:#5FB878;color: white;padding:10px;">';
                                $.each(res.data,function (index,value){
                                    show_html+="<span>"+(index+1)+"、</span><span>"+value.created_at+"---"+value.mark+"</span><br>"
                                });
                                show_html +="</div>"
                                layer.open({
                                    type: 1,
                                    shade: false,
                                    title: false, //不显示标题
                                    content: show_html,
                                    cancel: function(){
                                    }
                                });
                            }else{
                                layer.msg(res.info,{icon:2});
                            }
                        })
                    }else if(layEvent === 'cancel'){
                        if(data.status == 3) {
                            layer.msg('订单已取消', {icon: 2});
                        }else if(data.status == 6) {
                            layer.msg('订单已经退单', {icon: 2});
                        }else {
                            layer.open({
                                type:0,
                                title:'编辑',
                                anim: 0 ,
                                scrollbar: false,
                                area:["550px","450px"] ,
                                content: '<form class="layui-form" id="addEmployeeForm">' +
                                ' <div class="layui-row layui-col-space10 layui-form-item">' +
                                '<div class="layui-col-lg9">' +
                                '<label class="layui-form-label">取消原因：</label>' +
                                '<div class="layui-input-block">' +
                                '<textarea  id="edit_content" onkeyup="onfo($(this))" class="layui-input" style="height: 150px;resize:none"></textarea>' +
                                '<span style=" position: absolute; color: #ccc;top: 125px;left: 200px;">最多255字</span>' +
                                '</div>' +
                                '</div>' +
                                '</div>' +
                                '</form>',
                                btn: ['确定'],
                                yes: function(index, layero){
                                    var edit_content = $("#edit_content").val();
                                    if(!edit_content){
                                        layer.msg("请输入评论内容", {icon: 2, time: 2000});
                                        return false;
                                    }else if(edit_content.length > 255){
                                        layer.msg("请输入评论内容,255个字以内", {icon: 2, time: 2000});
                                        return false;
                                    }
                                    $.post('{{route('admin.apply.cancel')}}',{id:data.id,reason:edit_content},function(res){
                                        if(res.code == 0){
                                            layer.msg(res.info,{icon:1});
                                            dataTable.reload();
                                        }else{
                                            layer.msg(res.info,{icon:2});
                                        }
                                    })
                                }
                            });
                        }
                    }else{
                        layer.msg('该订单不能操作', {icon: 2});
                    }
                });
                laydate.render({
                    elem: '#start_time' //指定元素
                    ,type: 'datetime'
                });
                laydate.render({
                    elem: '#end_time' //指定元素
                    ,type: 'datetime'
                });

                $(".type_tag").click(function () {
                    var tag = $(this).attr("data");
                    if(!tag)
                    {
                        layer.msg('请选择类型',{icon:2});
                        return false;
                    }else{
                        location.href ='/admin/apply/cloud/type?type='+tag;
                    }
                })
            });

        </script>
    @endcan
@endsection

