<?php
			include "../db_connection/db_connection.php";
			include "../config/config.php";
			$per_rec = '25';//$_REQUEST['per_rec'];
			if($per_rec!=''){
				$perPage = '25';//$_REQUEST['per_rec'];
			} else {
				$perPage = 25;
			}
			if (isset($_REQUEST['page'])) {
				$page  = $_REQUEST['page'];
			} else {
				$page=1;
			}
			$page=1;
			$startFrom = ($page-1) * $perPage;

			if($page==1){
				$st_loop =1;
			} else {
				$st_loop =$startFrom+1;
			}

			$pcode = $_REQUEST['pcode'];
			$cmpname = $_REQUEST['cmpname'];
			$req_date = $_REQUEST['req_date'];
			$req_designs = $_REQUEST['req_designs'];
			$req_dies = $_REQUEST['req_dies'];
			$ord_designs = $_REQUEST['ord_designs'];
			$ord_dies = $_REQUEST['ord_dies'];
			$reqdate_where='';
			if($req_date!=''){
				$reqdate_where .=' where sel_date="'.$req_date.'"';
			}

			$pcode_where='';
			if($pcode!=''){
				$pcode_where .=' and party_code="'.$pcode.'"';
			}

			$cmp_where='';
			if($pcode!=''){
				$cmp_where .=' and cmp_name like "%'.$cmpname.'%"';
			}

			$paginationHtml = '';
			$page_id= base64_encode(base64_encode(base64_encode(2))); //THIS IS FOR PAGE IDENTIFICATION IN BL FILE for active inactive
			$bck_flg = base64_encode(base64_encode(base64_encode(1)));
			$flg1 = base64_encode(base64_encode(base64_encode(1)));

			$SLCT = "SELECT party_det_id,sel_date FROM design_sel_det ".$reqdate_where." GROUP BY party_det_id,sel_date ORDER BY design_sel_det_id DESC LIMIT $startFrom, $perPage";
			$QURY = mysqli_query($connect,$SLCT);
			$loop=$startFrom;

			if(mysqli_num_rows($QURY)>0)
			{
			while($FTCH = mysqli_fetch_object($QURY))
			{
				$pa_id	=	base64_encode(base64_encode(base64_encode($FTCH->party_det_id)));
				$sel_date = base64_encode(base64_encode(base64_encode($FTCH->sel_date)));
				$search3='';
				$s1=0;
				if($req_designs!=''){
					$search3 = " HAVING sel_cnt = '".$req_designs."'";
					$s1=1;
				}

				$search4='';
				if($req_dies!=''){
					if($s1==0){
						$search4 = " HAVING mas_pic = '".$req_dies."'";
					} else {
						$search4 = " and mas_pic = '".$req_dies."'";
					}
				}

				$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 = '$FTCH->party_det_id' and `sel_date` ='$FTCH->sel_date' AND design_det.design_det_id = design_sel_det.design_det_id GROUP BY sel_date ".$search3." ".$search4."";

				$QURY_dd = mysqli_query($connect,$SLCT_dd);
				$FTCH_dd_rows = mysqli_num_rows($QURY_dd);
				$FTCH_dd = mysqli_fetch_object($QURY_dd);
				$pcode_where='';
				if($pcode!=''){
					$pcode_where .=' and party_code="'.$pcode.'"';
				}

				$cmp_where='';
				if($cmpname!=''){
					$cmp_where .=' and cmp_name like "%'.$cmpname.'%"';
				}

				$SLCT_E = "SELECT cmp_name,party_code FROM party_det WHERE party_det_id = '$FTCH->party_det_id' ".$pcode_where." ".$cmp_where." "; 
				//$paginationHtml.=$SLCT_E;
				$QURY_E = mysqli_query($connect,$SLCT_E);
				$FTCH_E = mysqli_fetch_object($QURY_E);
				$REST_E = mysqli_num_rows($QURY_E);

				$search5='';
				$s2=0;
				if($ord_designs!=''){
					$search5 = " HAVING des_cnt = '".$ord_designs."'";
					$s2=1;
				}

				$search6='';
				if($ord_dies!=''){
					if($s2==0){
						$search6 = " HAVING mas_pic = '".$ord_dies."'";
					} else {
						$search6 = " and mas_pic = '".$ord_dies."'";
					}
				}

				$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 = '$FTCH->party_det_id' AND design_det.design_det_id = design_sel_det.design_det_id AND ord_status = 'yes' and `sel_date` ='$FTCH->sel_date' ".$search5." ".$search6."";

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

				$SLCTre = "SELECT party_det_id,sel_date FROM design_sel_det WHERE party_det_id = '$FTCH->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 = '$FTCH->party_det_id' AND sel_date = '$FTCH->sel_date'";
				$QURY_DES = mysqli_query($connect,$SLCT_DES);
				$FTCH_DES = mysqli_fetch_object($QURY_DES);

				if($loop%2 != "0")
					$bg = "#f2f2f2";
				else
					$bg = "#fff";

			if($REST_E>0 && $FTCH_dd_rows>0 && $FTCH_Ec_rows>0){
			$paginationHtml.='<tr style="background-color:'.$bg.'">';
			$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("-","/",$FTCH->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++;
			}
			 }
			 }
			 
	/*$SLCT_page = "SELECT party_det_id,sel_date FROM design_sel_det ".$reqdate_where." GROUP BY party_det_id,sel_date ORDER BY design_sel_det_id DESC";
			 
			 $QURY_page = mysqli_query($connect,$SLCT_page);
			 $page_loop=1;
			 if(mysqli_num_rows($QURY_page)>0)
			{
			while($FTCH_page = mysqli_fetch_object($QURY_page))
			{
				$search3='';
				$s1=0;
				if($req_designs!=''){
					$search3 = " HAVING sel_cnt = '".$req_designs."'";
					$s1=1;
				}

				$search4='';
				if($req_dies!=''){
					if($s1==0){
						$search4 = " HAVING mas_pic = '".$req_dies."'";
					} else {
						$search4 = " and mas_pic = '".$req_dies."'";
					}
				}

				$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 = '$FTCH_page->party_det_id' and `sel_date` ='$FTCH_page->sel_date' AND design_det.design_det_id = design_sel_det.design_det_id GROUP BY sel_date ".$search3." ".$search4."";

				$QURY_dd = mysqli_query($connect,$SLCT_dd);
				$FTCH_dd_rows = mysqli_num_rows($QURY_dd);
				$FTCH_dd = mysqli_fetch_object($QURY_dd);
				$pcode_where='';
				if($pcode!=''){
					$pcode_where .=' and party_code="'.$pcode.'"';
				}

				$cmp_where='';
				if($cmpname!=''){
					$cmp_where .=' and cmp_name like "%'.$cmpname.'%"';
				}

				$SLCT_E = "SELECT cmp_name,party_code FROM party_det WHERE party_det_id = '$FTCH_page->party_det_id' ".$pcode_where." ".$cmp_where." "; 
				$QURY_E = mysqli_query($connect,$SLCT_E);
				$FTCH_E = mysqli_fetch_object($QURY_E);
				$REST_E = mysqli_num_rows($QURY_E);
				
				
				$search5='';
				$s2=0;
				if($ord_designs!=''){
					$search5 = " HAVING des_cnt = '".$ord_designs."'";
					$s2=1;
				}

				$search6='';
				if($ord_dies!=''){
					if($s2==0){
						$search6 = " HAVING mas_pic = '".$ord_dies."'";
					} else {
						$search6 = " and mas_pic = '".$ord_dies."'";
					}
				}

				$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 = '$FTCH_page->party_det_id' AND design_det.design_det_id = design_sel_det.design_det_id AND ord_status = 'yes' and `sel_date` ='$FTCH_page->sel_date' ".$search5." ".$search6."";

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


			if($REST_E>0 && $FTCH_dd_rows>0 && $FTCH_Ec_rows>0){
				$page_loop++;
			}
			 }
			 }*/

			$paging='';
			$total_pages=  '10';//$page_loop;
			$num_results_on_page = $per_rec;

			$page = 1;//isset($_REQUEST['page']) && is_numeric($_REQUEST['page']) ? $_REQUEST['page'] : 1; 
			 $paging .='<ul class="pagination" style="float:right;">';
				 if ($page > 1): 
			 $paging .='<li class="prev"><a href="cli_ord_req_list.php?page='.($page-1).'">Prev</a></li>';
				 endif;  

				 if ($page > 3): 
				$paging .='<li class="start"><a href="cli_ord_req_list.php?page=1">1</a></li>
				<li class="dots">...</li>';
				  endif;  
				  if ($page-2 > 0):  
				  $paging .='<li class="page"><a href="cli_ord_req_list.php?page='.($page-2).'">'.($page-2).'</a></li>';
				  endif;
				 if ($page-1 > 0):  
				 $paging .='<li class="page"><a href="cli_ord_req_list.php?page='.($page-1).'">'.($page-1).'</a></li>';
				 endif;

				$paging .='<li class="currentpage"><a href="cli_ord_req_list.php?page='.$page.'">'.$page.'</a></li>';

				 if ($page+1 < ceil($total_pages / $num_results_on_page)+1):  
				 $paging .='<li class="page"><a href="cli_ord_req_list.php?page='.($page+1).'">'.($page+1).'</a></li>';
				 endif;
				 if ($page+2 < ceil($total_pages / $num_results_on_page)+1): 
				 $paging .='<li class="page"><a href="cli_ord_req_list.php?page='.($page+2).'">'.($page+2).'</a></li>';
				 endif;

				if ($page < ceil($total_pages / $num_results_on_page)-2):  
				$paging .='<li class="dots">...</li>
				<li class="end"><a href="cli_ord_req_list.php?page='.ceil($total_pages / $num_results_on_page).'">'.ceil($total_pages / $num_results_on_page).'</a></li>';
				 endif; 
				if ($page < ceil($total_pages / $num_results_on_page)): 
				$paging .='<li class="next"><a href="cli_ord_req_list.php?page='.($page+1).'">Next</a></li>';
				  endif;  
			$paging .='</ul>';

echo $paginationHtml."***".$paging;
?>