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

$heading="Manage Download";
$record_type="Download";
$script_name="manage_download.php";
$table_name="download_tbl";
####################################################

if(isset($_REQUEST["add_record"])) {
    @extract($_REQUEST);
    $sql = db_query("INSERT INTO $table_name SET token_name='$title', link_window='$win_url', link_linux='$lin_url', link_mac='$mac_url'");
    if($sql){
        $_SESSION['session_message']='New '.$record_type.' has been added successfully';
    }else{
        echo $_SESSION['session_message']= mysqli_error($dbconn);
    }
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if(isset($_REQUEST["update_record"])) {
  mysqli_query($dbconn,"UPDATE `$table_name` SET token_name='$etitle', link_window='$ewin_url', link_linux='$elin_url', link_mac='$emac_url' where id='$update_record'");
  $_SESSION['session_message']=$record_type.' has been updated successfully';
  header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
  exit;
}

if (isset($_POST['delete'])) {
	if (!empty($_POST['cat_arr'])) {
		$catid = $_POST['cat_arr'];
		foreach ($catid 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= db_query("select * from $table_name");
$res=getResult($table_name," where id='$_GET[recordID]'");
?>

<div class="content-wrapper">
  <div class="page-header">
      <h3 class="page-title">Manage Download</h3>
      <nav aria-label="breadcrumb">
			  <ol class="breadcrumb">
          <li class="breadcrumb-item">
            <a class="nav-link" href="#" data-toggle="modal" data-target="#addVideo">
              <span class="btn btn-primary">+ Add New Download</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 class="_reab">
                          <div class="form-check mt-0">
                            <label class="form-check-label">
                              <input type="checkbox" class="form-check-input">
                              <i class="input-helper"></i>
                            </label>
                          </div>
                        </th>
                        <th>S.No.</th>
                        <th>Token Name</th>
                        <th>Download ink</th>
                        <th>Actions</th>
                      </tr>
                    </thead>
                    <tbody>
                    <?php 
                      $sl=$start+1;
                      while($arr=mysqli_fetch_array($qry)) { 
                    ?>
                      <tr>
                        <td>
                          <div class="form-check mt-0">
                            <label class="form-check-label">
                              <input type="checkbox" class="form-check-input" name="cat_arr[]" value="<?= $arr['id'] ?>">
                              <i class="input-helper"></i>
                            </label>
                          </div>
                        </td>
                        <td><?=$sl?></td>
                        <td><p class="ordern"><?=$arr['token_name'];?></p></td>
                        <td>
                            <p class="productid-dcs">Window : <a href="<?=$arr['link_window'];?>" target="_blank"><?=$arr['link_window'];?></a></p>
                            <p class="productid-dcs">Linux : <a href="<?=$arr['link_linux'];?>" target="_blank"><?=$arr['link_linux'];?></a></p>
                            <p class="productid-dcs">Mac : <a href="<?=$arr['link_mac'];?>" target="_blank"><?=$arr['link_mac'];?></a></p>
                        </td>
                        <td>
                              
                          <p class="download-dcs">
                            <a href="#" class="edtBtn" data-toggle="modal" data-target="#updateVideo" data-id="<?=$arr['id'];?>" data-name="<?=$arr['token_name'];?>" data-win="<?=$arr['link_window'];?>"  data-lin="<?=$arr['link_linux'];?>"  data-mac="<?=$arr['link_mac'];?>">
                              <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">
							<i class="fas fa-trash-alt mr-1"></i>Delete
                        </button>
                        </div>
                  </div>
        </form>
                </div>
              </div>
            </div>
          </div>
        </div>

        <div class="modal" id="updateVideo" tabindex="-1" role="dialog">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header p-3">
                <h5 class="modal-title">Update Downloads</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <form class="forms-sample" method="POST" action="" enctype="multipart/form-data">
                <div class="modal-body p-3">
                <div class="form-group mb-2">
                    <label>Title</label>
                    <input class="typeahead" type="text"  name="etitle" placeholder="Title" required>
                  </div>
                  
                  <div class="form-group mb-2">
                    <label>Download Link Windows</label>
                    <input class="typeahead" type="text" name="ewin_url" required>
                  </div>
                  <div class="form-group mb-2">
                    <label>Download Link Linux</label>
                    <input class="typeahead" type="text" name="elin_url" required>
                  </div>
                  <div class="form-group mb-2">
                    <label>Download Link Mac</label>
                    <input class="typeahead" type="text" name="emac_url" required>
                  </div>
                  
                </div>
                <div class="modal-footer p-3">
                  <input type="hidden" name="update_record" value="">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary">Submit</button>
                </div>
              </form>
            </div>
          </div>
        </div>

        <div class="modal" id="addVideo" tabindex="-1" role="dialog">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header p-3">
                <h5 class="modal-title">Add new Download</h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <form class="forms-sample" method="POST" action="" enctype="multipart/form-data">
                <div class="modal-body p-3">
                  <div class="form-group mb-2">
                    <label>Title</label>
                    <input class="typeahead" type="text"  name="title" placeholder="Title" required>
                  </div>
                  
                  <div class="form-group mb-2">
                    <label>Download Link Windows</label>
                    <input class="typeahead" type="text" name="win_url" required>
                  </div>
                  <div class="form-group mb-2">
                    <label>Download Link Linux</label>
                    <input class="typeahead" type="text" name="lin_url" required>
                  </div>
                  <div class="form-group mb-2">
                    <label>Download Link Mac</label>
                    <input class="typeahead" type="text" name="mac_url" required>
                    <input type="hidden" name="edtid">
                  </div>
                  
                </div>
                <div class="modal-footer p-3">
                  <input type="hidden" name="add_record" value="add_record">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary">Submit</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(); ?>
        <script language="javascript">
          $(document).ready(function(){
            $('.edtBtn').click(function(){
              $('input[name=etitle]').val($(this).data('name'));
              $('input[name=ewin_url]').val($(this).data('win'));
              $('input[name=elin_url]').val($(this).data('lin'));
              $('input[name=emac_url]').val($(this).data('mac'));
              $('input[name=update_record]').val($(this).data('id'));
            });
          });
        </script>