<?php

require_once('Model/Queries.php');
require_once('Model/Constant.php');

require_once('Utility/Utility.php');
require_once('Object/DataObject.php');
require_once('Object/ResponseObject.php');
require_once('QueryBuilder/QueryDecorator.php');
require_once('QueryBuilder/CustomQuery.php');
require_once('../../config.php');


 class Shipping extends Constant implements Queries  {
   private $dtObject;
   private $actionType="";
   private $functionalityType="";
   public $postData = []; 
  

 	function __construct(DataObject $dtObject){

		
        parent::__construct();
 		$this->dtObject = $dtObject;
 		$this->actionType = $dtObject->getActionType();
 		$this->functionalityType = $dtObject->getFunctionalityType();
 		$this->postData = $dtObject->getPostData();
 		
 		
 	

 	}




 	public function create(){
        $listOfData = [];
        $postdata = $this->postData;

        if($this->functionalityType == "insert_shipping_data") {
            
            $name=$postdata['name'];
            $type=$postdata['type'];
            $days=$postdata['days'];
            $code=$postdata['code'];
            $symbol=$postdata['symbol'];
            $img=$postdata['img'];
            $logo=convert_save_image_from_base64("ic_ship".rand().".webp",$img,$this->shippingimages);

            $queryDecorator = new QueryDecorator("INSERT INTO CustomizedShipping (name,logo,type,estimated_delivery_days,currency_symbol,currency_code)  value('$name','$logo','$type','$days','$symbol','$code')",new CustomQuery("0",""));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data Retrieve Successfully","1","0",$listOfData,'');

        }

        else if($this->functionalityType == "insert_packages_data"){
            $name=$postdata['name'];
            $cust_id=$postdata['id'];
            $days=$postdata['days'];



            $queryDecorator = new QueryDecorator("INSERT INTO ShippingPackages (customized_shipping_id,name,delivery_days) value('$cust_id','$name','$days')",new CustomQuery("0",""));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data insert Successfully","1","0",$listOfData,'');

        }

        else if($this->functionalityType == "insert_sub_packages_data"){
            $id=$postdata['id'];
            $price=$postdata['price'];
            $from_weight=$postdata['from'];
            $to_weight=$postdata['to_weight'];



            $queryDecorator = new QueryDecorator("INSERT INTO ShippingPackagesMeta (term_id,from_weight,to_weight,rate) value('$id','$from_weight','$to_weight','$price')",new CustomQuery("0",""));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data insert Successfully","1","0",$listOfData,'');

        }

 	}

	public function retrieve(){
		$listOfData = [];

		$postdata = $this->postData;

		if($this->functionalityType == "retrieve_all_shipping") {
					$limit = 5;
				$page = $postdata['page'];

				if($postdata['page'] > 1)
				{
					$start = (($postdata['page'] - 1) * $limit);
					$page = $postdata['page'];
				}
			
			else
				{
					$start = 0;
				}

				$queryDecorator = new QueryDecorator("SELECT * FROM CustomizedShipping",new CustomQuery("0"," WHERE type = 'customized' "));
				$result = queryRunner($queryDecorator->getQuery());
				$total_data = countRow($result);

				$queryDecorator = new QueryDecorator("SELECT * FROM CustomizedShipping  WHERE type = 'customized' ",new CustomQuery("0"," LIMIT ".$start.",".$limit));
				

				$result = queryRunner($queryDecorator->getQuery());
				while ($row = rowRetriever($result)) {
					//  $pic_path = image_resize(, 40, 40);
					//  echo $pic_path;
						array_push($listOfData, [
								'id' => $row['id'] , 
								'name' => $row['name'],
                                 'type' => $row['type'],
                                'logo'=> $this->shippingimages.$row['logo'],
								'estimated_delivery_days' => $row['estimated_delivery_days'],
								'currency_symbol' => simplifySpecialCharacter($row['currency_symbol']),
								'currency_code' => $row['currency_code'],
								'enable' => $row['enable'],

							]);
					}
				
				$total_links = ceil($total_data/$limit);
							$previous_link = '';
							$next_link = '';
							$page_link = '';

							//echo $total_links;

							if($total_links > 4)
							{
								if($page < 5)
									{
									for($count = 1; $count <= 5; $count++)
										{
											$page_array[] = $count;
										}
										$page_array[] = '...';
										$page_array[] = $total_links;
								}
							else
							{
								$end_limit = $total_links - 5;
								if($page > $end_limit)
								{
								$page_array[] = 1;
								$page_array[] = '...';
								for($count = $end_limit; $count <= $total_links; $count++)
								{
									$page_array[] = $count;
								}
								}
								else
								{
									$page_array[] = 1;
									$page_array[] = '...';
									for($count = $page - 1; $count <= $page + 1; $count++)
									{
										$page_array[] = $count;
									}
									$page_array[] = '...';
									$page_array[] = $total_links;
							}
						}
					}
						else
							{
							for($count = 1; $count <= $total_links; $count++)
								{
								$page_array[] = $count;
					}
					}

					for($count = 0; $count < count($page_array); $count++)
					{
						if($page == $page_array[$count])
						{
							$page_link .= ' <li class="page-item "><a class="page-link active" href="#">'.$page_array[$count].'</a></li> ';

							$previous_id = $page_array[$count] - 1;
							if($previous_id > 0)
							{
								$previous_link = '<li class="page-item"><a class="page-link page_pre" href="#"  data-page_number="'.$previous_id.'"><span><i class="fas fa-angle-left"></i></span></a></li>';
							}
					
						else
						{
							$previous_link = '
												<li class="page-item disabled"><a class="page-link page_pre" href="#"><span><i
												class="fas fa-angle-left"></i></span></a></li>
											';
						}
						$next_id = $page_array[$count] + 1;
						if($next_id > $total_links)
						{
							$next_link = '
								<li class="page-item disabled"><a class="page-link page_next" href="#" data-page_number="'.$next_id.'"><span><i
								class="fas fa-angle-right"></i></span></a></li>
								';
						}
						else
						{
							$next_link = '<li class="page-item"><a class="page-link page_next" href="#" data-page_number="'.$next_id.'"><span><i class="fas fa-angle-right"></i></span></a></li>';
						}
						}
						else
							{
							if($page_array[$count] == '...')
								{
								$page_link .= '<li class="page-item disabled"><a class="page-link" href="#">...</a></li>';
								}
							else
							{
								$page_link .= '<li class="page-item"><a class="page-link" href="#" data-page_number="'.$page_array[$count].'">'.$page_array[$count].'</a></li>
						
											';
							}
							}
						}
						$htmlData = '<ul class="pagination pagination-sm">';
						$htmlData .= $previous_link . $page_link . $next_link;
						$htmlData .= '</ul>';
						
			return $this->getResponseObject($this->getSuccess_code(),"Data Retrieve Successfully","1","0",$listOfData,$htmlData);
		}
		else if ($this->functionalityType == "retrieve_shipping_packages") {
			
			$id=$postdata['id'];
			$queryDecorator = new QueryDecorator("SELECT * FROM ShippingPackages",new CustomQuery("0"," WHERE customized_shipping_id = '$id'"));
			// Utility::Logger($queryDecorator->getQuery());
			$result = queryRunner($queryDecorator->getQuery());
			while ($row = rowRetriever($result)) {

				array_push($listOfData, [
					'id' => $row['id'] , 
					'name' => $row['name'],
					'd_days'=> $row['delivery_days']
					

				]);


			}
            return $this->getResponseObject($this->getSuccess_code(),"Data Retrieve Successfully","1","0",$listOfData,'');
		}
		else if ($this->functionalityType == "retrieve_shipping_packages_meta") {
				$id=$postdata['id'];
				$queryDecorator = new QueryDecorator("SELECT * FROM ShippingPackagesMeta",new CustomQuery("0"," WHERE term_id = '$id'"));
				
				$result = queryRunner($queryDecorator->getQuery());
				while ($row = rowRetriever($result)) {

					array_push($listOfData, [
						'id' => $row['id'] , 
						'price' => $row['rate'],
						'from_weight'=> $row['from_weight'],
						'to_weight'=> $row['to_weight'],

						

					]);

				}
				return $this->getResponseObject($this->getSuccess_code(),"Data Retrieve Successfully","1","0",$listOfData,'');
		}
        else if ($this->functionalityType == "retrive_update_shipping_data") {
            $id=$postdata['id'];
            $queryDecorator = new QueryDecorator("SELECT * FROM CustomizedShipping",new CustomQuery("0"," WHERE id = '$id'"));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'id' => $row['id'] ,
                    'name' => $row['name'],
                    'type' => $row['type'],
                    'logo'=> $this->shippingimages.$row['logo'],
                    'estimated_delivery_days' => $row['estimated_delivery_days'],
                    'currency_symbol' => simplifySpecialCharacter($row['currency_symbol']),
                    'currency_code' => $row['currency_code'],
                    'enable' => $row['enable'],



                ]);

            }
            return $this->getResponseObject($this->getSuccess_code(),"Data Retrieve Successfully","1","0",$listOfData,'');
        }
        else if ($this->functionalityType == "retrive_update_shipping_packages_data") {
            $id=$postdata['id'];
            $queryDecorator = new QueryDecorator("SELECT * FROM ShippingPackages",new CustomQuery("0"," WHERE id = '$id'"));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'id' => $row['id'] ,
                    'name' => $row['name'],
                    'days' => $row['delivery_days'],





                ]);

            }
            return $this->getResponseObject($this->getSuccess_code(),"Data Retrieve Successfully","1","0",$listOfData,'');
        }
        else if ($this->functionalityType == "retrive_update_shipping_packages_meta_data") {
            $id=$postdata['id'];
            $queryDecorator = new QueryDecorator("SELECT * FROM ShippingPackagesMeta",new CustomQuery("0"," WHERE id = '$id'"));
            $result = queryRunner($queryDecorator->getQuery());
            while ($row = rowRetriever($result)) {

                array_push($listOfData, [
                    'id' => $row['id'] ,
                    'price' => $row['rate'],
                    'from_weight' => $row['from_weight'],
                    'to_weight' => $row['to_weight'],






                ]);

            }
            return $this->getResponseObject($this->getSuccess_code(),"Data Retrieve Successfully","1","0",$listOfData,'');
        }

		
	}

	public function update(){
        $listOfData = [];
        $postdata = $this->postData;

        if($this->functionalityType == "update_enable_data") {
            $id=$postdata['id'];
            $enable=$postdata['enable_val'];



            $queryDecorator = new QueryDecorator("UPDATE CustomizedShipping SET enable='$enable'",new CustomQuery("0"," WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data update Successfully","1","0",$listOfData,'');

        }
        else if($this->functionalityType == "update_shipping_data") {
            $id=$postdata['id'];
            $name=$postdata['name'];
            $type=$postdata['type'];
            $days=$postdata['days'];
            $code=$postdata['code'];
            $symbol=$postdata['symbol'];
            $img=$postdata['img'];



            $queryDecorator = new QueryDecorator("UPDATE CustomizedShipping SET 
            name ='$name', type='$type', estimated_delivery_days = '$days', currency_symbol = '$symbol', currency_code = '$code' 
           ",new CustomQuery("0",""));

            if (!isPath($img)) {

                $ship_image=convert_save_image_from_base64("ic_category_picture".rand().".webp",$img,$this->shippingimages);
                $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),new CustomQuery("0"," , logo ='$ship_image'"));


            }
            $queryDecorator = new QueryDecorator($queryDecorator->getQuery(),new CustomQuery("0"," WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data update Successfully","1","0",$listOfData,'');

        }
        else if($this->functionalityType == "update_shipping_packges_data") {
            $id=$postdata['id'];
            $name=$postdata['name'];
            $days=$postdata['days'];

            $queryDecorator = new QueryDecorator("UPDATE ShippingPackages SET name ='$name', delivery_days = '$days' ",new CustomQuery("0"," WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data update Successfully","1","0",$listOfData,'');

        }
        else if($this->functionalityType == "update_shipping_packges_meta_data") {
            $id=$postdata['id'];
            $price=$postdata['price'];
            $from_weight=$postdata['from_weight'];
            $to_weight=$postdata['to_weight'];


            $queryDecorator = new QueryDecorator("UPDATE ShippingPackagesMeta SET rate ='$price', from_weight = '$from_weight',to_weight= '$to_weight' ",new CustomQuery("0"," WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data update Successfully","1","0",$listOfData,'');

        }



	}

	public function delete(){
        $listOfData = [];
        $postdata = $this->postData;
        if($this->functionalityType == "delete_shipping_data"){
            $id=$postdata['id'];
            $queryDecorator = new QueryDecorator("DELETE FROM  CustomizedShipping",new CustomQuery("0"," WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data Delete Successfully","1","0",$listOfData,'');

        }
        else if($this->functionalityType == "delete_shipping_packages_data"){
            $id=$postdata['id'];
            $queryDecorator = new QueryDecorator("DELETE FROM  ShippingPackages",new CustomQuery("0"," WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data Delete Successfully","1","0",$listOfData,'');

        }
        else if($this->functionalityType == "delete_shipping_packages_meta_data"){
            $id=$postdata['id'];
            $queryDecorator = new QueryDecorator("DELETE FROM  ShippingPackagesMeta",new CustomQuery("0"," WHERE id = '$id'"));

            $result = queryRunner($queryDecorator->getQuery());

            return $this->getResponseObject($this->getSuccess_code(),"Data Delete Successfully","1","0",$listOfData,'');

        }




    }


	public function getResponseObject($code,$message,$setId,$itemCount,$data,$htmlData){

		$responseObject = new ResponseObject();
		$responseObject->setCode($code);
		$responseObject->setMessage($message);
		$responseObject->setId($setId);
		$responseObject->setListOfData($data);
		$responseObject->setTotalCount($itemCount);
		$responseObject->setHtmlData($htmlData);

		return $responseObject;

	}






 }



?>