<?php

namespace App\Http\Controllers\PreOrder;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\PreOrder\PreOrderItem;
use App\Models\PreOrder\PreOrder;
use App\Models\STORE\Store;
use App\Models\STORE\Product;

class PreOrderItemController extends Controller
{
    //
    public function index(){

        if(check_roles('preorder_access')){

            try {


                $success = true;
                $message = 'ສຳເລັດ!';

            } catch (\Illuminate\Database\QueryException $ex) {
                $success = false;
                $message = $ex->getMessage();
            }

            $response = [
                'success' => $success,
                'message' => $message,
            ];
            return response()->json($response);

        } else {
            $response = ['message' => 'ທ່ານ ບໍ່ມີສິດເຂົ້າເຖິງຂໍ້ມູນ!', ];
             return response()->json($response);
         }

    }
    public function add_import(Request $request){
        if(check_roles('preorder_add')){

            try {

                //dd($request->preorder_import);
                
       
                foreach($request->preorder_import as $li)
                    {
                       // $acccat = SessionProduct::find($id);
                       // $check = Store::find($li['id'],'preoder_item_id');
                      // echo $li['item_tran_status'];
                       // if($check)
                            //{
                              //  echo $check;
                             // echo $request->item_id;
                             
                            if($li['product_id']!=null && $li['item_tran_status']=='pending'){
                               // echo $li['item_tran_status'];
                                if($request->item_id==null){
                                    
                                
                                    PreOrderItem::where('id',$li['id'])->update([ 'item_tran_status'=> 'imported','amount_instore'=> 0 ]);
                                    // add import
                                    $st = Store::all()->sortByDesc('id')->take(1)->toArray();
                                    $store_id = null;
                                    foreach($st as $re) { $store_id = $re['store_id']; }
                                    if($store_id!=null){ $store_id = (int)$store_id+1; } else { $store_id = 1; }
                                    $length = 8;
                                    $store_id = substr(str_repeat(0, $length).$store_id, - $length);
                        
                                    $store = new Store();
                                    $store->store_id = $store_id; 
                                    $store->product_id = $li['product_id'];
                                    $store->preorder_id = $request->preorder_id;
                                    $store->st_import_amount = $li['amount'];
                                    $store->st_import_pack_amount = null;
                                    $store->st_price_buy = $li['price_buy'];
                                    $store->st_price_sell = $li['price_sell'];
                                    $store->st_instore_sell = 0;
                                    $store->st_trn_id = null;
                                    $store->st_cashtype = 'true';
                                    $store->st_user_id = $request->user_id;
                                    $store->save();

                                    $product = Product::where('product_id',$li['product_id']);
                                    $product_que = Product::where('product_id',$li['product_id'])->get();
                                    $product->update([
                                        'in_store' => (int)$product_que[0]->in_store + (int)$li['amount'],
                                        'in_store_pack' => (int)$product_que[0]->in_store_pack + ((int)$li['amount']*(int)$product_que[0]->product_unit_type),
                                    ]);

                                    // update transport day com
                                    $check_day_com = PreOrder::where('preorder_id',$request->preorder_id)->get();
                                    if(!$check_day_com[0]['transport_day_com']){
                                        PreOrder::where('preorder_id',$request->preorder_id)->update(['transport_day_com' => date("d/m/Y")]);
                                    } 
                                    

                                    // ບັນທຶກ ການເຄື່ອນໄຫວ save logs
                                    save_logs('ບັນທຶກການ ນຳເຂົ້າສິນຄ້າໃໝ່, ເລກທີ່ນຳເຂົ້າ: '.$store_id.'. ຊື່ລາຍການ: '.$li['name'],'success');

                            } else if($request->item_id==$li['id']){
                               // echo $li['id'];
                                //return $request->preorder_id;
                                PreOrderItem::where('id',$li['id'])->update([ 'item_tran_status'=> 'imported','amount_instore'=> 0,]);
                                // add import
                                $st = Store::all()->sortByDesc('id')->take(1)->toArray();
                                $store_id = null;
                                foreach($st as $re) { $store_id = $re['store_id']; }
                                if($store_id!=null){ $store_id = (int)$store_id+1; } else { $store_id = 1; }
                                $length = 8;
                                $store_id = substr(str_repeat(0, $length).$store_id, - $length);
                    
                                $store = new Store();
                                $store->store_id = $store_id; 
                                $store->product_id = $li['product_id'];
                                $store->preorder_id = $request->preorder_id;
                                $store->st_import_amount = $li['amount'];
                                $store->st_import_pack_amount = null;
                                $store->st_price_buy = $li['price_buy'];
                                $store->st_price_sell = $li['price_sell'];
                                $store->st_instore_sell = 0;
                                $store->st_trn_id = null;
                                $store->st_cashtype = 'true';
                                $store->st_user_id = $request->user_id;
                                $store->save();

                                $product = Product::where('product_id',$li['product_id']);
                                $product_que = Product::where('product_id',$li['product_id'])->get();
                                $product->update([
                                    'in_store' => (int)$product_que[0]->in_store + (int)$li['amount'],
                                    'in_store_pack' => (int)$product_que[0]->in_store_pack + ((int)$li['amount']*(int)$product_que[0]->product_unit_type),
                                ]);


                                // update transport day com
                                $check_day_com = PreOrder::where('preorder_id',$request->preorder_id)->get();
                                if(!$check_day_com[0]['transport_day_com']){
                                    PreOrder::where('preorder_id',$request->preorder_id)->update(['transport_day_com' => date("d/m/Y")]);
                                } 


                                // ບັນທຶກ ການເຄື່ອນໄຫວ save logs
                                save_logs('ບັນທຶກການ ນຳເຂົ້າສິນຄ້າໃໝ່, ເລກທີ່ນຳເຂົ້າ: '.$store_id.'. ຊື່ລາຍການ: '.$li['name'],'success');
                     
                            }
                        } else if($li['product_id']==null && $li['item_tran_status']=='pending'){
                            PreOrderItem::where('id',$li['id'])->update([ 'item_tran_status'=> 'imported']);
                        }
                            
                        
                        // if(!in_array('pending',$import_it)){
                        //     print_r($import_it);
                        // }
                    }

                    $pre_order = PreOrderItem::where('preorder_id',$request->preorder_id)->get();   
                    $import_it = [];
                    foreach($pre_order as $item){
                        array_push($import_it,$item['item_tran_status']);
                    }  
                        if(!in_array('pending',$import_it)){
                           // print_r($import_it);
                           PreOrder::where('preorder_id',$request->preorder_id)
                                ->update([
                                    'transport_status' => 'imported',
                            ]); 
                        }
                    

                    // if($request->item_id==null){
                    //     PreOrder::where('preorder_id',$request->preorder_id)
                    //         ->update([
                    //             'transport_status' => 'imported',
                    //     ]);  
                    // }
                //    $check_im = PreOrderItem::where('preorder_id',$request->preorder_id)->get();
                //    $pen = array();
                //    foreach($check_im as $li)
                //    {
                      
                //         if($li['item_tran_status']=='pendding'){
                //             array_push($pen,'pendding');
                //         }
                //    }
                   
                //    if(!in_array('pendding',$pen)){
                //         PreOrder::where('preorder_id',$request->preorder_id)
                //             ->update([
                //                 'transport_status' => 'imported',
                //         ]); 
                //    }
                    


                $success = true;
                $message = 'ບັນທຶກຂໍ້ມູນ ສຳເລັດ!';

            } catch (\Illuminate\Database\QueryException $ex) {
                $success = false;
                $message = $ex->getMessage();
            }

            $response = [
                'success' => $success,
                'message' => $message,
            ];
            return response()->json($response);

        } else {
            $response = ['message' => 'ທ່ານ ບໍ່ມີສິດເຂົ້າເຖິງຂໍ້ມູນ!', ];
             return response()->json($response);
         }
    }

