<?php

namespace App\Community\Controllers;

use App\Admin\Actions\Community\Docter\Chat;
use App\Admin\Actions\Community\Notice\sendNotice;
use App\Admin\Actions\Community\Order\Detail;
use App\Models\Docter;
use App\Models\Order;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use App\Models\User;

class DocterChatController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '咨询订单';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());

        $user = Admin::user();
        $org_id = $user->org_id;
        $docter_id = $user->docter_id;
        $is_admin = Admin::user()->inRoles(['administrator','devloper']);
        $grid->batchActions(function ($batch) {
            $batch->add(new sendNotice());
        });
        if(!$is_admin){
            $where['organization_id']=$org_id;
            if($docter_id){
                $where['docter_id'] = $docter_id;
            }
            $grid->model()->where($where);
//            $grid->model()->whereHas('orders',function ($query) use ($org_id,$docter_id) {
//                $query->where($where);
//            });
        }

        $grid->column('order_sn', __('订单号'));
        $grid->column('docter.name', __('医生'));
        $grid->column('orderUser.nickname', __('咨询用户'));
        $grid->column('orderPatient.name', __('咨询患者'));
        $grid->column('orderPatient.birthday', __('患者年龄'))->display(function ($w){
            return birthday_to_age($w);
        });
        $grid->actions(function ($actions) use($docter_id) {
            if($docter_id){
                $actions->add( new Chat());
            }
            $actions->add(new Detail());
           $actions->disableDelete();
           $actions->disableView();
           $actions->disableEdit();
        });
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->equal('order_status','订单状态')->select(Order::$_order_status);
        });
        $grid->column('order_status', __('订单状态'))->using(Order::$_order_status);
        $grid->column('created_at', __('下单时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        return '';
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
       return '';
    }

    function chats(Content $content){
        $content->title('咨询订单 ');
        $content->description('医生用户可以接受预约');
        $user = Admin::user();
        $org_id = $user->org_id;
        $docter_id = $user->docter_id;
        Admin::js('jquery-3.5.1.min.js');

        $url = '/cdms/chat_view?docter_id='.$docter_id;
//        $content->body('<iframe src="/cdms/chat_view?docter_id='.$docter_id.'" style="width: 100%;height:800px;border: none"></iframe>');
        $content->body('<iframe src="/cdms/chat_view?docter_id='.$docter_id.'" style="width: 100%;height:800px;border: none"></iframe>');
        return $content;
    }

    public function chat_view()
    {
        return view('cdms.chat');
    }


}
