<?php
	include "../db_connection/db_connection.php";
	include "../config/config.php";

//$connect = new PDO("mysql:host=localhost; dbname=thahaayaa", "root", "");

/*function get_total_row($connect)
{
  $query = "
  SELECT * FROM tbl_webslesson_post
  ";
  $statement = $connect->prepare($query);
  $statement->execute();
  return $statement->rowCount();
}

$total_record = get_total_row($connect);*/

$limit = '5';
$page = 1;
if($_POST['page'] > 1)
{
  $start = (($_POST['page'] - 1) * $limit);
  $page = $_POST['page'];
}
else
{
  $start = 0;
}

$query = "
SELECT * FROM design_sel_det ";

if($_POST['query'] != '')
{
  $query .= '
  WHERE sel_date LIKE "%'.str_replace(' ', '%', $_POST['query']).'%" 
  ';
}

$query .= 'ORDER BY design_sel_det_id ASC ';

$filter_query = $query . 'LIMIT '.$start.', '.$limit.'';

$statement = mysqli_query($connect,$query);//$connect->prepare($query);
//$statement->execute();
$total_data = mysqli_num_rows($statement);//$statement->rowCount();

$statement1 = mysqli_query($connect,$filter_query);//$connect->prepare($filter_query);
//$statement->execute();
//$result = $statement->fetchAll();
$total_filter_data = mysqli_num_rows($statement1);//$statement->rowCount();

$paginationHtml = '
<label>Total Records - '.$total_data.'</label>
<table class="table table-striped table-bordered">
  <tr>
    <th>ID</th>
    <th>Post Title</th>
  </tr>
';
if($total_data > 0)
{
	$st_loop=1;
	$loop=1;
  //foreach($result as $row) {
	  while($row= mysqli_fetch_array($statement1,MYSQLI_ASSOC)){
    /*$output .= '
    <tr>
      <td>'.$row["design_det_id"].'</td>
      <td>'.$row["sel_date"].'</td>
    </tr>
    ';*/
	$pa_id	=	base64_encode(base64_encode(base64_encode($row['party_det_id'])));
				$sel_date = base64_encode(base64_encode(base64_encode($row['sel_date'])));

				$SLCT_dd = "SELECT COUNT(design_sel_det_id) AS sel_cnt,IFNULL(SUM(master_pieces),0) AS mas_pic FROM design_sel_det,design_det WHERE  party_det_id = '".$row['party_det_id']."' and `sel_date` ='".$row['sel_date']."' AND design_det.design_det_id = design_sel_det.design_det_id GROUP BY sel_date";
				$QURY_dd = mysqli_query($connect,$SLCT_dd);
				$FTCH_dd = mysqli_fetch_object($QURY_dd);
				

				$SLCT_E = "SELECT cmp_name,party_code FROM party_det WHERE party_det_id = '".$row['party_det_id']."' "; 
				$QURY_E = mysqli_query($connect,$SLCT_E);
				$FTCH_E = mysqli_fetch_object($QURY_E);

				$SLCT_Ec = "SELECT COUNT(design_sel_det_id) AS des_cnt,IFNULL(SUM(master_pieces),0) AS mas_pic FROM design_sel_det,design_det WHERE party_det_id = '".$row['party_det_id']."' AND design_det.design_det_id = design_sel_det.design_det_id AND ord_status = 'yes' and `sel_date` ='".$row['sel_date']."'";

				$QURY_Ec = mysqli_query($connect,$SLCT_Ec);
				$FTCH_Ec = mysqli_fetch_object($QURY_Ec);

				$SLCTre = "SELECT party_det_id,sel_date FROM design_sel_det WHERE party_det_id = '".$row['party_det_id']."' GROUP BY STR_TO_DATE(sel_date,'%d-%m-%Y') ORDER BY design_sel_det_id DESC";

				$QURYre = mysqli_query($connect,$SLCTre);
				$nu_rows = 0;
				$rowsre = mysqli_num_rows($QURYre);
				if($rowsre>0){
				while($FTCHre = mysqli_fetch_object($QURYre))
				{
					$SLCTre_sub = "SELECT * FROM design_sel_det WHERE party_det_id = '$FTCHre->party_det_id' and sel_date = '$FTCHre->sel_date' and ord_status='no'";
					//echo $SLCTre;
					$QURYre_sub = mysqli_query($connect,$SLCTre_sub);
					if(mysqli_num_rows($QURYre_sub)>0)
					{
						$nu_rows++;
					}
				}
				}

				$SLCT_DES = "SELECT COUNT(des_sel_cancel_id) AS canc_cnt FROM des_sel_cancel WHERE party_det_id = '".$row['party_det_id']."' AND sel_date = '".$row['sel_date']."'";
				$QURY_DES = mysqli_query($connect,$SLCT_DES);
				$FTCH_DES = mysqli_fetch_object($QURY_DES);

			$paginationHtml.='<tr>';
			$paginationHtml.='<td align="center" style="vertical-align:middle;">'.$st_loop.'</td>';
			$paginationHtml.='<td align="center" style="vertical-align:middle;">'.$FTCH_E->party_code.'</td>';
			if($admin_flagg==1){
				$paginationHtml.='<td>';
				$name_count = strlen($FTCH_E->cmp_name);
				$name='';
				if($name_count>25){
					$name = substr($FTCH_E->cmp_name,0,25)."...";
				} else {
					$name = $FTCH_E->cmp_name;
				} 

				if($name_count>25){
					$paginationHtml.='<div class="tooltip1">'.$name.'
				<span class="tooltiptext1">'.$FTCH_E->cmp_name.'</span>
			</div>';
				} else {
					$paginationHtml.= $name;
				}

				$paginationHtml.='</td>';
			}

		$paginationHtml.='<td align="center" style="vertical-align:middle; text-align:center;">'.str_replace("-","/",$row['sel_date']).'</td>';

		$paginationHtml.='<td align="center" style="vertical-align:middle; text-align:center;">'.$FTCH_dd->sel_cnt;

		if($FTCH_DES->canc_cnt > 0){ 
			$paginationHtml.='<a id="viewlink1'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" href="clien_cancel_view.php?po_id='.$pa_id.'&sel_date='.$sel_date.'" class="an_own">'.$FTCH_DES->canc_cnt.'</a>';
		}
		$paginationHtml.='</td>
			<td class="center" style="vertical-align:middle; text-align:center;">'.$FTCH_dd->mas_pic.'</td>
            <td class="center" style="vertical-align:middle; text-align:center;">';

			if($FTCH_Ec->des_cnt > 0){ 

			$paginationHtml.='<a id="viewlink2'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" href="cli_ord_req_po_view.php?pa_id='.$pa_id.'&sel_date='.$sel_date.'" style="text-decoration:underline">'.$FTCH_Ec->des_cnt.'</a>';
			} else {
				$paginationHtml.= $FTCH_Ec->des_cnt; 
				} 
			$paginationHtml.='</td>
			<td class="center" style="vertical-align:middle; text-align:center;">'.$FTCH_Ec->mas_pic.'</td>
			<td class="center" style="vertical-align:middle; text-align:center;">';
			
			 if($FTCH_Ec->des_cnt != $FTCH_dd->sel_cnt){ 
			$paginationHtml.='<a href="cli_ord_req_cancel.php?pa_id='.$pa_id.'&sel_date='.$sel_date.'&bck_flg='.$bck_flg.'&flg='.$flg1.'" class="an_own" id="viewlink3'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)">Cancel</a>';
			 }else{  
			$paginationHtml.='-----';
			 } 
			$paginationHtml.='</td>
            <td class="center" align="center" style="vertical-align:middle;">';
			if($FTCH_Ec->des_cnt != $FTCH_dd->sel_cnt){ 
			if($nu_rows > 1){ 
				$paginationHtml.='<a id="viewlink4'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" href="cli_ord_req_sub_view.php?pa_id='.$pa_id.'" class="an_own">View/Approve</a>';
			}else{
				$paginationHtml.='<a id="viewlink5'.$loop.'" onfocus="foc(this.id)" onblur="stnon(this.id)" href="cli_ord_req_view.php?pa_id='.$pa_id.'&sel_date='.$sel_date.'&bck_flg='.$bck_flg.'&flg='.$flg1.'" class="an_own">View/Approve</a>';
			} 
			} else {
			$paginationHtml.='Approved';
			 } 
			$paginationHtml.='</td>
			</tr>';

			$loop++;
			$st_loop++;
			 }
  }
