@can('system.inquiries')    
        <script>
            layui.use(['layer','table','form'],function () {
                var $ = layui.jquery;
                var layer = layui.layer;
                var form = layui.form;
                var table = layui.table;
                //用户表格初始化
                var dataTable = table.render({
                    elem: '#dataTable'
                    // ,height: 500
                    ,url: "{{ route('admin.data') }}" //数据接口
                    ,where:{model:"inquiries"}
                    ,page: true //开启分页
                    ,cols: [[ //表头
                        {field: 'id', title: 'Id',width: 80}
                        ,{field: 'created_at', title: '询盘时间',width: 200,templet:"#timeTpl"}
                        ,{field: 'user', title: '客户信息',templet:"#userTpl"}
                        ,{field: 'updated_at', title: '商品信息',templet:"#goodsTpl"}
                        ,{field: 'package', title: '包装信息',templet:"#packageTpl"}
                        ,{field: 'cuohe',width:100, title: '撮合员',templet:"#cuoheTpl"}
                        ,{field: 'status',width:100, title: '询盘状态',templet:"#statusTpl"}
                        ,{field: 'handle',width:100, title: '处理状态',templet:"#handleTpl"}
                        ,{fixed: 'right', width: 220, align:'center', toolbar: '#options'}
                    ]]
                });
                //搜索
                form.on('submit(btn-search)', function(data){
                    table.reload('dataTable', {
                        where: {
                            user_info: data.field.user_info
                            ,start_time:data.field.start_time
                            ,end_time:data.field.end_time
                            ,cas: data.field.cas
                            ,status: data.field.status
                            ,origin: data.field.origin
                            ,intermediary_id: data.field.intermediary_id
                        }
                        ,page: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 === 'del'){
                        layer.confirm('确认关闭询盘吗?', function(index){
                            $.post("{{ route('admin.inquiries.close') }}",{_method:'post',ids:[data.id]},function (result) {
                                if (result.code==0){
                                    obj.del(); //删除对应行(tr)的DOM结构
                                    layer.msg(result.msg,{icon:6})
                                }else{
                                    layer.msg(result.msg,{icon:5})
                                }
                                layer.close(index);
                            });
                        });
                    } else if(layEvent === 'edit'){
                        location.href = '/admin/inquiries/'+data.id+'/edit';
                    } else if (layEvent === 'permission'){
                        location.href = '/admin/role/'+data.id+'/permission';
                    }
                });
                //按钮批量删除
                $("#listDelete").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)
                        })
                    }
                    if (ids.length>0){
                        layer.confirm('确认删除吗?', function(index){
                            $.post("{{ route('admin.role.destroy') }}",{_method:'delete',ids:ids},function (result) {
                                if (result.code==0){
                                    dataTable.reload()
                                }
                                layer.close(index);
                                layer.msg(result.msg,{icon:6})
                            });
                        })
                    }else {
                        layer.msg('请选择删除项',{icon:5})
                    }
                })
            });
        </script>
        @endcan

    public function data(Request $request)    
    {
        $role_id = auth('web')->user()->role->role_id; //角色id
        $user_id = auth('web')->user()->id; //用户id,撮合员id
        $model = $request->get('model');
        switch (strtolower($model)) {
                    case 'user':
                        $query = (new User())->with(['role.roleOne']);
                        break;
                case 'inquiries':
                    $query = Order::with(['prod','user.user','item'])
                    ->whereHas('user',function($q) use ($role_id,$user_id,$intermediary_id){
                                  if($role_id == 3){
                                     $q->where('user_id','=',$user_id);
                                  }
                     })->orderBy('id','desc');
                        break;
                 default:
                        $query = new User();
                        break;
        }
        
        $res = $query->paginate($request->get('limit', 30))->toArray();
                $data = [
                    'code' => 0,
                    'msg' => '正在请求中...',
                    'count' => $res['total'],
                    'data' => $res['data']
                ];
         return response()->json($data);
    }

    class Inquiry extends Model    
    {
        public $guarded = [];
        protected $casts = ['package'=>'array'];
        protected $fillable =['prod_id','cas','prod_name','package','invoice_type','province','city','district','note','anonymous','allow_contact','user_id','user_name','status','handle','origin','company_name','company_id'];
        public function invoices(){
            return $this->hasOne(InvoiceCategory::class, 'id', 'invoice_type');
        }
        public function user(){
            return $this->hasOne(Member::class, 'id','user_id');
        }
        public function prod(){
            return $this->hasOne(Prod::class, 'cas','cas');
        }
        public function company(){
            return $this->hasOne(Company::class, 'id','company_id');
        }
    }
    class Member extends Authenticatable    
    {
        protected $table = 'members';
        protected $fillable = ['id','identity','phone','email','fax','tel','password','name','qq','company_name','company_id','remember_token','idcard_no','status','is_deposit','user_id'];
        protected $hidden = ['password'];
        public function company(){
            return $this->hasOne(Company::class, 'id','company_id');
        }
        public function user(){
            return $this->hasOne(User::class, 'id','user_id');
        }
    }