<?php

namespace App\Http\Controllers\admin\Download;

use Illuminate\Http\Request;
use App\Http\Requests\DownloadClassRequest;
use App\Http\Controllers\Controller;
use DB;
use Input;
use App\Models\Download\Download;
use App\Models\Download\DownloadClass;
class BackDownloadClassController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $BodyArray = array(
            0  => array('title' => 'Id', 'width' => '5'),
            1  => array('title' => '名稱', 'width' => '9'),
            2  => array('title' => 'option', 'width' => '20')
        );
        $Data=DB::table('download_class')
                ->select('id','name')
                ->get();
        $Option = ['Img' => 0, 'Edit' => 1, 'Delete' => 1, 'Check' => 1, 'SmartSort' => 0, 'Add' => 0, 'AssignNewProduct' => 0,'AssignHotProduct'=>0];
        $ControllerName = "BackDownloadClass";
        $DataTableJs    = DataTableJs("tableWithSearch", $BodyArray, $Data, $Option, $ControllerName, 2);
        return view('admin.DataTable.index', ['DataTableJs' => $DataTableJs, 'LevelOne' => '下載管理', 'LevelTwo' => '檔案分類','AddUrl'=>'BackDownloadClassAdd']);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function add()
    {
        $BodyArray = $this->BodyArray();
        //表格標題
        $LayoutTitle = "新增檔案類別";
        //要POST的網址
        $Url = "BackDownloadClass.store";
        //表格簡介
        $LayoutBody = "";
        //app/Support/Helpers底下的自訂function
        $FormTtile = FormTtile($LayoutTitle, $LayoutTitle, $LayoutBody);
        $FormBody = FormBody($BodyArray, $Url);
        //統一使用admin.Form.index，除非特殊狀況
        return view('admin.Form.index', ['FormTtile' => $FormTtile, 'FormBody' => $FormBody, 'LevelOne' => '會務介紹', 'LevelTwo' => '新增檔案類別']);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(DownloadClassRequest $request)
    {
        $BackDownloadClass=new DownloadClass;
        $BackDownloadClass->name=$request->name;
        $BackDownloadClass->save();
        return redirect('BackAffairsClass');
    }

    public function edit()
    {
        $BackDownloadClass = DownloadClass::where('id','=',Input::get('id'))->first();
        $BodyArray = $this->BodyArray($BackDownloadClass);
        //表格標題
        $LayoutTitle = "新增檔案類別";
        //要POST的網址
        $Url = "BackDownloadClass.update";
        //表格簡介
        $LayoutBody = "";
        //app/Support/Helpers底下的自訂function
        $FormTtile = FormTtile($LayoutTitle, $LayoutTitle, $LayoutBody);
        $FormBody = FormBody($BodyArray, $Url);
        //統一使用admin.Form.index，除非特殊狀況
        return view('admin.Form.index', ['FormTtile' => $FormTtile, 'FormBody' => $FormBody, 'LevelOne' => '下載管理', 'LevelTwo' => '新增檔案類別']);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(DownloadClassRequest $request)
    {
        $BackDownloadClass=DownloadClass::where('id','=',Input::get('id'))->first();
        $BackDownloadClass->name=$request->name;
        $BackDownloadClass->update();
        return redirect('BackDownloadClass');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete()
    {
        $BackDownloadClass=DownloadClass::where('id','=',Input::get('id'))->first();
        $BackDownloadClass->delete();
        return redirect('BackAffairsClass');
    }
    public function BodyArray($Data = 1) {
        $BodyArray = array(
            0 => array('id' => 'name', 'name' => 'name', 'CNname' => '名稱：', 'placeholder' => "", 'class' => "form-control", 'readonly' => 'readonly', 'Option' => "", 'Type' => 'Text', "Value" => (is_object($Data)) ? $Data['name'] : null),
            1 => array('id' => 'id', 'name' => 'id', 'CNname' => '', 'placeholder' => "", 'class' => "", 'readonly' => '', 'Option' => '', 'Type' => 'Hidden', "Value" => (is_object($Data)) ?  $Data['id']  : null)
            );
        return $BodyArray;
    }
}