else
{
  $paginationHtml .= '
  <tr>
    <td colspan="2" align="center">No Data Found</td>
  </tr>
  ';
}

$paginationHtml .= '
</table>
<br />
<div align="center">
  <ul class="pagination">
';

$total_links = ceil($total_data/$limit);
$previous_link = '';
$next_link = '';
$page_link = '';

//echo $total_links;

if($total_links > 4)
{
  if($page < 5)
  {
    for($count = 1; $count <= 5; $count++)
    {
      $page_array[] = $count;
    }
    $page_array[] = '...';
    $page_array[] = $total_links;
  }
  else
  {
    $end_limit = $total_links - 5;
    if($page > $end_limit)
    {
      $page_array[] = 1;
      $page_array[] = '...';
      for($count = $end_limit; $count <= $total_links; $count++)
      {
        $page_array[] = $count;
      }
    }
    else
    {
      $page_array[] = 1;
      $page_array[] = '...';
      for($count = $page - 1; $count <= $page + 1; $count++)
      {
        $page_array[] = $count;
      }
      $page_array[] = '...';
      $page_array[] = $total_links;
    }
  }
}
else
{
  for($count = 1; $count <= $total_links; $count++)
  {
    $page_array[] = $count;
  }
}

for($count = 0; $count < count($page_array); $count++)
{
  if($page == $page_array[$count])
  {
    $page_link .= '
    <li class="page-item active">
      <a class="page-link" href="#">'.$page_array[$count].' <span class="sr-only">(current)</span></a>
    </li>
    ';

    $previous_id = $page_array[$count] - 1;
    if($previous_id > 0)
    {
      $previous_link = '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$previous_id.'">Previous</a></li>';
    }
    else
    {
      $previous_link = '
      <li class="page-item disabled">
        <a class="page-link" href="#">Previous</a>
      </li>
      ';
    }
    $next_id = $page_array[$count] + 1;
    if($next_id >= $total_links)
    {
      $next_link = '
      <li class="page-item disabled">
        <a class="page-link" href="#">Next</a>
      </li>
        ';
    }
    else
    {
      $next_link = '<li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$next_id.'">Next</a></li>';
    }
  }
  else
  {
    if($page_array[$count] == '...')
    {
      $page_link .= '
      <li class="page-item disabled">
          <a class="page-link" href="#">...</a>
      </li>
      ';
    }
    else
    {
      $page_link .= '
      <li class="page-item"><a class="page-link" href="javascript:void(0)" data-page_number="'.$page_array[$count].'">'.$page_array[$count].'</a></li>
      ';
    }
  }
}

$paginationHtml .= $previous_link . $page_link . $next_link;
$paginationHtml .= '
  </ul>

</div>
';

echo $paginationHtml;

?>
