<?php

namespace App\Http\Controllers\administrator\Construction\Purchase;

use App\ConstructionModel\CategoriesModel;
use App\PurchaseOrderSettingModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use App\ConstructionModel\BrandModel;

class PurchaseOrderSettingController extends Controller
{
    public function index(Request $request){
        $setting_amount = PurchaseOrderSettingModel::where('type','amount')->first();
        if($request->all()){

            PurchaseOrderSettingModel::where("type","amount_for_ceo_approval")->delete();
            $amount = new PurchaseOrderSettingModel();
            $amount->amount=$request->amount_for_ceo_approval;
            $amount->type='amount_for_ceo_approval';
            $amount->save();

            if($setting_amount){
                $setting_amount->amount = $request->amount;
                $setting_amount->update();
            }else{
                $amount = new PurchaseOrderSettingModel();
                $amount->amount=$request->amount;
                $amount->type='amount';
                $amount->save();
            }
            PurchaseOrderSettingModel::where('type','category')->delete();
            if($request->category){
                foreach ($request->category as $row){
                    $amount = new PurchaseOrderSettingModel();
                    $amount->category_id = $row;
                    $amount->type='category';
                    $amount->save();
                }
            }

            PurchaseOrderSettingModel::where('type','order_do_not_need_boq')->delete();
            if($request->cate_do_not_need_boq){
                foreach ($request->cate_do_not_need_boq as $row){
                    $amount = new PurchaseOrderSettingModel();
                    $amount->category_id = $row;
                    $amount->type='order_do_not_need_boq';
                    $amount->save();
                }
            }


            DB::table("nso007_purchase_order_setting_sub_category")->delete();
            DB::table('nso007_purchase_order_setting_limit_product')->delete();
            if(!empty($request->subcategory)){
                foreach($request->subcategory as $key=>$value){
                    if(!empty($value))
                    {
                        DB::table("nso007_purchase_order_setting_sub_category")->insert(['sub_category_id'=>$value,'category_id'=>BrandModel::find($value)->category_id]);
                        if(session()->has('subcategory-product')){
                            $data_subcategory = session('subcategory-product');
                            foreach($data_subcategory as $k=>$products){
                                foreach($products as $index=>$product){
                                    if($product['subcategory_id']==$value){
                                        DB::table('nso007_purchase_order_setting_limit_product')->insert(
                                            [
                                                'subcategory_id'=>$product['subcategory_id'],
                                                'product_id'=>$product['product_id'],
                                                'qty_limit'=>$product['qty'],
                                                'limit_by'=>($product['status']=="by-day"?1:($product['status']=="by-week"?2:($product['status']=="by-month"?3:'')))
                                            ]
                                        );
                                    }
                                }
                            }
                        }
                    }
                        
                }
            }

        }
        

        $this->put_session_product_setting();
        $amount_for_ceo_approval = PurchaseOrderSettingModel::where("type","amount_for_ceo_approval")->first();
        $cate = CategoriesModel::pluck('name','id');
        $setting_category = PurchaseOrderSettingModel::where('type','category')->pluck('category_id');
        $setting_order_do_not_need_boq = PurchaseOrderSettingModel::where('type','order_do_not_need_boq')->pluck('category_id');
        $subcategories = json_encode(DB::table("nso007_purchase_order_setting_sub_category")->get());
        // dd($setting_order_do_not_need_boq);
        return view("administrator.construction.purchase-setting.index",compact('cate','setting_amount','setting_category','setting_order_do_not_need_boq','subcategories','amount_for_ceo_approval'));
    }
    public function get_sub_category(Request $request)
    {
        $data = array();
        $data_value = array();
        foreach($request->data as $k=>$value)
        {
            $data = DB::table('nso007_product_brand_construction')->where("category_id",$value)->get();
            foreach($data as $value){
                array_push($data_value,$value);
            }
        }
        return json_encode($data_value);
    }
    public function put_session_product_setting(){
        $data = DB::table('nso007_purchase_order_setting_limit_product')->select('subcategory_id')->groupBy('subcategory_id')->get();
        $data_put = array();
        foreach($data as $k=>$value)
        {
            $sub_data = DB::table('nso007_purchase_order_setting_limit_product')->select('product_id','qty_limit','limit_by')->where('subcategory_id',$value->subcategory_id)->get();
            foreach($sub_data as $index=>$val){
                $data_put[$value->subcategory_id][$index] = array(
                    'subcategory_id' => $value->subcategory_id,
                    'product_id'     => $val->product_id,
                    'qty'            => $val->qty_limit,
                    'status'         => ($val->limit_by==1?"by-day":($val->limit_by==2?"by-week":($val->limit_by==3?"by-month":'')))
                );
            }
        }
        session()->put('subcategory-product', $data_put);
        return  $data_put;
    }
    public function save_product_data_setting(Request $request){
        // dd($request->all());        
        $subcategory_id = $request->subcategory_id;
        
        foreach($request->data_product as $k=>$val){
            $product_data[$k] = array(
                'subcategory_id' => $subcategory_id,
                'product_id'     => $val[0],
                'qty'            => $val[1],
                'status'         => $val[2]
            );
        }
        $data = [];$status = 0;
            if(session()->has('subcategory-product')){
                $data_get = session('subcategory-product');
                if(!empty( $data_get)){
                    foreach($data_get as $key=>$value){
                        if($key==$subcategory_id){
                            $data[$subcategory_id] = $product_data;
                            $status = 1;
                        }
                        else{
                            $data[$key] = $value;
                        }
                    }
                }
                else{
                    $data[$subcategory_id] = $product_data;
                    $status = 1;
                }
            }
            else{
                $data[$subcategory_id] = $product_data;
                $status = 1;
            }
            if($status==0){
                $data[$subcategory_id] = $product_data;
            }
        session()->put('subcategory-product', $data);
        return json_encode(session('subcategory-product'));
    }
}
