<?php
			include "../db_connection/db_connection.php";
			include "../config/config.php";
			$per_rec = $_REQUEST['per_rec'];
			if($per_rec!=''){
				$perPage = $_REQUEST['per_rec'];
			} else {
				$perPage = 25;
			}
			//$page = 0;
			//$st_loop =1;
			if (isset($_REQUEST['page'])) {
				$page  = $_REQUEST['page'];
			} else {
				$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'];
			/*$sqlQuery = "SELECT * FROM design_sel_det GROUP BY party_det_id,sel_date ORDER BY design_sel_det_id DESC LIMIT $startFrom, $perPage";
			$result = mysqli_query($conn, $sqlQuery);
			$paginationHtml = '';
			while ($row = mysqli_fetch_assoc($result)) {
				$paginationHtml.='<tr>';
				$paginationHtml.='<td>'.$row["party_det_id"].'</td>';
				$paginationHtml.='<td>'.$row["design_sel_det_id"].'</td>';
				$paginationHtml.='<td>'.$row["design_det_id"].'</td>'; 
				$paginationHtml.='<td>'.$row["sel_date"].'</td>';
				$paginationHtml.='<td>'.$row["statu_s"].'</td>';
				$paginationHtml.='<td>'.$row["ord_status"].'</td>';
				$paginationHtml.='</tr>';
			} 
			$jsonData = array(
				"html"	=> $paginationHtml,
			);*/
			$reqdate_where='';
			if($req_date!=''){
				$reqdate_where .=' where sel_date="'.$req_date.'"';
			}
			$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)));

				$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";
				$QURY_dd = mysqli_query($connect,$SLCT_dd);
				$FTCH_dd = mysqli_fetch_object($QURY_dd);
				$pcode_where='';
				if($pcode!=''){
					$pcode_where .=' and party_code="'.$pcode.'"';
				}

				$cmp_where='';
				if($pcode!=''){
					$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." "; 
				$QURY_E = mysqli_query($connect,$SLCT_E);
				$FTCH_E = mysqli_fetch_object($QURY_E);
				$REST_E = mysqli_num_rows($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 = '$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'";

				$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 = '$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){
			$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++;
			}
			 }
			 }

			 $jsonData = array(
				"html"	=> $paginationHtml,
				);
echo json_encode($jsonData);
?>