<?php

namespace Modules\Mine\Http\Controllers;

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\TreeAuxiliary;
use Modules\Admin\Http\Controllers\BaseController;
use Modules\Mine\Entities\MineList;
use Modules\Mine\Enum\MineEnum;
use Modules\Mine\Services\MineServices;

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

    //矿区单位列表
    public function mineList(Request $request)
    {
        $this->menusActive[0] = 'adminMine';
        $this->menusActive[1] = 'adminMineList';
        $list = MineServices::getAdminMineList($request->user()->staff_num);
        $tableObj = new TreeAuxiliary('mine/list', $list);
        $tableObj->column('title', '区域名称');
        $tableObj->column('sort', '排序');
        $tableObj->column('ip', 'ip');
        $tableObj->column('port', 'port');
        $tableObj->column('key', '请求key(登录名)');
        $tableObj->column('secret', '请求secret(密码)');
        $tableObj->column('is_hak', '视频服务器类型');
        return $this->tree($tableObj);
    }

    //添加矿区单位
    public function mineAdd(Request $request)
    {
        $mineService = new MineServices();
        $mineService->initMineList();
        $titleList = $mineService->getTierList();

        $formObj = new FromAuxiliary('mine/list/add');
        $formObj->tree_select('parent_id', '父级区域', $titleList);
        $formObj->input('title', '区域名称');
        $formObj->input('sort', '排序', 1);
        $formObj->radio('is_hak', '视频服务器类型', [MineEnum::IS_HAK_YES => '海康',MineEnum::IS_HAK_TD => '天地伟业',MineEnum::IS_HAK_NO => '其它', ], 1);
        $formObj->input('ip', 'ip地址');
        $formObj->input('port', '端口');
        $formObj->input('key', '请求key');
        $formObj->input('secret', '请求secret');
        $formObj->input('sOrgId', '设备ID');
//        $formObj->radio('show', '是否显示', [1 => '是', 0 => '否'], 1);
        $formObj->title = '添加区域';
        return $this->from($formObj);
    }

    //添加矿区单位操作
    public function mineAddPost(Request $request)
    {
        if (!$request->input('title')) {
            return $this->error(1, '请输入矿区单位名称');
        }
        $mineService = new MineServices();
        $mineService->initMineList();
        $result = $mineService->add($request->all());
        $mine_id = $result->id;

        if ($request->input('is_hak') == MineEnum::IS_HAK_YES) {//海康
            if (!$request->input('ip') || !$request->input('port')
                || !$request->input('key') || !$request->input('secret')
            ) {
                return $this->error(1, '请输入连接视频服务器必传参数');
            }

            MineServices::insertMineListExt($mine_id, $request->all());
        }

        if ($request->input('is_hak') == MineEnum::IS_HAK_TD) {//天地伟业
//            return $this->error(1, $request->input('parent_id'));
            if($request->input('parent_id')==0){
                if (!$request->input('ip') || !$request->input('port')
                    || !$request->input('key') || !$request->input('secret') || !$request->input('sOrgId')
                ) {
                    return $this->error(1, '请输入连接视频服务器必传参数');
                }
            }
            MineServices::insertMineListExt($mine_id, $request->all());
        }

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

    //编辑矿区单位
    public function mineEdit(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error(1, '缺少必要参数');
        }

        $mineService = new MineServices();
        $mineService->initMineList();
        $titleList = $mineService->getTierList();
        $titleOne  = MineServices::getAdminMineOne($request->input('id'));

        $formObj = new FromAuxiliary('mine/list/edit', $titleOne);
        $formObj->tree_select('parent_id', '父级区域', $titleList, $titleOne->parent_id);
        $formObj->input('title', '区域名称');
        $formObj->input('sort', '排序', 10);
        $formObj->radio('is_hak', '视频服务器类型', [MineEnum::IS_HAK_YES => '海康',MineEnum::IS_HAK_TD => '天地伟业',MineEnum::IS_HAK_NO => '其它', ], 0);
        $formObj->input('ip', 'id地址');
        $formObj->input('port', '端口');
        $formObj->input('key', '请求key');
        $formObj->input('secret', '请求secret');
        $formObj->input('sOrgId', '设备ID');
        $formObj->title = '修改区域';
        return $this->from($formObj);
    }

    //编辑矿区单位操作
    public function mineEditPost(Request $request)
    {
        if (!$request->input('id')) {
            return $this->error(1, '缺少必要参数');
        }
        $mineService = new MineServices();
        $mineService->initMineList();

        if ($request->input('id') == $request->input('parent_id')) {
            return $this->error(1, '不能选择自己为父级标题');
        }
        $mineService->edit($request->all());

        if ($request->input('is_hak') == MineEnum::IS_HAK_NO) {//其他
            MineServices::editMineListExt($request->input('id'), $request->all());
        }

        if ($request->input('is_hak') == MineEnum::IS_HAK_YES) {//海康
            if (!$request->input('ip') || !$request->input('port')
                || !$request->input('key') || !$request->input('secret')
            ) {
                return $this->error(1, '请输入连接视频服务器必传参数');
            }

            MineServices::editMineListExt($request->input('id'), $request->all());
        }

        if ($request->input('is_hak') == MineEnum::IS_HAK_TD) {//天地伟业
//            return $this->error(1, $request->input('parent_id'));
            if($request->input('parent_id')==0){
                if (!$request->input('ip') || !$request->input('port')
                    || !$request->input('key') || !$request->input('secret') || !$request->input('sOrgId')
                ) {
                    return $this->error(1, '请输入连接视频服务器必传参数');
                }
            }
            MineServices::editMineListExt($request->input('id'), $request->all());
        }

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

    //删除矿区单位操作
    public function mineDelPost(Request $request)
    {
        if (!$request->has('id')) {
            return $this->error(1, '缺少必要参数');
        }

        $one_title = MineList::find($request->input('id'));

        if (!$one_title) {
            return $this->error(1, '需删除的数据不存在');
        }

        $mineService = new MineServices();
        $mineService->initMineList();
        $mineService->del($request->input('id'));
        return $this->success();
    }
}
