<?php 
ini_set('session.gc_maxlifetime', 360000);
// each client should remember their session id for EXACTLY 4days
session_set_cookie_params(360000);
session_start();
include('sql_details.php');
 

$page = 'home';
$menu = 'no';
$product_id = $_GET['pid'];

$link = '#';


$page = 'editnewproduct';
$menu = 'yes';

$message = '';


if (isset($_POST['save_data'])) 
{
    // Check if a file was uploaded
    if (!empty($_FILES['validID_file']['name'])) {
        // Handle file uploads
        $upload_valid = handleFileUpload('validID_file', '../../member/user/products');
        
        // Update product image only if a new image is uploaded
        $updateImageSQL = "UPDATE tbl_product SET product_image = '$upload_valid' WHERE product_id = '$product_id'";
        
        if (mysqli_query($conn, $updateImageSQL)) {
            // File upload successful, move the file
            move_uploaded_file($_FILES['validID_file']['tmp_name'], $upload_valid);
        } else {
            $success_messages = '<script type="text/javascript">
                swal({
                    title: "Failed to update image!",
                    text: "Kindly Check if you missed any details",
                    type: "info"
                });
            </script>';
        }
    }


    $major_status = isset($_POST['major_sw']) ? 1 : 0;
    $promo_status = isset($_POST['promo_sw']) ? 1 : 0;


   

if ( $promo_status  == 1) {

  $promo_status = 1;
  $promo_price = mysqli_real_escape_string($conn, utf8_decode($_POST['promo_price']));
  $promo_valid_date = mysqli_real_escape_string($conn, $_POST['promo_valid_date']);

          if ($major_status == 1) {
            $srp = mysqli_real_escape_string($conn, utf8_decode($_POST['regular_srp1']));
            $major_product = 1;
            $solo_price = 0;
          }else {
            $srp = mysqli_real_escape_string($conn, utf8_decode($_POST['regular_srp']));
            $solo_price = mysqli_real_escape_string($conn, utf8_decode($_POST['solo_price']));
          }



}else {


            if ($major_status == 1) {
              $srp = mysqli_real_escape_string($conn, utf8_decode($_POST['regular_srp1']));
              $major_product = 1;
              $solo_price = 0;
           
             // $promo_valid_date = '0000-00-00';
            }else {
               $srp = mysqli_real_escape_string($conn, utf8_decode($_POST['regular_srp']));
              $solo_price = mysqli_real_escape_string($conn, utf8_decode($_POST['solo_price']));
          
            //  $promo_valid_date = '0000-00-00';
            }

}
       
    $cob = mysqli_real_escape_string($conn, $_POST['cob']);
    $sm_income = mysqli_real_escape_string($conn, $_POST['sm_income']);
    $sc_income = mysqli_real_escape_string($conn, $_POST['sc_income']);
    $sp_income = mysqli_real_escape_string($conn, $_POST['sp_income']);
    $cob_points = mysqli_real_escape_string($conn, $_POST['cob_points']);
    $sm_price = mysqli_real_escape_string($conn, $_POST['sm_price']);
    $sp_price = mysqli_real_escape_string($conn, $_POST['sp_price']);
    $sc_price = mysqli_real_escape_string($conn, $_POST['sc_price']);
    $members_price = mysqli_real_escape_string($conn, $_POST['members_price']);
   
    $product_name = mysqli_real_escape_string($conn, $_POST['product_name']);
    $category = mysqli_real_escape_string($conn, $_POST['category']);
    $description = mysqli_real_escape_string($conn, $_POST['description']);
  
    // Handle checkboxes
 
    // Construct and execute the SQL query for other fields
    $sqlUpdate = "UPDATE tbl_product SET 
    major_product = '$major_product',
        promo_price = '$promo_price',
         promo_status = '$promo_status',
        solo_price = '$solo_price',
        category = '$category',
        product_name = '$product_name',
        cob = '$cob',
        sm_earned = '$sm_income',
        coo_earned = '$sc_income',
        sp_earned = '$sp_income',
        cob_points = '$cob_points',
        sm_price = '$sm_price',
        sp_price = '$sp_price',
        coordinator_price = '$sc_price', 
        srp = '$srp',
        description = '$description'
        WHERE product_id = '$product_id'";

    if (mysqli_query($conn, $sqlUpdate)) {
        $success_messages = '<script type="text/javascript">
            swal({
                title: "Product Updated Successfully!",
                text: "Redirecting in 2 seconds.",
                type: "success",
                timer: 2000,
                showConfirmButton: false
            });
        </script>';
    } else {
        $success_messages = '<script type="text/javascript">
            swal({
                title: "Failed to update!",
                text: "Kindly Check if you missed any details",
                type: "info"
            });
        </script>';
    }
}

