<?php

namespace App\Http\Controllers\admin\Download;


use Illuminate\Http\Request;
use App\Http\Requests\DownloadRequest;
use App\Http\Controllers\Controller;
use DB;
use Input;
use App\Models\Download\Download;
use App\Models\Download\DownloadClass;

class BackDownloadController 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' => '分類', 'width' => '9'),
            4  => array('title' => 'option', 'width' => '20')
        );
        $Data=DB::table('download')
            ->select('download.id','download.title','download_class.name')
            ->join('download_class','download.download_class','=','download_class.id')
            ->get();
        $Option = ['Img' => 1, 'Edit' => 1, 'Delete' => 1, 'Check' => 1, 'SmartSort' => 0, 'Add' => 0, 'AssignNewProduct' => 0,'AssignHotProduct'=>0];
        $ControllerName = "BackDownload";
        $DataTableJs    = DataTableJs("tableWithSearch", $BodyArray, $Data, $Option, $ControllerName, 2);
        return view('admin.DataTable.index', ['DataTableJs' => $DataTableJs, 'LevelOne' => '下載', 'LevelTwo' => '列表','AddUrl'=>'BackDownloadAdd']);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function add()
    {
        $BodyArray = $this->BodyArray();
        //表格標題
        $LayoutTitle = "新增檔案";
        //要POST的網址
        $Url = "BackDownload.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(DownloadRequest $request)
    {
    $Download=new Download;
    $Download->title=$request->title;
    $Download->download_class=$request->download_class;

    $Path = "Upload/Download/";
    $random_sn = date('ymdHis').rand(11111, 99999); 
    $download->file =$random_sn."." . Input::file('file')->getClientOriginalExtension();
    Input::file('file')->move($Path,$download->file);
    $download->save();

     return redirect()->to("BackDownload");
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit()
    {
        $download = download::where('id','=',Input::get('id'))->first();
        $BodyArray = $this->BodyArray($download);
        //表格標題
        $LayoutTitle = "修改檔案";
        //要POST的網址
        $Url = "BackDownload.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' => '修改檔案']);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
  

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(DownloadRequest $request)
    {
        $download=download::where('id','=',$request->id)->first();
        $download->title=$request->title;
        $download->download_class=$request->download_class;
        $download->download_writings=$request->download_writings;
        if(Input::hasfile('file')){
            $Path = "Upload/Download/";
            $random_sn = date('ymdHis').rand(11111, 99999); 
            $download->file =$random_sn."." . Input::file('file')->getClientOriginalExtension();
            Input::file('file')->move($Path,$download->file);
        }
        $download->update();

         return redirect()->to("BackDownload");
    }
    public function delete()
{
    $affairs=download::where('id','=',Input::get('id'))->first();
    $affairs->delete();
    return redirect()->to("BackDownload");
}
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function BodyArray($Data = 1) {
        $download_class=DB::table('download_class')
                                    ->select('id','name')
                                    ->get();
        $BodyArray = array(
            0 => array('id' => 'title', 'name' => 'title', 'CNname' => '標題：', 'placeholder' => "", 'class' => "form-control", 'readonly' => 'readonly', 'Option' => "", 'Type' => 'Text', "Value" => (is_object($Data)) ? $Data['title'] : null),
            1 => array('id' => 'download_class', 'name' => 'download_class', 'CNname' => '分類：', 'placeholder' => "", 'class' => "", 'readonly' => 'readonly', 'Option' => ArrayToLaravelFormSelectOption($download_class), 'Type' => 'Select', "Value" => (is_object($Data)) ?  $Data['id']  : null),
            2 => array('id' => 'id', 'name' => 'id', 'CNname' => '', 'placeholder' => "", 'class' => "", 'readonly' => 'readonly', 'Option' => '', 'Type' => 'Hidden', "Value" => (is_object($Data)) ?  $Data['id']  : null),
            3 => array('id' => 'file', 'name' => 'file', 'CNname' => '檔案', 'placeholder' => "", 'class' => "form-control", 'readonly' => '', 'Option' => 'url', 'Type' => 'FileBox', "Value" =>(is_object($Data)) ? $Data['file'] : null,"Path" => "./Upload/Download/"),

           
            );
        return $BodyArray;
    }
}
