<?php

namespace app\admin\controller\video;

use app\admin\controller\Curd;
use support\spark\model\video\Videorules;
use support\spark\model\risk\Management;

use support\Request;
use support\spark\model\system\AdminRole;
use support\spark\model\system\AdminUser;
use support\View;
use support\Db;

class VideorulesController extends Curd
{

    # 视频规则列表
    public function index(Request $request)
    {
        if (request()->isAjax()) {
            $param = $request->all();
            $model = new Videorules();
            $limit = $param['limit'];
            $where = [];
            $cycle_type  = $param['cycle_type'];
            $engineering_name         = $param['engineering_name'];
            if (!empty($cycle_type)) {
                $where[] = ['cycle_type', '=', $cycle_type];
            }
            if (!empty($engineering_name)) {
                $where[] = ['engineering_name', 'like', '%' . $engineering_name . '%'];
            }
            $list = $model->getPaginateList($where, ['*'], ['id' => 'asc'], [], $limit);
            $roles_type_arr = ['','集团','施工公司','建设公司','项目部','其他'];
            if($list['total']>0) {
                foreach ($list['data'] as $key => $item) {
                    $list['data'][$key]['roles_type'] = $roles_type_arr[$item['admin_role_category']];
                    $list['data'][$key]['roles_name'] =(new AdminRole)
                        ->where('id', $item['rules_id'])
                        ->first('name')['name'];
                }
            }
            # 工程风险点列表列表
            $engineeringModel = new Management();
            $where1[] = ['status', '=', 0];
            $data['engineeringlist'] = $engineeringModel->getAllList($where1);
//            $data['belist'] = (new BasicdataEngineeringController())->getListout();
            $data['belist'] = [];
            $data['list'] = $list;
            return sparkSuccess(dataReturn(0, '查询成功',$data));
        }
        return view('video/videorules/index');
    }

    /**
     * 新增视频队则
     * @param Request $request
     * @return \support\Response
     */
    public function store(Request $request){
        if ($request->isAjaxPost()) {
            $param = $request->post();

            $model = new Videorules();
            if (!empty($param['no_admin_ids'])) {
                $param['no_admin_ids'] = implode(',',$param['no_admin_ids']);
            } else {
                unset($param['no_admin_ids']);
            }
            if (!empty($param['week_days'])) {
                $param['week_days'] = implode(',',$param['week_days']);
            } else {
                unset($param['week_days']);
            }

            $res = $model->insertOne($param);
            return sparkSuccess(dataReturn(0, '添加成功'));
        }

    }
    /**
     * 编辑工程
     * @param Request $request
     * @return \support\Response
     */
    public function update(Request $request){
        if ($request->isAjaxPost()) {
            $param = $request->post();
            $model = new Videorules();
            if (!empty($param['no_admin_ids'])) {
                $param['no_admin_ids'] = implode(',',$param['no_admin_ids']);
            } else {
                $param['no_admin_ids'] = '';
            }

            $res = $model->updateById($param,$param['id']);
            return sparkSuccess(dataReturn(0, '编辑成功'));
        }
        return sparkSuccess(dataReturn(0, 'error'));
    }

    /**
     * 删除工程信息
     * @param Request $request
     * @return \support\Response
     */
    public function destroy(Request $request){
        $id = $request->get('id');
        $model = new Videorules();
        $model->delById($id);
        return sparKSuccess(dataReturn(0, '删除成功'));
    }

    /**
     * 查询工程详情
     * @param Request $request
     * @return \support\Response
     */
    public function show(Request $request){
        $id = $request->get('id');
        $model = new Videorules();
        $row = $model->getInfoById($id);
        if ($row['no_admin_ids']) {
            $row['no_admin_ids'] = explode(',',$row['no_admin_ids']);
        }
        $row['admin_role_category'] = strval($row['admin_role_category']);
        return sparKSuccess(dataReturn(0, 'success', $row));
    }
    # 查询角色
    public function searchAdminUserRoles()
    {
        $param = request()->all();
        $where = [];
        if (isset($param['admin_role_category']) && !empty($param['admin_role_category'])) {
            $where[] = ['category', '=', $param['admin_role_category']];
        }
        $adminRoleModel = new AdminRole();
        $where[] = ['status', '=', 1];
        $where[] = ['id', '>', 1];
        return sparkSuccess(dataReturn(0, 'ok', $adminRoleModel->getAllList($where)));

    }
    # 获取用户账号
    public function searchAdminUser()
    {
        $param = request()->all();
        $where = [];
        if (isset($param['role_id']) && !empty($param['role_id'])) {
            $where[] = ['role_id', '=', $param['role_id']];
            $list = (new AdminUser)
                ->select('nickname', 'name','id')
                ->where($where)
                ->get();
        } else {
            $where[] = ['nickname', 'like', '%' . $param['nickname'] . '%'];
            $list = (new AdminUser)
                ->select('nickname', 'name','id')
                ->where($where)
                ->limit(10)
                ->get();
        }

        return sparkSuccess(dataReturn(0, 'success', $list));
    }

}