// Handle file upload function
function handleFileUpload($inputName, $destinationFolder) {
    $rand = rand();
    $upload_valid = $rand . '_' . date('Y-m-d') . '_' . $_FILES[$inputName]['name'];
    $file_item_loc_valid = $_FILES[$inputName]['tmp_name'];
    $file_store_valid = $destinationFolder . $upload_valid;
    return $file_store_valid;
}

function imageResize($imageSrc,$imageWidth,$imageHeight) {
  $newImageWidth = 500;
  $newImageHeight = 500;
  $newImageLayer=imagecreatetruecolor($newImageWidth,$newImageHeight);
  imagecopyresampled($newImageLayer,$imageSrc,0,0,0,0,$newImageWidth,$newImageHeight,$imageWidth,$imageHeight);
  return $newImageLayer;
} // Img Resize & Reduce Quality

function fn_resize($image_resource_id, $width, $height) {
  $target_width = 500;
  $target_height = 500;
  $target_layer = imagecreatetruecolor($target_width, $target_height);
  imagecopyresampled($target_layer, $image_resource_id, 0, 0, 0, 0, $target_width, $target_height, $width, $height);
  return $target_layer;
} // Sub Img Resize & Reduce Quality

function fn_resize_SubImg($image_resource_id, $width, $height) {
  $target_width = 500;
  $target_height = 500;
  $target_layer = imagecreatetruecolor($target_width, $target_height);
  imagecopyresampled($target_layer, $image_resource_id, 0, 0, 0, 0, $target_width, $target_height, $width, $height);
  return $target_layer;
} // variant Img Resize & Reduce Quality

?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Edit Product | Dashboard</title>
  <?php include_once('linkStyle.php'); ?>
  <link rel="stylesheet" href="plugins/summernote/summernote-bs4.css">
<style type="text/css">
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #6c757d;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

.switch-disabled .slider{
  pointer-events: none!important;
  cursor: not-allowed!important;
  background-color: #ccc;
}

