<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');
?>
<script type="text/javascript">
function chk_valid_date() {
    if (document.bannerFrm.validFrom.value == 0) {
        alert("Please select Validity Period");
        return false;
    }
    if (document.bannerFrm.validTo.value == 0) {
        alert("Please select Validity Period");
        return false;
    }
}
</script>
<?php
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;
$ress = getResult("admin_details", " where user_type='admin'");

$heading = "Manage Business We Serve";
$record_type = "Business We Serve";
$script_name = "business-we-serve.php";
$table_name = "business_tbl";
####################################################

if ($action == "add_record" && $name != "") {
  $numCnt = getCount($table_name, " where name='$name'");
    if ($numCnt > 0) {
      $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    } else {
    
      if ($_FILES['clientimg']['name']!=" ") {
        $file_extension = strtolower(getExtension($_FILES['clientimg']['name']));
        if ($_FILES['clientimg']['name'] != "") {
          $Client =clean($name);
          $img_name = $Client;
          $img = upload_file($img_name, "clientimg", "uploaded-files/business");
           
          $width="420";
          $height="490";
          $source_folder=SITE_DIR_PATH."/uploaded-files/business/";
          $destination_folder=SITE_DIR_PATH."/uploaded-files/business/thumbs/";
          saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);
  
          //admin thumb
          $width="32";
          $height="60";
          $source_folder=SITE_DIR_PATH."/uploaded-files/business/";
          $destination_folder=SITE_DIR_PATH."/uploaded-files/business/admin/thumbs/";
				  saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);

        }
      }
    
      $maxRes = db_fetch_array(db_query("select max(client_order) from $table_name"));
      $client_order = $maxRes[0] + 1;

      $sql = mysqli_query($GLOBALS['dbconn'],"insert into `$table_name` set name='$name',bn='$bn',bl='$bl',alt='$alt',Business_Heading='$Business_Heading',title='$title',caption='$caption',logo_image='$img',heading_icon='$heading_icon',client_order='$client_order'");
      //$_SESSION['session_message'] ='<div class="btn btn-success">' . $name . ' has been added successfully  </div> ';

      header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
      exit;
    }
  
}
################ Update Record ##################

if ($action == "update_record") {

  $numCnt = getCount($table_name, " where name='$name' and id!='$catID'");
  if ($numCnt > 0) {
    $_SESSION['session_message'] = '<div class="btn btn-warning">' . $record_type . ' already exist  </div>';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
  }

  if ($_FILES['clientimg']['name'] != "") {


    $file_extension = strtolower(getExtension($_FILES['clientimg']['name']));
    if ($file_extension != "jpg" && $file_extension != "gif" && $file_extension != "jpeg" && $file_extension != "png" && $file_extension != "webp") {
      $_SESSION['session_message'] = '<div class="btn btn-warning"> Please upload image (.jpg,.gif,.png Format Only) </div>';
      header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
      exit;
    }
    if ($_FILES['clientimg']['name'] != "") {

      $categoryRes = getResult($table_name, " where id='$catID'", "logo_image");
     

      $filename = $categoryRes['logo_image'];
  
      if ($filename != "") {
        $file = site_url . "/uploaded-files/business/" . $filename;
        if (file_exists($file)) {
          unlink($file);
        }
    }
   
      
        $Client =clean($name);
        $img_name = $Client;
   
        $img = upload_file($img_name, "clientimg", "uploaded-files/business");

                $width="420";
				$height="490";
				$source_folder=SITE_DIR_PATH."/uploaded-files/business/";
				$destination_folder=SITE_DIR_PATH."/uploaded-files/business/thumbs/";

        saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);

        //admin thumb
                $width="32";
				$height="60";
				$source_folder=SITE_DIR_PATH."/uploaded-files/business/";
				$destination_folder=SITE_DIR_PATH."/uploaded-files/business/admin/thumbs/";

				saveResizedPhoto($img, $width, $height, $source_folder, $destination_folder);

           
                
  
        updateTable($table_name, " logo_image='$img' where id='$catID'");

      
        


    
    }
}

  updateTable($table_name, " name='$name',bn='$bn',bl='$bl',caption='$caption',alt='$alt',title='$title',Business_Heading='$Business_Heading',heading_icon='$heading_icon' where id='$catID'");
  $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';
   
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
  exit;
}

####################################################

//Manage Default Heading/Content

