<?php
include_once("../include/main.inc.php");
check_session('MyCpanel', 'index.php');
?>
<?php
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start) < 0 or $start == "") ? 0 : $start;
$pagesize = intval($pagesize) == 0 ? $pagesize = 20 : $pagesize;

$heading = "Manage Brand";
$record_type = "Brand";
$script_name = "manage-brand.php";
$redirect_script_name = "manage-brand.php";
$table_name = "brand_tbl";
####################################################

if (isset($_POST['delete'])) {
  if (!empty($_POST['arr_id'])) {
    $arr_id = $_POST['arr_id'];
    foreach ($arr_id as $id) {
      $catid = mysqli_fetch_assoc(mysqli_query($dbconn, "SELECT catID FROM $table_name WHERE slno=".$id));
      mysqli_query($dbconn, "DELETE FROM $table_name WHERE slno = '$id'");
      mysqli_query($dbconn, "DELETE FROM category_tbl WHERE catID =".$catid['catID']);
    }
    $_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 . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
  exit;
}

admin_header("");
$qry = db_query("select brand_tbl.*, catName from $table_name LEFT JOIN category_tbl on category_tbl.catID=brand_tbl.catID");
$res = getResult($table_name, " where slno='$_GET[recordID]'");
?>

<style>
  .frm-error{
    color: red;
    font-size: 12px;;
  }
</style>

<div class="content-wrapper">
  <!-- <div class="page-header">
    <h3 class="page-title">Add New Brand</h3>
    <nav aria-label="breadcrumb">
      <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="<? admin_url ?>welcome.php">Admin</a></li>
        <li class="breadcrumb-item active" aria-current="page">Add New Brand</li>
      </ol>
    </nav>
  </div>
  <div class="row">
    <div class="col-12 grid-margin stretch-card">
      <div class="card">
        <div class="card-body">
          <form class="forms-sample" method="POST" action="" enctype="multipart/form-data">
            <div class="form-group">
              <label>Brand Name</label>
              <input class="typeahead" type="text" name="brandName" value="<?= $res['brandName'] ?>"
                placeholder="Brand Name" required>
            </div>
            <div class="form-group">
              <label>Brand Title</label>
              <input class="typeahead" type="text" name="brandTitle" value="<?= $res['brandTitle'] ?>"
                placeholder="Brand Title" required>
            </div>
            <?php
            if ($res['brandImage'] != Null) {
              ?>
              <div class="form-group row">
                <div class="col">
                  <label>Brand Logo</label>
                  <div id="the-basics">
                    <input class="dropify" type="file" name="brandImage">
                  </div>
                </div>
                <div class="col">
                  <div id="bloodhound">
                    <img src="<?= site_url . "/uploaded-files/brand-logo/" . $res['brandImage'] ?>" alt="<?= $res['brandName'] ?>"
                      title="<?= $res['brandName'] ?>">
                  </div>
                </div>
              </div>
            <?php } else { ?>
              <div class="form-group">
                <label>Brand Logo</label>
                <input class="dropify" type="file" name="brandImage" required>
              </div>
            <?php } ?>
            <?php if ($_GET[recordID] != Null) { ?>
              <button type="submit" class="btn btn-primary mr-2" name="add_record">Update Brand</button>
              <input type="hidden" name="recordID" value="<?= $res['slno']; ?>">
              <input type="hidden" name="id" value="update_record">
            <?php } else { ?>
              <button type="submit" class="btn btn-primary mr-2" name="submit">Submit</button>
              <input type="hidden" name="id" value="add_record">
            <?php } ?>
            <button class="btn btn-light"
              onclick="javascript: window.location.href = '<?= SITE_ADMIN_URL; ?>/<?= $redirect_script_name; ?>?parentID=<?= $parentID; ?>&catID=<?= $catID; ?>'">Cancel</button>

          </form>
        </div>
      </div>
    </div>
  </div> -->
  <div class="page-header">
    <h3 class="page-title">
      <?= $heading; ?>
    </h3>
    <nav aria-label="breadcrumb">
      <ol class="breadcrumb">
        <li class="breadcrumb-item active" aria-current="page">
          <a class="nav-link" href="#" data-toggle="modal" data-target="#addBox">
            <span class="btn btn-primary">+ Add New Brand</span>
          </a>
        </li>
      </ol>
    </nav>
  </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>Brand Name</th>
                    <th>Category Dislay Name</th>
                    <th>Brand Logo</th>
                    <th>Actions</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $sl = $start + 1;
                  while ($arr = mysqli_fetch_array($qry)) { ?>
                    <tr>
                      <td><input type="checkbox" name="arr_id[]" value="<?= $arr['slno'] ?>"></td>
                      <td><?= $sl ?></td>
                      <td><p class="ordern"><?= $arr['brandName']; ?></p></td>
                      <td><p class="ordern"><?= $arr['catName']; ?></p></td>
                      <td>
                        <p class="dongle-dcs">
                          <?php
                          if ($arr['brandImage'] != "") {
                            $file1 = SITE_DIR_PATH . "/uploaded-files/brand-logo/" . $arr['brandImage'];
                            $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=brand-logo/<?= $arr['brandImage']; ?>', '<?= $dimens[0]; ?>', '<?= $dimens[1]; ?>')"><span>View
                                Image <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
                                  class="bi bi-eye-slash" viewBox="0 0 16 16">
                                  <path
                                    d="M13.359 11.238C15.06 9.72 16 8 16 8s-3-5.5-8-5.5a7.028 7.028 0 0 0-2.79.588l.77.771A5.944 5.944 0 0 1 8 3.5c2.12 0 3.879 1.168 5.168 2.457A13.134 13.134 0 0 1 14.828 8c-.058.087-.122.183-.195.288-.335.48-.83 1.12-1.465 1.755-.165.165-.337.328-.517.486l.708.709z" />
                                  <path
                                    d="M11.297 9.176a3.5 3.5 0 0 0-4.474-4.474l.823.823a2.5 2.5 0 0 1 2.829 2.829l.822.822zm-2.943 1.299.822.822a3.5 3.5 0 0 1-4.474-4.474l.823.823a2.5 2.5 0 0 0 2.829 2.829z" />
                                  <path
                                    d="M3.35 5.47c-.18.16-.353.322-.518.487A13.134 13.134 0 0 0 1.172 8l.195.288c.335.48.83 1.12 1.465 1.755C4.121 11.332 5.881 12.5 8 12.5c.716 0 1.39-.133 2.02-.36l.77.772A7.029 7.029 0 0 1 8 13.5C3 13.5 0 8 0 8s.939-1.721 2.641-3.238l.708.709zm10.296 8.884-12-12 .708-.708 12 12-.708.708z" />
                                </svg></span></a>
                          <?php } ?>
                      </td>
                      <td>
                        <p class="download-dcs">
                          <a href="<?= SITE_ADMIN_URL.'/manage-brand-edit.php' ?>?recordID=<?= $arr['slno']; ?>"><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="delete" onclick="return select_chk()"
                  class="btn btn-danger btn-icon-text btn-sm">
                  <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash"
                    viewBox="0 0 16 16">
                    <path
                      d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z" />
                    <path fill-rule="evenodd"
                      d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z" />
                  </svg> Delete
                </button>
              </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
</div>

<div class="modal" id="addBox" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header p-2">
                <h5 class="modal-title">Add New Brand</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="form_ajax.php" class="form_submit" enctype="form-data/multipart">
                <div class="modal-body p-2">
                    <div class="form-group row">
                        <div class="col">
                            <label>Brand Name</label>
                            <div id="the-basics">
                                <input class="typeahead" type="text" name="brandname" placeholder="Brand Name" required>
                            </div>
                        </div>
                        <div class="col">
                            <label>Brand Title</label>
                            <div id="the-basics">
                                <input class="typeahead" type="text" name="brandtitle" placeholder="Brand Title" required>
                            </div>
                        </div>
                        <div class="col">
                            <label>Brand (Category) Display Name</label>
                            <div id="bloodhound">
                                <input class="typeahead" type="text" name="catDname" placeholder="Brand Display Name" required>
                            </div>
                        </div>
                    
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Meta Title</label>
                        <input class="typeahead" type="text" name="metatitle" placeholder="Meta Title">
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Meta Keyword</label>
                        <input class="typeahead" type="text" name="metakeyword" placeholder="Meta Keyword">
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Meta Description</label>
                        <textarea class="form-control" name="metadesc" rows="3"> </textarea>
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Short Description</label>
                        <textarea class="form-control editor" name="shortdesc" rows="6"></textarea>
                    </div>
                    <div class="form-group">
                        <label for="exampleTextarea1">Description</label>
                        <textarea class="form-control editor" name="fulldesc" rows="4"></textarea>
                    </div>

                    <div class="form-group">
                        <label for="exampleTextarea1">brand Image</label>
                        <input type="file" class="dropify" name="upFile" value="">
                    </div>
                    
                </div>
                <div class="modal-footer">
                    <input type="hidden" name="add_brand" value="add_brand">
                    <button type="submit" class="btn btn-primary">Save changes</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script language="javascript">
  function select_chk() {
    var chks = document.getElementsByName('arr_id[]');
    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(); ?>
<script>
    $(document).ready(function(){
        $('.form_submit').submit(function(e){
            e.preventDefault();
            let frm = $(this);
            let lnk = frm.attr('action');
            frm.find('input, textarea, select, button').prop('readonly', true);
            $('.frm-error').remove();
            $.ajax({
            url: lnk,
            data: new FormData(this),
            type:'post',
            dataType: 'JSON',
            processData: false,
            cache: false,
            contentType: false,
            success: function(r){
                if(r.success == true){
                if(r.alert){alert(alert());}
                if(r.msg){frm.before(r.msg);}
                if(r.rlink){window.location.href = r.rlink;}
                if(r.reload){location.reload();}
                }else{
                frm.find('input, textarea, select, button').prop('readonly', false);
                $.each(r.message, function(k,v){
                    $('input[name='+k+'],select[name='+k+'],textarea[name='+k+']').parent().before(v);
                });
                }
            }
            });
        });
    });
</script>