input:checked + .slider {
  background-color: #ff7108;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
</style>
</head>
<body class="hold-transition sidebar-mini layout-fixed layout-footer-fixed layout-navbar-fixed">
  
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js" ></script>
  <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.css">

<div id="preloader" style="display:none;"></div>
<div class="wrapper">
  <nav class="main-header navbar navbar-expand navbar-white navbar-light">
    <ul class="navbar-nav">
      <li class="nav-item maxBars">
        <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
      </li>
      <li class="nav-item nav-link minBars pl-2 click-link">
        <a href="productList.php">
          <i class="fas fa-arrow-left text-supplier"></i>
        </a>
      </li>
      <li class="nav-item" style="line-height: 40px;">
        <span class="brand-text title-form-brand font-weight-bolder fontSize-20">Edit Product</span>
      </li>
    </ul>

    <ul class="navbar-nav ml-auto">
      <li class="nav-item minBars">
        <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
      </li>
      <li class="nav-item dropdown" id="dropDownFullScreen">
        <a class="nav-link" data-toggle="dropdown" href="#">
          <i class="fas fa-user"></i>
        </a>
        <div class="dropdown-menu dropdown-menu-sm dropdown-menu-right">
          <a href="#" class="dropdown-item">
            <i class="fas fa-user mr-2"></i> 
            <span class="text-muted text-sm">Profile</span>
          </a>
          <div class="dropdown-divider"></div>
          <a href="../logout.php" class="dropdown-item dropdown-footer">Logout</a>
        </div>
      </li>
    </ul>
  </nav>
  <!-- /.navbar -->
  <?php include_once('includes/sidebar.php'); ?>
  <div class="content-wrapper bg-white">
    <div class="content-header pb-5" style="background: #f4f6f9;" id="breadcrumb">
      <div class="container-fluid">
        <div>
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="index.php">Dashboard</a></li>
              <li class="breadcrumb-item active">Edit Product</li>
            </ol>
        </div>
      </div>
    </div>
 <section class="content">

      <div class="container-fluid pt-5 pb-5">
      <?php echo $success_messages ;?>

      <input type="hidden" value = "<?php echo $product_id  ;?>">
        <div class="row">
          <div class="col-lg-8 offset-lg-2">
            <h4 class="text-bold title-form" style="letter-spacing: 2">EDIT PRODUCT</h4>
            <form method="POST" enctype="multipart/form-data" autocomplete="off" id="newProduct">

            

            <?php
             
             if (empty($product_image)){


            ?> <center>
                <img src="dist/img/boxed-bg.jpg" style="border-radius: 10px;" id="displayPicture" width="200" height="200">
              </center>
              

              <div class="form-group mt-4 mb-2">
                <label class="label">Upload Product Image
                 <span class="text-danger">*</span>
                </label><br>
                <input type="file" name="validID_file" id="imgValidID" accept="image/*" onchange="readURL1(this);" required/>
                <br>
                <small class="text-success"><i>Only .jpg, .png file allowed for upload.</i></small>
              </div>
            <?php } else {?>
              <div class="title-sm">Current Product Image:</div>
              <div class="form-group mb-2">
          
              <img src= "<?php echo  'https://cobph.com/cob/member/user/'.$product_image ;?>" style="border-radius: 10px;" id="displayPicture" width="200" height="200">
            </div>

            <div class="form-group mt-4 mb-2">
                <label class="label">Upload New Image
                 <span class="text-danger">*</span>
                </label><br>
                <input type="file" name="validID_file" id="imgValidID" accept="image/*" onchange="readURL1(this);" />
                <br>
                <small class="text-success"><i>Only .jpg, .png file allowed for upload.</i></small>
              </div>
            
           <?php }?>
  


           <div class="row">
                  <div class=" col-md-12">
                    <div class="form-group">
                      <label class="label">Category <span class="text-danger">*</span></label>
 

                      <span id="mall-category">
                      <select class="form-control select2bs4" name="category" data-width="100%" required>
                              <option disabled>Select Category</option>
                              <?php 
                              $sqlCategoryMall = 'SELECT * FROM tbl_marketplace_product_category WHERE market_type = 0';
                              $resultCategoryMall = $conn->query($sqlCategoryMall);
                              while($rowCategoryMall = mysqli_fetch_array($resultCategoryMall)) {
                                  $categoryId = $rowCategoryMall['id'];
                                  $categoryName = $rowCategoryMall['category_name'];
                                  $selected = ($category == $categoryId) ? 'selected' : '';
                              ?>
                                  <option value="<?php echo $categoryId; ?>" <?php echo $selected; ?>>
                                      <?php echo $categoryName; ?>
                                  </option>
                              <?php } ?>
                  </select>

                      </span>

                      
                    </div>


              
                
                  </div>
       
                 
                </div>
             

                <div class="row">
                  <div class=" col-md-6">
                    <div class="form-group">
                      <label class="label">Product Name <span class="text-danger">*</span></label>
                      <input type="text" name="product_name" value="<?= $product_name; ?>" class="form-control" id="product_name" placeholder="Product Name" required>
                    </div>
                  </div>
                  <div class=" col-md-6">
                    <div class="form-group">
                      <label class="label">Stock <span class="text-danger">*</span></label>
                      <input type="number" value="<?= $stock; ?>" min ="1" name="stock" class="form-control" id="stock" placeholder="Product Stock" required>
                    </div>
                  </div>
                </div>

                <div class="form-group">
                <label class="label">Description</label>
                <textarea id="editor" class="form-control" name="description" placeholder="Description..."><?= htmlspecialchars($description); ?></textarea>
            </div>


              

 

   <div class="form-group">
    <?php if ($major_product == 1) {
        $checked = "checked";
        $major_div_style = 'style="display: block;"';
        $regular_div_style = 'style="display: none;"';
    } else {
        $checked = "";
        $major_div_style = 'style="display: none;"';
        $regular_div_style = 'style="display: block;"';
    }?>
    <center class="pr-4">This is a Major Product<br>
        <label class="switch mt-2" id="major-id" for="major">
            <input type="checkbox" <?=  $checked; ?> name="major_sw" id="major">
            <span class="slider round"></span>
        </label>
    </center>

    <div class="row">
        <div class="col-md-12" id="major-div" <?= $major_div_style; ?>>
            <div class="table-responsive">
                <table class="table border-0" id="dynamic_field_size">
                    <tr>
                        <td class="pl-0">
                            <small class="size" style="font-weight: bolder; font-size: 15px;">Regular Members (SRP)<span class="text-danger">*</span></small>
                            <input type="text" value="<?= $srp; ?>" name="regular_srp1" id="members-value" class="form-control size name_list" required/>
                        </td>
                    </tr>
                </table>
            </div>
        </div>

        <div class="col-md-12" id="regular-div" <?= $regular_div_style; ?>>
            <div class="table-responsive">
                <table class="table border-0" id="dynamic_field_size">
                    <tr>
                        <td class="pl-0">
                            <small class="size" style="font-weight: bolder; font-size: 15px;">Regular Members Price (SRP)<span class="text-danger">*</span></small>
                            <input type="text" value="<?= $srp; ?>" name="regular_srp" id="members-value" class="form-control size name_list" required/>
                        </td>
                        <td class="pl-0" width="50%">
                            <small class="size" style="font-weight: bolder; font-size: 15px;">Solo SRP Price<span class="text-danger">*</span></small>
                            <input type="text" value="<?= $solo_price; ?>" name="solo_price" id="solo-value" class="form-control size name_list" required/>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function () {
        const majorCheckbox = document.getElementById("major");
        const majorDiv = document.getElementById("major-div");
        const regularDiv = document.getElementById("regular-div");

        majorCheckbox.addEventListener("change", function () {
            if (this.checked) {
                majorDiv.style.display = "block";
                regularDiv.style.display = "none";
            } else {
                majorDiv.style.display = "none";
                regularDiv.style.display = "block";
            }
        });
    });