if ($action == "update_heading" && $top_heading != "") {
 

  updateTable('headingAll_tbl', " heading1='$top_heading',content_desc='$top_content',bn='$bn',bl='$bl',recvDate=now() where id='1'");

      header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
      exit;  
}





if ($action == "Update") {
  $input = $_POST;
  $part = array_splice($input, 0, 2);

  while (list($key, $val) = each($input)) {
      if (substr($key, 0, 6) == "order_") {
          $key1 = substr($key, 6, strlen($key) - 6);

          $qry1 = db_query("update $table_name set client_order='$val' where id='$key1'");
      }
  }
  //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully  </div>';

  header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
  exit;
}

if (isset($_POST['active'])) {
  if (!empty($_POST['cat_arr'])) {
      $catid = $_POST['cat_arr'];
      foreach ($catid as $id) {
          mysqli_query($dbconn, "UPDATE $table_name SET status = 'Y' WHERE id = '$id'");
      }
      //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
  } else {
      //$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
  }
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
  exit;
}

if (isset($_POST['inactive'])) {
  if (!empty($_POST['cat_arr'])) {
      $catid = $_POST['cat_arr'];
      foreach ($catid as $id) {
          mysqli_query($dbconn, "UPDATE $table_name SET status = 'N'  WHERE id = '$id'");
      }
      //$_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been updated successfully</div>';
  } else {
      //$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
  }
  header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
  exit;
}

