<?php
/**
 * Created by PhpStorm.
 * User: qiuzijian
 * Date: 2021-05-28
 * Time: 15:04
 */

namespace Modules\Mine\Http\Controllers;

use Illuminate\Http\Request;
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\Mine\Entities\WorkingSurface;
use Modules\Mine\Enum\SurfaceEnum;
use Modules\Mine\Services\MineServices;
use Modules\Mine\Services\SurfaceServices;

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

    //工作面列表
    public function surfaceList(Request $request)
    {
        $this->menusActive[0] = 'adminSurface';
        $this->menusActive[1] = 'adminSurfaceList';

        $list = SurfaceServices::getSurfaceList($request->user()->staff_num);

        $tableObj = new TableAuxiliary('surface/list', $list);
        $tableObj->column('title', '矿区单位名称');
        $tableObj->column('working_area', '工作面类型', function ($working_area) {
            return SurfaceEnum::WORKING_AREA_LIST[$working_area];
        });
        $tableObj->column('surface_name', '工作面名称');
        return $this->tableList($tableObj);
    }

    //查看工作面
    public function surfaceView(Request $request)
    {
        if (!$request->has('id')) {
            return $this->redirect('admin/surface/list');
        }

        $this->menusActive[0] = 'adminSurface';
        $this->menusActive[1] = 'adminSurfaceList';

        $list = SurfaceServices::getSrufaceDetail($request->input('id'));

        $tableObj = new TableAuxiliary('surface/list/view', $list);
        $tableObj->column('title', '矿区单位名称');
        $tableObj->column('working_area', '工作面类型', function ($working_area) {
            return SurfaceEnum::WORKING_AREA_LIST[$working_area];
        });
        $tableObj->column('surface_name', '工作面名称');
        return $this->tableView($tableObj);
    }

    //添加工作面
    public function surfaceAdd(Request $request)
    {
        $titleList = MineServices::getMineList();
        $titleArr  = [];
        foreach ($titleList['data'] as $key => $val) {
            $titleArr[$val['id']] = $val['name'];
        }

        $formObj = new FromAuxiliary('surface/list/add');
        $formObj->select('mine_id', '矿区单位', $titleArr);
        $formObj->select('working_area', '工作面类型', SurfaceEnum::WORKING_AREA_LIST);
        $formObj->input('surface_name', '工作面名称');
        $formObj->title = '添加工作面';
        return $this->from($formObj);
    }

    //添加工作面操作
    public function surfaceAddPost(Request $request)
    {
        if (!$request->input('mine_id') || !$request->input('surface_name') || !$request->input('working_area')) {
            return $this->error(1, '必填参数不能为空');
        }

        $result = SurfaceServices::addSurfaceList($request->all());

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

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

    //编辑工作面
    public function surfaceEdit(Request $request)
    {
        if (!$request->input('id')) {
            return $this->redirect('admin/surface/list');
        }

        $surface = WorkingSurface::find($request->input('id'));

        $titleList = MineServices::getMineList();
        $titleArr  = [];
        foreach ($titleList['data'] as $key => $val) {
            $titleArr[$val['id']] = $val['name'];
        }

        $formObj = new FromAuxiliary('surface/list/edit', $surface);
        $formObj->select('mine_id', '矿区单位', $titleArr);
        $formObj->select('working_area', '工作面类型', SurfaceEnum::WORKING_AREA_LIST);
        $formObj->input('surface_name', '工作面名称');
        $formObj->title = '编辑工作面';
        return $this->from($formObj);
    }

    //编辑工作面操作
    public function surfaceEditPost(Request $request)
    {
        if (!$request->input('mine_id') || !$request->input('surface_name') || !$request->input('working_area')) {
            return $this->error(1, '必填参数不能为空');
        }

        $result = SurfaceServices::editSurfaceList($request->all());

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

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

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

        $result = SurfaceServices::delSurfaceList($request->all());

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