</script>

<div class="form-group">
                <div style="display: flex;">
             

                    <center class="pr-4">Set Variant<br>
                      <label class="switch mt-2" id="variant-id" for="variant">
                        <input type="checkbox" name="variant_sw" id="variant">
                        <span class="slider round"></span>
                      </label>
                    </center>

                  
                  
                </div>
              </div>

                <div id="variant-div" style="display: none;">
                  <div class="table-responsive">  
                    <table class="table border-0" id="dynamic_field">  
                      <tr>  
                        <td class="pl-0" width="10%">
                          <small style="font-weight: bolder; font-size: 15px;">Thumbnail URL</small><br>
                          <input type="file" name="thumbnail_variant[]" accept="image/*" class="name_list form-control-file variant-input mt-2" style="width: 150px; font-size: 13px;" required/>
                        </td>
                        <td class="pl-0">
                          <small class="size" style="font-weight: bolder; font-size: 15px;">Variant</small>
                          <input type="text" name="variant[]" id="variant-value" class="form-control variant name_list" required/>
                        </td>
                        <td class="pl-0" width="10%">
                          <br>
                          <center>
                            <button type="button" name="add" id="add" class="btn btn-success btn-block btn-radius" ><i class="fas fa-plus"></i></button>
                          </center>
                        </td>
                      </tr>  
                    </table>    
                  </div>  
                </div>

                <div id="size-div" style="display: none;">
                  <div class="table-responsive">  
                    <table class="table border-0" id="dynamic_field_size">  
                      <tr>  
                        <td class="pl-0">
                          <small class="size" style="font-weight: bolder; font-size: 15px;">Size Label</small>
                          <input type="text" name="size[]" id="size-value" class="form-control size name_list" required/>
                        </td>
                        <td class="pl-0" width="10%">
                          <br>
                          <center>
                            <button type="button" name="add" id="add-size" class="btn btn-success btn-block btn-radius size"><i class="fas fa-plus"></i></button>
                          </center>
                        </td>
                      </tr>  
                    </table>    
                  </div>  
                </div>

                <div id="suburl-div" style="display: none;">
                  <div class="table-responsive">  
                    <table class="table border-0" id="dynamic_field_suburl">  
                      <tr>  
                        <td class="pl-0">
                          <small class="size" style="font-weight: bolder; font-size: 15px;">Sub Image URL</small><br>
                          <input type="file" name="suburl[]" multiple accept="image/*" id="suburl-value" class="form-control-file suburl name_list" required/>
                        </td>
                      </tr>  
                    </table>    
                  </div>  
                </div>


        