if (isset($_POST['delete'])) {
  if (!empty($_POST['cat_arr'])) {
      $arr_id = $_POST['cat_arr'];
      foreach ($arr_id as $id) {
          mysqli_query($dbconn, "DELETE FROM $table_name WHERE id = '$id'");
      }
      // $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been deleted successfully</div>';
  } else {
      //$_SESSION['session_message'] = '<div class="btn btn-danger">No Category selected</div>';
  }
  header("Location: ".SITE_ADMIN_URL."/".$script_name);
  exit;
}
admin_header("");
$qry= mysqli_query($dbconn,"select * from $table_name order by id desc");	  
$res = getResult($table_name, " where id='$catID'");
$resTop = getResult('headingAll_tbl', " where id='1'");
?>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title">
            <?=$heading;?>
        </h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">

                <li class="nav-item d-none d-lg-flex">
                    <a class="nav-link" href="#" data-toggle="modal" data-target="#edtPop">
                        <span class="btn btn-info">+ Manage Default Heading/Content</span>

                    </a>
                </li>
                <li class="nav-item d-none d-lg-flex">
                    <a class="nav-link" href="<?=admin_url?>business-we-serve.php">
                        <span class="btn btn-primary">+ Add New Business List</span>
                    </a>
                </li>
            </ol>
        </nav>
    </div>
    <div class="row">

        <div class="col-12 ">
            <div class="card">
                <div class="card-body">
                    <form class="forms-sample" method="POST" action="" enctype="multipart/form-data">
                        <div class="form-group row">
                            <div class="col">
                                <label>Business Name *</label>
                                <div id="the-basics">
                                    <input class="typeahead" type="text" name="name" value="<?=$res['name']?>"
                                        placeholder="" required>
                                </div>
                            </div>
                            <div class="col">
                                <label>Business Heading *</label>
                                <div id="bloodhound">
                                    <input class="typeahead" type="text" name="Business_Heading"
                                        value="<?=$res['Business_Heading']?>" placeholder="" required>
                                </div>
                            </div>
                        </div>

                        <div class="form-group row">
                            <div class="col">
                                <label>Business Heading Icon *</label>
                                <div id="the-basics">
                                    <textarea class="form-control editor" name="heading_icon" required
                                        rows="4"><?= $res['heading_icon'] ?></textarea>
                                </div>
                            </div>
                        </div>

                        <div class="form-group row">
                            <div class="col">
                                <label>Description *</label>
                                <div id="the-basics">
                                    <textarea class="form-control editor" id="myTextarea" name="caption"
                                        rows="4"><?= $res['caption'] ?></textarea>
                                </div>
                            </div>
                        </div>

                        <div class="form-group row">
                            <div class="col">
                                <label>Button Name</label>
                                <div id="the-basics">
                                    <input class="typeahead" type="text" name="bn" value="<?=$res['bn']?>"
                                        placeholder="">
                                </div>
                            </div>
                            <div class="col">
                                <label>Button Link</label>
                                <div id="bloodhound">
                                    <input class="typeahead" type="text" name="bl" value="<?=$res['bl']?>"
                                        placeholder="">
                                </div>
                            </div>
                        </div>


                        <!-- <div class="col">
                      <label>Image Caption</label>
                      <div id="bloodhound">
                        <input class="typeahead" type="text"  name="caption" value="<?=$res['caption']?>" placeholder="Image Caption" >
                      </div>
                    </div> -->

                        <?php
          if ($res['id'] == "") { ?>
                        <div class="form-group">
                            <label for="exampleTextarea1">Business Photo</label>
                            <input type="file" class="dropify" name="clientimg">
                        </div>
                        <?php } else{ ?>
                        <div class="form-group row">
                            <div class="col">
                                <label>Business Photo</label>
                                <input type="file" class="dropify" name="clientimg">
                            </div>
                            <div class="col">
                                <img src="<?=site_url."/uploaded-files/business/thumbs/".$res['logo_image']?>"
                                    alt="image" width="200" height="200">
                            </div>
                        </div>
                        <?php   } ?>
                        <div class="form-group row">
                            <div class="col">
                                <label>Alt</label>

                                <input class="typeahead" type="text" name="alt" value="<?=$res['alt']?>"
                                    placeholder="Image ALT">

                            </div>
                            <div class="col">
                                <label>Title</label>

                                <input class="typeahead" type="text" name="title" value="<?=$res['title']?>"
                                    placeholder="Image Title">
                            </div>
                        </div>
                        <?php
          if ($res['id'] == "") {
            ?>
                        <button type="submit" class="btn btn-primary mr-2" name="add_record">Submit</button>
                        <input type="hidden" name="action" value="add_record">
                        <?php
          } else {
            ?>
                        <button type="submit" class="btn btn-primary mr-2" name="update_record">Update</button>
                        <input type="hidden" name="action" value="update_record">
                        <?php
          }
          ?>
                        <button class="btn btn-light"
                            onclick="javascript: window.location.href = '<?=SITE_ADMIN_URL;?>/<?= $redirect_script_name; ?>'">Cancel</button>
                    </form>
                </div>
            </div>
            <div class="card">
                <div class="card-body">
                    <div class="row">
                        <div class="col-12">
                            <form action="" method="POST">
                                <div class="table-responsive">
                                    <table id="order-listing" class="table table-bordered table-hover">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>S.No.</th>
                                                <th>Business Name</th>
                                                <th>Image</th>
                                                <th>Order</th>
                                                <th>Status</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
 $sl=$start+1;
 while($arr=mysqli_fetch_array($qry)) { ?>
                                            <tr>
                                                <td><input type="checkbox" name="cat_arr[]" value="<?= $arr['id'] ?>">
                                                </td>
                                                <td><?=$sl?></td>
                                                <td>
                                                    <p class="ordern"><?=$arr['name'];?></p>
                                                </td>


                                                <td>
                                                    <?php
                                                if ($arr['logo_image'] != "") {
                                                    $file1 = SITE_DIR_PATH . "/uploaded-files/business/" . $arr['logo_image'];
                                                    $dimens = GetImageSize($file1);
                                                    if ($dimens[0] > 130) {
                                                        $dimens[0] = $dimens[0] + 30;
                                                        $dimens[1] = $dimens[1] + 80;
                                                    } else {
                                                        $dimens[0] = 200;
                                                        $dimens[1] = 200;
                                                    }
                                                ?>
                                                    <a href="javascript:void(0);"
                                                        onClick="openwin('<?= SITE_ADMIN_URL; ?>/popup_new.php?img=business/<?= $arr['logo_image']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')"
                                                        class="pay-status-dcs">

                                                        <span class="adminThumb">
                                                            <!-- <img src="<? //= SITE_ADMIN_URL; ?>/popup_new.php?img=category/images/<? //= $arr['catImage']; ?>', '<? //= $dimens[0]; ?>', '<? //= $dimens[1]; ?>'"> -->
                                                            <img
                                                                src="<?=site_url?>/uploaded-files/business/admin/thumbs/<?= $arr['logo_image']; ?>">
                                                        </span>

                                                    </a>
                                                    <?php } ?>
                                                </td>

                                                <td>
                                                    <input name="order_<?= $arr['id']; ?>" class="form-control order"
                                                        type="text" size="2" value="<?= $arr['client_order']; ?>">
                                                </td>

                                                <td>
                                                    <p class="status-dcs"><?php if ($arr['status'] == 'Y') { ?><label
                                                            class="badge badge-success badge-pill">Active</label><?php } else if ($arr['status'] == 'N') { ?>
                                                        <label class="badge badge-danger badge-pill">In-Active</label>
                                                        <?php } ?></p>
                                                </td>
                                                <td>
                                                    <p class="download-dcs">
                                                        <a
                                                            href="<?=SITE_ADMIN_URL?>/business-we-serve.php?catID=<?=$arr['id'];?>"><svg
                                                                xmlns="http://www.w3.org/2000/svg" width="24"
                                                                height="24" viewBox="0 0 24 24" fill="none"
                                                                stroke="currentColor" stroke-width="2"
                                                                stroke-linecap="round" stroke-linejoin="round"
                                                                class="feather feather-edit-3">
                                                                <path d="M12 20h9"></path>
                                                                <path
                                                                    d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z">
                                                                </path>
                                                            </svg></a>
                                                    </p>
                                                </td>
                                            </tr>

                                            <?php  $sl++; } ?>

                                        </tbody>
                                    </table>
                                    <div class="btn-grp">
                                        <button type="submit" name="active" onclick="return select_chk()"
                                            class="btn btn-success btn-icon-text btn-sm">
                                            <i class="fas fa-toggle-on mr-1"></i>Activate
                                        </button>
                                        <button type="submit" name="inactive" onclick="return select_chk()"
                                            class="btn btn-warning btn-icon-text btn-sm">
                                            <i class="far fa-times-circle mr-1"></i>Deactivate
                                        </button>

                                        <button type="submit" name="action" value="Update"
                                            class="btn btn-default btn-icon-text btn-sm">
                                            <i class="far fa-check-square mr-1"></i>Update Order
                                        </button>
                                        <button type="submit" name="delete" onclick="return select_chk()"
                                            class="btn btn-danger btn-icon-text btn-sm">
                                            <i class="fas fa-trash-alt mr-1"></i>Delete
                                        </button>

                                    </div>
                            </form>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>



    <div class="modal" id="edtPop" tabindex="-1" role="dialog">

        <div class="modal-dialog" role="document">

            <div class="modal-content">

                <div class="modal-header p-2">

                    <h5 class="modal-title">Add Heading/Content</h5>

                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">

                        <span aria-hidden="true">&times;</span>

                    </button>

                </div>

                <form action="" class="form_submit" enctype="form-data/multipart" method="post">

                    <div class="modal-body p-2">

                        <div class="form-group row">


                            <div class="col-md-12 mb-2">

                                <label>Heading<sup class="text-danger">*</sup></label>

                                <div id="the-basics">

                                    <input class="form-control form-control-sm" type="text"
                                        value="<?= $resTop['heading1'] ?>" name="top_heading" placeholder="" required>

                                </div>

                            </div>



                            <div class="col-md-12 mb-2">

                                <label>Content<sup class="text-danger">*</sup></label>

                                <div id="the-basics">


                                    <div id="the-basics">
                                        <textarea class="form-control editor" name="top_content"
                                            rows="2"><?= $resTop['content_desc'] ?></textarea>


                                    </div>

                                </div>

                            </div>

                            <div class="col-md-6 mb-2">

                                <label>Button Name<sup class="text-danger">*</sup></label>

                                <div id="the-basics">

                                    <input class="form-control form-control-sm" type="text"
                                        value="<?= $resTop['bn'] ?>" name="bn" placeholder="" >

                                </div>

                            </div>

                            
                            <div class="col-md-6 mb-2">

                                <label>Button Link<sup class="text-danger">*</sup></label>

                                <div id="the-basics">

                                    <input class="form-control form-control-sm" type="text"
                                        value="<?= $resTop['bl'] ?>" name="bl" placeholder="">

                                </div>

                            </div>



                        </div>

                    </div>

                    <div class="modal-footer">



                        <button type="submit" class="btn btn-primary mr-2" name="update_heading">Submit</button>
                        <input type="hidden" name="action" value="update_heading">


                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>

                    </div>

                </form>

            </div>

        </div>

    </div>
    <script language="javascript">
    function select_chk() {
        var chks = document.getElementsByName('cat_arr[]');
        var hasChecked = false;
        for (var i = 0; i < chks.length; i++) {
            if (chks[i].checked) {
                hasChecked = true;
                break;
            }
        }
        if (hasChecked == false) {
            alert("Please Select At Least One.");
            return false;
        }
    }
    </script>
    <?php
                  admin_footer();
                  exit;
                  ?>