<?php

namespace App\Http\Controllers;
use App\Ask;
use Illuminate\Http\Request;
use App\Charts\AskChart;
class AskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
      // $asks = Ask::all();
      // // dd($asks);
      // return view('ask.index')->with('asks', $asks);

      $asks = Ask::all();

      // $chart = new AskChart;
      // $chart->labels(['จำนวน', 'ชาย', 'หญิง']);
      // $chart->dataset('เพศ', 'line', [1, 2, 3, 4]);
      // // $chart = new AskChart(ctx, {
      //     type: 'bar',
      //     data: data,
      //     options: options
      // });
      return view('ask.index',['asks' => $asks]);
      // return view('ask.index',['asks' => $asks],['chart' => $chart]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('ask.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      $ask = new Ask;

      $ask->name = $request->name;
      $ask->sex = $request->sex;
      $ask->age = $request->age;
      $ask->p1 = $request->p1;
      $ask->p2 = $request->p2;
      $ask->p3 = $request->p3;
      $ask->p4 = $request->p4;
      $ask->commet = $request->commet;

      // $result = $ask->save();
      if($ask->save()){
        $request->session()->flash('status', 'เพิ่มข้อมูลเรียบร้อย');
        return view('ask.create');
      }else {
        $request->session()->flash('status', 'ไม่สำเร็จ');
        return view('ask.create');
      }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