<hr>
<h5><b class="text-danger">Regular Income:</b></h5>
                                            
                                      <div class="row">
                                                  <div class=" col-md-6">
                                                    <div class="form-group">
                                                      <label class="label">COB<span class="text-danger">*</span></label>
                                                      <input type="text"  value="<?= $cob; ?>" name="cob" id="bank_account_number" class="form-control" placeholder="COB" required>
                                                    </div> 
                                                  </div>

                                                      <div class="col-md-6 ">    
                                                        <div class="form-group">
                                                          <label class="label">COB Points<span class="text-danger">*</span></label>
                                                          <input type="text"  value="<?= $cob_points; ?>" name="cob_points" id="bank_account_number" class="form-control" placeholder="COB Points" required>
                                                        </div>
                                                     </div>

                                            
                                      </div>


                  <div class="row">
                                  <div class="col-md-6 ">
                                          <div class="form-group">
                                            <label class="label">SM Income<span class="text-danger">*</span></label>
                                            <input type="text"  value="<?= $sm_earned; ?>" name="sm_income" id="bank_account_number" class="form-control" placeholder="SM Income" required>
                                          </div> 
                                    </div>

                                        <div class="col-md-6 ">    
                                              <div class="form-group">
                                                <label class="label">SM Price<span class="text-danger">*</span></label>
                                                <input type="text"  value="<?= $sm_price; ?>" name="sm_price" id="bank_account_number" class="form-control" placeholder="SM Price" required>
                                              </div>
                                      </div>

                        
                  </div>

            <div class="row">
                      <div class="col-md-6 ">
                          <div class="form-group">
                            <label class="label">SP Income<span class="text-danger">*</span></label>
                            <input type="text"  value="<?= $sp_earned; ?>" name="sp_income" id="bank_account_number" class="form-control" placeholder="SP Income" required>
                          </div> </div>

                          <div class="col-md-6 ">    
                          <div class="form-group">
                            <label class="label">SP Price<span class="text-danger">*</span></label>
                            <input type="text"   value="<?= $sp_price; ?>" name="sp_price" id="bank_account_number" class="form-control" placeholder="SP Price" required>
                          </div></div>

                  
            </div>

                  <div class="row">
                            <div class="col-md-6 ">
                                <div class="form-group">
                                  <label class="label">SC Income<span class="text-danger">*</span></label>
                                  <input type="text"  value="<?= $coo_earned; ?>" name="sc_income" id="bank_account_number" class="form-control" placeholder="Sc Income" required>
                                </div> </div>

                                <div class="col-md-6 ">    
                                <div class="form-group">
                                  <label class="label">SC Price<span class="text-danger">*</span></label>
                                  <input type="text"   value="<?= $coordinator_price; ?>" name="sc_price" id="bank_account_number" class="form-control" placeholder="Sc Price" required>
                                </div></div> 
                              
                    </div>


                         
                        
<hr>

               
<?php if ($promo_status == 1) {
    $checked = "checked";
    $promo_div_style = 'style="display: block;"';
} else {
    $checked = "";
    $promo_div_style = 'style="display: none;"';
}?>

              <div class="form-group">
                <div style="display: flex;">
                <center class="pr-4">Set Promo<br>
                      <label class="switch mt-2" id="promo-id" for="promo">
                        <input type="checkbox" <?=  $checked; ?>  name="promo_sw" id="promo">
                        <span class="slider round"></span>
                      </label>
                </center>

 
                  
                  
                </div>
              </div>
   

<div id="promo-div" <?= $promo_div_style; ?>>
    <div class="table-responsive">  
        <table class="table border-0" id="dynamic_field_size">  
            <tr>  
                <td class="pl-0">
                    <small class="size" style="font-weight: bolder; font-size: 15px;">Promo Price</small>
                    <input type="number" value="<?= $promo_price; ?>" name="promo_price" id="promo-value" class="form-control size name_list" required/>
                </td>
                <td class="pl-0" width="50%">
                    <small class="size" style="font-weight: bolder; font-size: 15px;">Valid Until</small>
                    <input type="date" value="<?= isset($promo_valid_date) ? $promo_valid_date : ''; ?>" name="promo_valid_date" id="promo-date-value" class="form-control size name_list" required/>
  </td>
            </tr>  
        </table>    


             
