<?php

namespace Modules\Camera\Http\Controllers;

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Log;
use Modules\Admin\Auxiliary\View\FromAuxiliary;
use Modules\Admin\Auxiliary\View\TableAuxiliary;
use Modules\Admin\Auxiliary\View\TreeAuxiliary;
use Modules\Admin\Http\Controllers\BaseController;
use Modules\Camera\Entities\CameraFieldList;
use Modules\Camera\Entities\CameraList;
use Modules\Camera\Jobs\CameraListExcel;
use Modules\Camera\Services\CameraServices;
use Modules\Mine\Entities\MineList;
use Modules\Mine\Services\MineServices;

class CameraController extends BaseController
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        return view('camera::index');
    }

    //摄像头列表
    public function cameraList(Request $request)
    {
        $this->menusActive[0] = 'adminCamera';
        $this->menusActive[1] = 'adminCameraList';

        $camera_list = CameraList::join(
            'mine_list', 'camera_list.mine_id', '=', 'mine_list.id'
        )->orderBy('camera_list.sort', 'desc')->select(
            'camera_list.id',
            'camera_list.camera_url',
            'camera_list.camera_name',
            'camera_list.revert_id',
            'camera_list.sort',
            'mine_list.title',
            'mine_list.degree'
        )->paginate(50);

        foreach ($camera_list as $key => $val) {
            $camera_list[$key]->parent_title = MineList::where('id', explode('|', $val->degree)[0])->value('title');
        }

        $tableObj             = new TableAuxiliary('camera/list', $camera_list);
        $tableObj->topActions = ['add', 'import', 'importTemplate'];
        $tableObj->column('parent_title', '矿区名称');
        $tableObj->column('title', '区域名称');
        $tableObj->column('camera_name', '摄像头名称');
        $tableObj->column('revert_id', '流媒体id');
        $tableObj->column('camera_url', '链接');
        $tableObj->column('sort', '排序');
        return $this->tableList($tableObj);
    }

    //查看摄像头列表详情
    public function cameraListView(Request $request)
    {
        if (!$request->has('id')) {
            return $this->redirect('admin/camera/list');
        }

        $camera_list = CameraList::join(
            'mine_list', 'camera_list.mine_id', '=', 'mine_list.id'
        )->where('camera_list.id', $request->input('id'))->select(
            'camera_list.id',
            'camera_list.ip',
            'camera_list.port',
            'camera_list.user_name',
            'camera_list.password',
            'camera_list.com_number',
            'camera_list.camera_url',
            'camera_list.camera_name',
            'camera_list.revert_id',
            'camera_list.sort',
            'mine_list.title',
            'mine_list.degree'
        )->first();

        $camera_list->parent_title = MineList::where('id', explode('|', $camera_list->degree)[0])->value('title');

        $tableObj = new TableAuxiliary('', $camera_list);
        $tableObj->column('parent_title', '矿区名称');
        $tableObj->column('title', '区域名称');
        $tableObj->column('camera_name', '摄像头名称');
        $tableObj->column('revert_id', '流媒体id');
        $tableObj->column('user_name', '用户名');
        $tableObj->column('password', '密码');
        $tableObj->column('ip', 'ip地址');
        $tableObj->column('port', '端口');
        $tableObj->column('com_number', '通道号');
        $tableObj->column('camera_url', '链接');
        $tableObj->column('sort', '排序');
        $tableObj->title = '摄像头链接详情';
        return $this->tableView($tableObj);
    }

    //添加摄像头列表
    public function cameraListAdd(Request $request)
    {
        $mineService = new MineServices();
        $mineService->initMineList();
        $titleList = $mineService->getTierList();

        $formObj = new FromAuxiliary('camera/list/add');
        $formObj->tree_select('mine_id', '矿区单位', $titleList);
        $formObj->input('camera_name', '摄像头名称');
        $formObj->input('user_name', '用户名');
        $formObj->input('password', '密码');
        $formObj->input('ip', 'ip地址');
        $formObj->input('port', '端口');
        $formObj->input('com_number', '通道号');
        $formObj->input('sort', '排序');
        $formObj->title = '添加请求链接';
        return $this->from($formObj);
    }

    //添加摄像头列表操作
    public function cameraListAddPost(Request $request)
    {
        if (!$request->input('mine_id')) {
            return $this->error(1, '请选择矿区单位');
        }

        if (!$request->input('camera_name')) {
            return $this->error(1, '请输入摄像头名称');
        }

        if (
            !$request->input('ip') || !$request->input('port') || !$request->input('user_name')
            || !$request->input('password') ||!$request->input('com_number')
        ) {
            return $this->error(1, '请输入必填字段');
        }

        $result = CameraServices::addCameraList($request->all());

        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }

        return $this->redirect('admin/camera/list');
    }

    //编辑摄像头列表
    public function cameraListEdit(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            return $this->redirect('admin/camera/list');
        }

        $mineService = new MineServices();
        $mineService->initMineList();
        $titleList = $mineService->getTierList();

        $camera_list = CameraList::find($id);

        $formObj = new FromAuxiliary('camera/list/edit', $camera_list);
        $formObj->tree_select('mine_id', '矿区单位', $titleList);
        $formObj->input('camera_name', '摄像头名称');
        $formObj->input('user_name', '用户名');
        $formObj->input('password', '密码');
        $formObj->input('ip', 'ip地址');
        $formObj->input('port', '端口');
        $formObj->input('com_number', '通道号');
        $formObj->input('sort', '排序');
        $formObj->title = '编辑请求链接';
        return $this->from($formObj);
    }

    //编辑摄像头操作
    public function cameraListEditPost(Request $request)
    {
        if (!$request->input('mine_id')) {
            return $this->error(1, '请选择矿区单位');
        }

        if (!$request->input('camera_name')) {
            return $this->error(1, '请输入摄像头名称');
        }

        if (
            !$request->input('ip') || !$request->input('port') || !$request->input('user_name')
            || !$request->input('password') ||!$request->input('com_number')
        ) {
            return $this->error(1, '请输入必填字段');
        }

        $result = CameraServices::editCameraList($request->all());

        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }

        return $this->redirect('admin/camera/list');
    }

    //删除摄像头列表记录
    public function cameraListDelPost(Request $request)
    {
        if (!$request->has('id')) {
            return $this->error(1, '缺少必要参数');
        }

        $result = CameraServices::delCameraList($request->all());
        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }
        return $this->success();
    }

    //导入摄像头列表
    public function cameraListImport(Request $request)
    {
        if (!$request->hasFile('excel')) {
            return $this->error(9001, '缺少上传文件');
        }
        ini_set('memory_limit', '500M');
        set_time_limit(300);
        $reader = ReaderEntityFactory::createReaderFromFile($request->file('excel')->getClientOriginalName());
        $reader->open($request->file('excel')->getRealPath());
        $sheet = $reader->getSheetIterator();  // 获取sheet对象
        $sheet->rewind();  // 指定当前sheet序数
        $sheet = $sheet->current();  // 获取指定sheet
        $arr   = [];
        foreach ($sheet->getRowIterator() as $index => $row) {  // 读取每行记录
            $arr[] = $row->toArray();
        }
        $reader->close();
        $result = CameraListExcel::dispatch($arr);
        if (!$result) {
            return $this->error(1, '所传矿区信息不存在');
        }
        return $this->success();
    }

    //下载摄像头列表导入模板
    public function cameraListTemplate()
    {
        return $this->redirect('/template/摄像头列表导入模板_v1.0.xlsx');
    }

    //字段列表
    public function fieldList(Request $request)
    {
        $this->menusActive[0] = 'adminCamera';
        $this->menusActive[1] = 'adminCameraFieldlist';

        $field_list = CameraFieldList::get();

        $tableObj             = new TableAuxiliary('camera/field_list', $field_list);
        $tableObj->actionBtns = ['edit', 'del'];
        $tableObj->column('field_name', '字段名称');
        return $this->tableList($tableObj);
    }

    //添加字段
    public function fieldListAdd(Request $request)
    {
        $formObj = new FromAuxiliary('camera/field_list/add');
        $formObj->input('field_name', '字段名称');
        $formObj->title = '添加字段';
        return $this->from($formObj);
    }

    //添加字段操作
    public function fieldListAddPost(Request $request)
    {
        if (!$request->input('field_name')) {
            return $this->error(1, '请输入字段名称');
        }

        $result = CameraServices::addCameraFieldList($request->all());

        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }

        return $this->redirect('admin/camera/field_list');
    }

    //编辑字段
    public function fieldListEdit(Request $request)
    {
        $field_list_id = $request->input('id');

        if (empty($field_list_id)) {
            return $this->redirect('admin/camera/field_list');
        }

        $field_list = CameraFieldList::find($field_list_id);

        $fromObj = new FromAuxiliary('camera/field_list/edit', $field_list);
        $fromObj->input("field_name", "字段名称", $field_list->field_name);
        $fromObj->title = '修改字段';
        return $this->from($fromObj);
    }

    //编辑字段操作
    public function fieldListEditPost(Request $request)
    {
        if (!$request->input('field_name')) {
            return $this->error(1, '请输入字段名称');
        }

        $result = CameraServices::editCameraFieldList($request->all());

        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }

        return $this->redirect('admin/camera/field_list');
    }

    //删除字段
    public function fieldListDelPost(Request $request)
    {
        if (!$request->has('id')) {
            return $this->error(1, '缺少必要参数');
        }

        $result = CameraServices::delCameraFieldList($request->all());
        if (!$result['status']) {
            return $this->error(1, $result['msg']);
        }
        return $this->success();
    }
}
