<?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);
$res = getResult("admin_details", " where user_type='admin'");
$heading = "Add New Coupan Code";
$record_type = "Add New Coupan Code";
$script_name = "manage-coupan-codes.php";
$redirect_script_name ="manage-coupan-codes.php";
$table_name = "coupan_codes_tbl";
####################################################

if ($_POST['action'] == "add_record" && $code!=null) {
  // echo "insert into $table_name set code='$code', discount='$discount', startdate='$startdate',enddate='$enddate'"; 
  // die();
      $sql = db_query("insert into $table_name set code='$code', discount='$discount', startdate='$startdate',enddate='$enddate'");
      $_SESSION['session_message'] = '<div class="btn btn-success">' . $record_type . ' has been added successfully  </div>';
      header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
      exit;
}
################ Update Record ##################

if ($_POST['action'] == "update_record" && $code!=null) {
    updateTable($table_name, " code='$code', discount='$discount', startdate='$startdate',enddate='$enddate' where id='$id'");
    $_SESSION['session_message'] = $record_type . ' has been updated successfully';
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name);
    exit;
}
########################################################
admin_header();
$res = getResult($table_name, " where id='$id'");
?>
           <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="<?=$redirect_script_name?>">
              <span class="btn btn-primary">Go Back</span>
            </a>
          </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="">
                  <div class="form-group row">
                    <div class="col">
                      <label>Coupan Code</label>
                      <div id="the-basics">
                        <input class="typeahead" type="text" name="code" value="<?=$res['code']?>" placeholder="Coupan Code" Required>
                      </div>
                    </div>
                    <div class="col">
                      <label>Discount</label>
                      <div id="bloodhound">
                        <input class="typeahead" type="number"  name="discount" value="<?=$res['discount']?>" placeholder="Discount" Required>
                      </div>
                    </div>
                  </div>
                  <div class="form-group row">
                    <div class="col">
                      <label>Start Date</label>
                      <div id="the-basics">
                        <input class="typeahead" type="date" name="startdate" value="<?=$res['startdate']?>" Required>
                      </div>
                    </div>
                    <div class="col">
                      <label>Expire Date</label>
                      <div id="bloodhound">
                      <input class="typeahead" type="date" name="enddate" value="<?=$res['enddate']?>" Required>
                      </div>
                    </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>
          </div>
        </div>
        <!-- content-wrapper ends -->
<?php
admin_footer();
exit;
?>