<hr>
<h5><b class="text-info">Promo Income:</b></h5>
                                            
                                      <div class="row">
                                                  <div class=" col-md-6">
                                                    <div class="form-group">
                                                      <label class="label">COB<span class="text-danger">*</span></label>
                                                      <input type="text"  value="<?= $cob_promo; ?>" name="cob_promo" id="bank_account_number" class="form-control" placeholder="COB" required>
                                                    </div> 
                                                  </div>

                                                      <div class="col-md-6 ">    
                                                        <div class="form-group">
                                                          <label class="label">COB Points<span class="text-danger">*</span></label>
                                                          <input type="text"  value="<?= $cob_points_promo; ?>" name="cob_points_promo" id="bank_account_number" class="form-control" placeholder="COB Points" required>
                                                        </div>
                                                     </div>

                                            
                                      </div>


                  <div class="row">
                                  <div class="col-md-6 ">
                                          <div class="form-group">
                                            <label class="label">SM Income<span class="text-danger">*</span></label>
                                            <input type="text"  value="<?= $sm_earned_promo; ?>" name="sm_income_promo" id="bank_account_number" class="form-control" placeholder="SM Income" required>
                                          </div> 
                                    </div>

                                        <div class="col-md-6 ">    
                                              <div class="form-group">
                                                <label class="label">SM Price<span class="text-danger">*</span></label>
                                                <input type="text"  value="<?= $sm_price_promo; ?>" name="sm_price_promo" id="bank_account_number" class="form-control" placeholder="SM Price" required>
                                              </div>
                                      </div>

                        
                  </div>

            <div class="row">
                      <div class="col-md-6 ">
                          <div class="form-group">
                            <label class="label">SP Income<span class="text-danger">*</span></label>
                            <input type="text"  value="<?= $sp_earned_promo; ?>" name="sp_income_promo" id="bank_account_number" class="form-control" placeholder="SP Income" required>
                          </div> </div>

                          <div class="col-md-6 ">    
                          <div class="form-group">
                            <label class="label">SP Price<span class="text-danger">*</span></label>
                            <input type="text"   value="<?= $sp_price_promo; ?>" name="sp_price_promo" id="bank_account_number" class="form-control" placeholder="SP Price" required>
                          </div></div>

                  
            </div>

                  <div class="row">
                            <div class="col-md-6 ">
                                <div class="form-group">
                                  <label class="label">SC Income<span class="text-danger">*</span></label>
                                  <input type="text"  value="<?= $coo_earned_promo; ?>" name="sc_income_promo" id="bank_account_number" class="form-control" placeholder="Sc Income" required>
                                </div> </div>

                                <div class="col-md-6 ">    
                                <div class="form-group">
                                  <label class="label">SC Price<span class="text-danger">*</span></label>
                                  <input type="text"   value="<?= $coordinator_price_promo; ?>" name="sc_price_promo" id="bank_account_number" class="form-control" placeholder="Sc Price" required>
                                </div></div> 
                              
                    </div>


    </div>  
</div>
<script>
    document.addEventListener("DOMContentLoaded", function () {
        const promoCheckbox = document.getElementById("promo");
        const promoDiv = document.getElementById("promo-div");

        promoCheckbox.addEventListener("change", function () {
            if (this.checked) {
                promoDiv.style.display = "block";
            } else {
                promoDiv.style.display = "none";
            }
        });
    });
