<?php

namespace Modules\Mine\Http\Controllers;

use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
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\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';

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

        $tableObj = new TreeAuxiliary('mine/list', $list);
        $tableObj->column('title', '矿区单位名称');
        $tableObj->column('sort', '排序');
        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('show', '是否显示', [1 => '是', 0 => '否'], 1);
        $formObj->title = '添加矿区单位';
        return $this->from($formObj);
    }

    //添加矿区单位操作
    public function mineAddPost(Request $request)
    {
        if (!$request['title']) {
            return $this->error(1, '请输入矿区单位名称');
        }

        $mineService = new MineServices();
        $mineService->initMineList();
        $mineService->add($request->all());
        return $this->redirect('admin/mine/list');
    }

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

        $mineService = new MineServices();
        $mineService->initMineList();
        $titleList = $mineService->getTierList();
        $titleOne  = $mineService->getOneInfo($request->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->title = '修改矿区单位';
        return $this->from($formObj);
    }

    //编辑矿区单位操作
    public function mineEditPost(Request $request)
    {
        if (!$request->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());
        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();
    }
}
