<?php

include_once 'requestFunctions.php';
include_once 'inc/functions.php';
include_once 'inc/database.php';

if (isset($_POST["promotions_ids"]) && isset($_POST["store_hash"])) {
    $store_hash = $_POST["store_hash"];
   // echo "store hash",$store_hash;
    $access_token = $db->getSingleColumn("stores", "access_token", array("store_id" => $store_hash));

   // echo "aces  ",$access_token;

    $name = $_POST["promotion_name"];
    $currency_code=$_POST['currency_code'];
    $stop=$_POST['stop'];
    $discount=$_POST['discount'];

    $promo_prod_id=$_POST["Promotion_product_ids"];
    $promo_prod_qty=$_POST["promotion_minimum_quantity"];

     $target_prod_id=$_POST["target_product_id"];
     $target_prod_qty=$_POST["target_minimum_quantity"];

     $product_minimum_spend=$_POST["product_minimum_spend"];
     $Category_ids=$_POST["Category_ids"];

     $promotion_product_quantity=$_POST["promotion_product_quantity"];
     $target_product_quantity=$_POST["target_product_quantity"];
     $gift_product_id=$_POST["gift_product_id"];

     $customer_group_ids=$_POST["customer_group_ids"];
     $minimum_order_count=$_POST["minimum_order_count"];

    //echo "Propmotion ",$promo_prod_id,$promo_prod_qty,$target_prod_id,$target_prod_qty;


    // i send per and fixed amount so i track below we remove string from amount

    $needle = "per";


    if (strpos($discount, $needle) !== false) {

      // echo "per find";
    //echo "The string 'per' was found in the input string.";

      $discount_price=str_replace("per","",$discount);
       $discount1=array(
                                'percentage_amount'=>(int)$discount_price
                            );
      $discount_array=array(
                array(
                    "action"=> array(
                        "cart_value"=> array(
                            "discount"=> array(
                                "percentage_amount"=> (int)$discount_price
                            )
                        )
                    ),
                    
                   
                )
            );

    } else {
       // echo "fixed find";
       $discount_price=str_replace("fixed","",$discount);

       $discount1=array(
                            'fixed_amount'=>(int)$discount_price
                        );
        $discount_array=array(
                array(
                    "action"=> array(
                        "cart_value"=> array(
                            "discount"=> array(
                                "fixed_amount"=> (int)$discount_price
                            ),
                        )
                    )
                   
                )
            );
    }


    $end_date = $_POST["end_date"];

    $start_date = $_POST["start_date"];
    $promotions_id = $_POST["promotions_ids"];

    //echo "prom id",$promotions_id;

    $end_date = date("Y-m-d", strtotime( $end_date));
    $start_date = date("Y-m-d", strtotime( $start_date));

    $end_date=$end_date."T00:00:00+00:00";
    $start_date=$start_date."T00:00:00+00:00";

     $post_data = [
    'name' => $name,
     "currency_code"=>$currency_code,
    "rules"=>$discount_array,
    'start_date' =>$start_date,
    'end_date' => $end_date,
    'stope'     =>$stop,
    ];

    /*customer group ids pro*/

    if(!empty($customer_group_ids) && !empty($discount_price) )
    {
        $customer_ids_group=explode(",",$customer_group_ids);
        $customer_ids_group = array_map('intval', $customer_ids_group);
        $post_data=array();
        

         $post_data = array(
                'name'=> $name,
                "currency_code"=> $currency_code,
                'customer'=>array(
                    "group_ids"=>$customer_ids_group

                ),
                'rules'=>array(
                    array(
                        'action'=>array(
                            'cart_value'=>array(
                                'discount'=> array("percentage_amount"=>(int)$discount_price),
                                
                            )
                        ),
                        
                    )
                ),
               
                // 'priority'=>1,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'status'=>'ENABLED'
            );         

    }
    /*minimum order count*/
    if(!empty($minimum_order_count) && !empty($discount_price) )
    {
        
        $post_data=array();
        

         $post_data = array(
                'name'=> $name,
                "currency_code"=> $currency_code,
                'customer'=>array(
                    "minimum_order_count"=>(int)$minimum_order_count

                ),
                'rules'=>array(
                    array(
                        'action'=>array(
                            'cart_value'=>array(
                                'discount'=> array("percentage_amount"=>$discount_price),
                                
                            )
                        ),
                        
                    )
                ),
               
                // 'priority'=>1,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'status'=>'ENABLED'
            );         

    }


    if(!empty($product_minimum_spend) && !empty($discount_price) )
    {
        $post_data=array();
        

         $post_data = array(
                'name'=> $name,
                "currency_code"=> $currency_code,
                'rules'=>array(
                    array(
                        'action'=>array(
                            'cart_value'=>array(
                                'discount'=> array("fixed_amount"=>$discount_price),
                                
                            )
                        ),
                        'condition'=>array(
                            'cart'=>array(
                                "minimum_spend" =>$product_minimum_spend
                            )
                        )
                    )
                ),
               
                // 'priority'=>1,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'status'=>'ENABLED'
            );

         

    }

    if(!empty($product_minimum_spend) && !empty($gift_product_id) )
    {
        $post_data=array();
        

         $post_data = array(
                'name'=> $name,
                "currency_code"=> $currency_code,
                'rules'=>array(
                    array(
                        'action'=>array(
                            'gift_item'=>array(
                                'quantity'=>1,
                                'product_id'=>(int)$gift_product_id
                                
                            )
                        ),
                        'condition'=>array(
                            'cart'=>array(
                                "minimum_spend" =>$product_minimum_spend
                            )
                        )
                    )
                ),
               
                // 'priority'=>1,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'status'=>'ENABLED'
            );

         

    }



    if(!empty($Category_ids) && empty($promotion_product_quantity) && empty($target_product_quantity))
    {
        $post_data = array(
                'name'=> $name,
                "currency_code"=> $currency_code,
                'rules'=>array(
                    array(
                        'action'=>array(
                            'cart_items'=>array(
                                'discount'=> array(
                                "percentage_amount"=> (int)$discount_price
                            ), 
                                'items'=>array("categories"=>array($Category_ids))                           
                               
                            )
                        )
                    )
                ),
               
                // 'priority'=>1,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'status'=>'ENABLED'
            );

    }

     if(!empty($Category_ids) && !empty($promotion_product_quantity) && !empty($target_product_quantity))
    {
        $post_data = array(
                'name'=> $name,
                "currency_code"=> $currency_code,
                'rules'=>array(
                    array(
                        'action'=>array(
                            'cart_items'=>array(
                                'discount'=> array(
                                "percentage_amount"=> (int)$discount_price
                            ), 
                                'items'=>array("categories"=>array($Category_ids)),
                                'quantity'=>$promotion_product_quantity                          
                               
                            )
                        ),
                        'condition'=>array(
                            "cart"=>array(
                                "items"=>array(
                                    "categories"=>array($Category_ids)),
                                   'minimum_quantity'=>$target_product_quantity)
                        )
                    )
                ),
               
                // 'priority'=>1,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'status'=>'ENABLED'
            );

    }


    if(!empty($product_minimum_spend) && !empty($promo_prod_qty))
    {
         $post_data = array(
                'name'=> $name,
                "currency_code"=> $currency_code,
                'rules'=>array(
                    array(
                        'action'=>array(
                            'cart_items'=>array(
                                'discount'=> $discount1,                            
                                'items'=>array(
                                    'products'=>array(
                                        (int)$promo_prod_id
                                    )
                                ),
                                'quantity'=>(int)$promo_prod_qty
                            )
                        ),
                        'condition'=>array(
                            'cart'=>array(
                                "minimum_spend" =>$product_minimum_spend
                            )
                        )
                    )
                ),
               
                // 'priority'=>1,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'status'=>'ENABLED'
            );

    }


    if(!empty($promo_prod_id) && !empty($target_prod_id) )
    {

        $post_data = array(
                'name'=> $name,
                "currency_code"=> $currency_code,
                'rules'=>array(
                    array(
                        'action'=>array(
                            'cart_items'=>array(
                                'discount'=> $discount1,                            
                                'items'=>array(
                                    'products'=>array(
                                        (int)$promo_prod_id
                                    )
                                ),
                                'quantity'=>(int)$promo_prod_qty
                            )
                        ),
                        'condition'=>array(
                            'cart'=>array(
                                'items'=>array(
                                    'products'=>array(
                                        (int)$target_prod_id
                                    )
                                ),
                                'minimum_quantity'=>(int)$target_prod_qty
                            )
                        )
                    )
                ),
               
                // 'priority'=>1,
                'start_date'=>$start_date,
                'end_date'=>$end_date,
                'status'=>'ENABLED'
            );
    }


    $jsonData = json_encode($post_data);

  
    $url = "https://api.bigcommerce.com/stores/$store_hash/v3/promotions/$promotions_id";
    $headers = array(
        "accept: application/json",
        "content-type: application/json",
        "x-auth-client: " . CLIENT_ID,
        "x-auth-token: " . $access_token
    );
    $response = curlPutRequest($url, $headers, $jsonData);

   // print_r($response);

   // echo "Response1 ",$response;

  

    if(!empty($response["data"]["id"]))
    {
        echo json_encode(["msg"=>"Promtions Update Successfully"]);
    }
    else{
        
        echo json_encode(["msg"=>"Promtions Not Update Try Again"]);

    }
    
    
}
?>