</script>

                
 

              <div class="form-group" id="saveDiv">
                <button type="button" data-toggle="modal" data-target="#add_product" class="btn btn-success btn-block btn-radius btn-save" id="save">Save Changes Now</button>
              </div>

              <div class="modal" id="add_product">
                <div class="modal-dialog">
                  <div class="modal-content btn-radius">
                    <div class="modal-header pb-0" style="border: 0px;">
                      <h5 class="modal-title" style="font-weight: bold;">Save Change Data</h5>
                    </div>
                    <div class="modal-body pt-0 pb-0">
                      <p class="text-muted">Are you sure you want to save Change?</p>
                    </div>
                    <div class="modal-footer pr-0 pl-0 pb-2 pt-0" style="display: block; border: 0px">
                      <div class="row">
                        <div class="col-6">
                          <button type="button" class="btn btn-default btn-block btn-save bg-white" data-dismiss="modal">Cancel</button>
                        </div>
                        <div class="col-6">
                          <input type="hidden" name="save_data" value="save">
                          <button type="button" id="action" class="btn btn-success btn-save btn-block">
                            <span id="button-text">Yes, Update</span>
                          </button>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div> <!-- modal save -->

            </form>
          </div> <!-- col -->
        </div> <!-- row -->

      </div>


    </section>
 
  <footer class="main-footer pb-2 pt-2 saveFooterDiv" style="box-shadow: 0 2px 5px 0 rgba(0,0,0,.16), 0 2px 10px 0 rgba(0,0,0,.12)!important;">
       <button type="button" data-toggle="modal" data-target="#add_product" class="btn btn-success btn-block btn-radius btn-save" id="save">Save Update Now</button>
  </footer>
  <aside class="control-sidebar control-sidebar-dark">
  </aside>
</div>
<!-- ./wrapper -->
 <div class="modal" id="alert_item_image">
    <div class="modal-dialog">
      <div class="modal-content btn-radius">
        <div class="modal-header pb-0" style="border: 0px;">
          <h5 class="modal-title" style="font-weight: bold;">Invalid Image Format</h5>
        </div>
        <div class="modal-body pt-0 pb-0">
          <p class="text-muted">Invalid format. Only jpg / jpeg/ png format allowed! Choose new image</p>
        </div>
        <div class="modal-footer pb-2 pt-0" style="border: 0px">
         <button type="button" class="btn btn-dark btn-radius" data-dismiss="modal">OK</button>       
       </div>
     </div>
   </div>
 </div>
 
<script src="plugins/jquery/jquery.min.js"></script>
<script src="plugins/jquery-validation/jquery.validate.min.js"></script>
<script src="plugins/jquery-validation/additional-methods.min.js"></script>
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<script src="plugins/select2/js/select2.full.min.js"></script>
<script src="dist/js/adminlte.min.js"></script>
<script src="plugins/summernote/summernote-bs4.min.js"></script>
<?php include_once('newOrderBadge.php'); ?>
<script type="text/javascript">
$(document).ready(function() {
  $('.click-link').click(function() {
    $('#preloader').show();
  });

  var form = $("#newProduct");
    form.validate({
      rules: {
        market_type: {
          required: true,
        },
      },
    errorElement: 'span',
    errorPlacement: function (error, element) {
      error.addClass('invalid-feedback');
      element.closest('.form-group').append(error);
    },
    highlight: function (element, errorClass, validClass) {
      $(element).addClass('is-invalid');
      $('#add_product').modal('hide');
    },
    unhighlight: function (element, errorClass, validClass) {
      $(element).removeClass('is-invalid');
    }
  });

  $('#action').click(function() {
    if ($('#newProduct').valid() === true){
      $('#button-text').html('Please wait...');
      $('#action').attr('disabled', true);
      $('#newProduct').submit();
    }else{
      $('#add_product').modal('hide');
    }
  });

  setTimeout(function() {
    $('#message').hide();
  }, 5000);

  $('#variant').change(function() {
    if($('#variant').is(":checked")) {
      $('#variant-div').fadeIn();
    }else{
      $('#variant-div').hide();
    }
  });


  $('#promo').change(function() {
    if($('#promo').is(":checked")) {
      $('#promo-div').fadeIn();
    }else{
      $('#promo-div').hide();
    }
  });



  $('#major').change(function() {
    if($('#major').is(":checked")) {
      $('#major-div').fadeIn();
      $('#regular-div').hide();
    }else{
      $('#major-div').hide();
      $('#regular-div').fadeIn();
    }
  });


  $('#size').change(function() {
    if($('#size').is(":checked")) {
      $('#size-div').fadeIn();
    }else{
      $('#size-div').hide();
    }
  });

  $('#suburl').change(function() {
    if($('#suburl').is(":checked")) {
      $('#suburl-div').fadeIn();
    }else{
      $('#suburl-div').hide();
    }
  });

   var i=1;  
   $('#add').click(function(){
     i++;  
     $('#dynamic_field').append('<tr id="row'+i+'" class="clear"><td class="pl-0" width="10%"><input type="file" name="thumbnail_variant[]" accept="image/*" class="name_list form-control-file variant-input mt-2" style="width: 150px; font-size: 13px;" required/></td><td class="pl-0"><input type="text" name="variant[]" class="form-control name_list" required/></td><td class="pl-0" width="10%"><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn-radius btn-block btn_remove" ><i class="fas fa-times"></i></button></td></tr>');  
   });  
    $(document).on('click', '.btn_remove', function(){  
     var button_id = $(this).attr("id");   
     $('#row'+button_id+'').remove();  
   });

   $('#add-size').click(function(){
     i++;  
     $('#dynamic_field_size').append('<tr id="row'+i+'" class="clear"><td class="pl-0"><input type="text" name="size[]" class="form-control name_list" required/></td><td class="pl-0"><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn-radius btn-block btn_remove"><i class="fas fa-times"></i></button></td></tr>');  
   });  
    $(document).on('click', '.btn_remove', function(){  
     var button_id = $(this).attr("id");   
     $('#row'+button_id+'').remove();  
   });

   $('.inputRadioProductType').click(function() {
    var categoryValue = $(this).val();
    $('#category-fake').hide();
    if (categoryValue == 0) {
      $('#mall-category').show();
      $('#grocery-category').hide();
    }else{
      $('#mall-category').hide();
      $('#grocery-category').show();
    }
   });
   
   $('#productPrimaryImg').change(function () {
    var ext = this.value.match(/\.(.+)$/)[1];
    switch (ext) {
      case 'jpg':
      case 'jpeg':
      case 'png':
      break;
      default:
      $("#alert_item_image").modal('show');
      this.value = '';
    }
  });
  
  $('.variant-input').change(function () {
    var ext = this.value.match(/\.(.+)$/)[1];
    switch (ext) {
      case 'jpg':
      case 'jpeg':
      case 'png':
      break;
      default:
      $("#alert_item_image").modal('show');
      this.value = '';
    }
  });
});   
 $(function () {
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    });
    $('.select2bs4new').select2({
      theme: 'bootstrap4'
    });
    $('#editor').summernote();
});
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}




