<?php

namespace App\Http\Controllers\Backend\DataManagement;

use Illuminate\Http\Request;
use App\Http\Services\Backend\DataManagement\TAuditManagementService;
use App\Http\Controllers\Controller;
use App\Http\Services\ConstDef\GeneralConst;
use App\Http\Services\CheckParamService;
use Redirect;

class TAuditManagementController extends Controller
{
    // 相關私有服務層調用器宣告
    private $tauditManagementSv;
    private $checkParamSv;
    
    public function __construct()
    {
        // 建構服務層調用器
        $this->tauditManagementSv = new TAuditManagementService();
        $this->checkParamSv = new CheckParamService();
        // 時區調整
        date_default_timezone_set("Asia/Taipei");
    }
    
    public function index()
    {
        // 渲染
        return view('admin.DataManagement.TAuditManagement');
    }
    
    public function grid_tround()
    {
        // 取得參數
        $param = $_GET;
        if ($param == null) exit();
        $draw = $param["draw"]; //客戶端傳來的查詢次數，無條件回傳用以核對
        $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送，但 mysql 從 1 開始算
        $orderDir = $param["order"][0]["dir"];
        $start = $param["start"];   // 頁碼
        $length = $param["length"]; // 一頁多大
        $searchValue = $param["search"]["value"];
        //客製化搜尋欄位
        // 驗證
        //資料庫
        $recordsTotal = 0;
        $result = $this->tauditManagementSv->getTRounds(
            $recordsTotal,
            $orderColumn,
            $orderDir,
            $start,
            $length,
            $searchValue
        );
        // 整理返回資料
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[] = array(
                //一般資料
                $result[ $i ]["BtnTAudit"],
                $result[ $i ]["id"],
                $result[ $i ]["roundName"],
                $result[ $i ]["dateBegin"],
                $result[ $i ]["dateFinal"],
                $result[ $i ]["redeemExtra"],
                $result[ $i ]["active"],
                $result[ $i ]["cdate"],
                $result[ $i ]["mdate"],
                $result[ $i ]["oid"],
            );
        }
        $json = array(
            "draw"            => $draw,
            "recordsTotal"    => $recordsTotal,
            "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數，本次筆數可從陣列取得
            "data"            => $data,
        );
        
        // 返回
        return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
    }
    
    public function grid_tcheckin()
    {
        // 取得參數
        $param = $_GET;
        if ($param == null) exit();
        $draw = $param["draw"]; //客戶端傳來的查詢次數，無條件回傳用以核對
        $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送，但 mysql 從 1 開始算
        $orderDir = $param["order"][0]["dir"];
        $start = $param["start"];   // 頁碼
        $length = $param["length"]; // 一頁多大
        $searchValue = $param["search"]["value"];
        //客製化搜尋欄位
        // 驗證
        //資料庫
        $recordsTotal = 0;
        $result = $this->tauditManagementSv->getTCheckins(
            $recordsTotal,
            $orderColumn,
            $orderDir,
            $start,
            $length,
            $searchValue
        );
        // 整理返回資料
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[] = array(
                //一般資料
                $result[ $i ]["BtnAudit"],
                $result[ $i ]["id"],
                $result[ $i ]["day"],
                $result[ $i ]["gp"],
                $result[ $i ]["cdate"],
                $result[ $i ]["mdate"],
                $result[ $i ]["oid"],
            );
        }
        $json = array(
            "draw"            => $draw,
            "recordsTotal"    => $recordsTotal,
            "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數，本次筆數可從陣列取得
            "data"            => $data,
        );
        
        // 返回
        return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
    }

    public function grid_tgood()
    {
        // 取得參數
        $param = $_GET;
        if ($param == null) exit();
        $draw = $param["draw"]; //客戶端傳來的查詢次數，無條件回傳用以核對
        $orderColumn = $param["order"][0]["column"] + 1; //前端從 0 開始送，但 mysql 從 1 開始算
        $orderDir = $param["order"][0]["dir"];
        $start = $param["start"];   // 頁碼
        $length = $param["length"]; // 一頁多大
        $searchValue = $param["search"]["value"];
        //客製化搜尋欄位
        // 驗證
        //資料庫
        $recordsTotal = 0;
        $result = $this->tauditManagementSv->getTGoods(
            $recordsTotal,
            $orderColumn,
            $orderDir,
            $start,
            $length,
            $searchValue
        );
        // 整理返回資料
        $data = array();
        for ($i = 0; $i < count($result); $i++) {
            $data[] = array(
                //一般資料
                $result[ $i ]["BtnTAudit"],
                $result[ $i ]["id"],
                $result[ $i ]["lp"],
                $result[ $i ]["ratio"],
                $result[ $i ]["active"],
                $result[ $i ]["totalQty"],
                $result[ $i ]["issuedQty"],
                $result[ $i ]["cdate"],
                $result[ $i ]["mdate"],
                $result[ $i ]["oid"],
            );
        }
        $json = array(
            "draw"            => $draw,
            "recordsTotal"    => $recordsTotal,
            "recordsFiltered" => $recordsTotal, //其實還是填入所有筆數，本次筆數可從陣列取得
            "data"            => $data,
        );
        
        // 返回
        return json_decode(json_encode($json, JSON_NUMERIC_CHECK), true);
    }
    
    public function pass_tround(Request $request, $id)
    {
        $this->tauditManagementSv->passTRound($id, $request->user()->id);
        
        return redirect('/backend/dataManagement/tauditManagement');
    }
    
    public function pass_tcheckin(Request $request, $id)
    {
        $this->tauditManagementSv->passTCheckin($id, $request->user()->id);
        
        return redirect('/backend/dataManagement/tauditManagement');
    }
    
    public function pass_tgood(Request $request, $id)
    {
        $this->tauditManagementSv->passTGood($id, $request->user()->id);
        
        return redirect('/backend/dataManagement/tauditManagement');
    }
    
}