    public function add(Request $request){
        if(check_roles('preorder_add')){

            try {


                $success = true;
                $message = 'ບັນທຶກຂໍ້ມູນ ສຳເລັດ!';

            } catch (\Illuminate\Database\QueryException $ex) {
                $success = false;
                $message = $ex->getMessage();
            }

            $response = [
                'success' => $success,
                'message' => $message,
            ];
            return response()->json($response);

        } else {
            $response = ['message' => 'ທ່ານ ບໍ່ມີສິດເຂົ້າເຖິງຂໍ້ມູນ!', ];
             return response()->json($response);
         }
    }

    public function edit($id){
        if(check_roles('preorder_access')){

            try {


                $success = true;
                $message = 'ສຳເລັດ!';

            } catch (\Illuminate\Database\QueryException $ex) {
                $success = false;
                $message = $ex->getMessage();
            }

            $response = [
                'success' => $success,
                'message' => $message,
            ];
            return response()->json($response);

        } else {
            $response = ['message' => 'ທ່ານ ບໍ່ມີສິດເຂົ້າເຖິງຂໍ້ມູນ!', ];
             return response()->json($response);
         }

    }

    public function update($id, Request $request){
        if(check_roles('preorder_update')){

            try {


                $success = true;
                $message = 'ອັບເດດຂໍ້ມູນ ສຳເລັດ!';

            } catch (\Illuminate\Database\QueryException $ex) {
                $success = false;
                $message = $ex->getMessage();
            }

            $response = [
                'success' => $success,
                'message' => $message,
            ];
            return response()->json($response);

        } else {
            $response = ['message' => 'ທ່ານ ບໍ່ມີສິດເຂົ້າເຖິງຂໍ້ມູນ!', ];
             return response()->json($response);
         }

    }

    public function delete($id){
        if(check_roles('preorder_remove')){

            try {

                //$user = PreOrderItem::find($id);
               // $user->delete();
               PreOrderItem::where('preorder_id',$id)->update([ 'work_status'=> 'reject' ]);
          

                $success = true;
                $message = 'ລຶບຂໍ້ມູນ ສຳເລັດ!';

            } catch (\Illuminate\Database\QueryException $ex) {
                $success = false;
                $message = $ex->getMessage();
            }

            $response = [
                'success' => $success,
                'message' => $message,
            ];
            return response()->json($response);

        } else {
            $response = ['message' => 'ທ່ານ ບໍ່ມີສິດເຂົ້າເຖິງຂໍ້ມູນ!', ];
             return response()->json($response);
         }

    }
}