$('#selectOption').change(function() {
    var selectedOptions = $(this).val();
    if (selectedOptions == 1) {
      $('.percentMarkupDiv').show();
      $('.fixedAmountDiv').hide();
      $('.markupFake').hide();
      $('#sellingprice').val('');
      $('#total_margin').val('0');
      $('#percent_markup').val('');
    }else{
      $('.percentMarkupDiv').hide();
      $('.fixedAmountDiv').show();
      $('.markupFake').hide();
      $('#sellingprice').val('');
      $('#total_margin').val('0');
      $('#fixed_amount').val('');
    }
  });

  $('#fixed_amount').keyup(function() {
    var fixedAmount = $(this).val();
    var supplierPrice = $('#supplier_price').val();
    var net_selling_total = parseInt(supplierPrice) - parseInt(fixedAmount);

    var shukrancom = (parseInt(net_selling_total) * parseInt(2)) / 100;
    var totalSellingPrice = parseInt(supplierPrice) - parseInt(fixedAmount)- parseInt(shukrancom);

    $('#sellingprice').val(totalSellingPrice);
    $('#total_margin').val(fixedAmount);
  });


  $('#percent_markup').keyup(function() {
    var percent = $(this).val();
    var supplier_price = $('#supplier_price').val();
    var percentage = (parseInt(supplier_price) * parseInt(percent)) / 100;
   var net_selling_total = parseInt(supplier_price) - parseInt(percentage);
    var shukrancom = (parseInt(net_selling_total) * parseInt(2)) / 100;
    var selling_total = parseInt(supplier_price) - parseInt(percentage) - parseInt(shukrancom);



    $('#sellingprice').val(selling_total);
    $('#total_margin').val(percentage);
  });


  $('#supplier_price').keyup(function() {
    $('#fixed_amount').val('');
    $('#percent_markup').val('');
    $('#sellingprice').val('');
    $('#total_margin').val('0');
  });



  function readURL1(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
      $('#displayPicture')
      .attr('src', e.target.result)
      .width(200)
      .height(200);
    };

    reader.readAsDataURL(input.files[0]);
  }
}

 

</script>
</body>
</html>


