<?php
    require_once "../include/config.php";
    require_once GLOBAL_INCLUDE_PATH."include/startup.php";
       //require_once GLOBAL_INCLUDE_PATH."include/date_related_functions.php";

    $alltable = new AllTables();
     $allform = new allform();
     
    if(!adminchecklogin('', '', '', $G_userid))
    {
        $redirect = ADMIN_DIR."auth/login.php";
        header("location:".$redirect);
        exit();
    }

    if($G_userid > 0 && $G_userid != null && $G_userid != '')
    {   
         $csv = (isset($_POST['csv_output'])?'1':'0');
        if ($csv=='1')
        {
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment; filename=\"All_website_Form_Report.csv\"");
            
            $readerstatusresultlist = str_replace('\\"','"',$_POST['csv_output']);
            echo $readerstatusresultlist;
            exit();
        }
          /*      Start Display Date Range      */ 

		$display_today					=	date('d-M-Y',strtotime("now"));
		$display_yesterday			=	date('d-M-Y',strtotime("last days"));
		$display_this_month		=	date("M",strtotime("now"));
		$this_year 							= date("Y",strtotime("now"));
		$display_lastmonth      	= date("M",strtotime("-1 month"));
     	$lastmonthyear  				= date("Y",strtotime("-1 month"));
		$ThisQuarter = ThisQuarter();
		$ThisQuarter_and_Month =  explode('||',$ThisQuarter);
		$display_quarter_month	= $ThisQuarter_and_Month[1];
		$LastQuarter = LastQuarter();
		$LastQuarter_and_Month =  explode('||',$LastQuarter);
		$display_lastquarter_month 	= $LastQuarter_and_Month [1];
	/*      End Display Date Range         */
		
	
		$display1 = sanitize($_POST['txtfromdate'],SQL);
		$display2 = sanitize($_POST['txttodate'],SQL);
	
		$fromdate	= date("Y-m-d", strtotime($_POST['txtfromdate']));
		$todate		= date("Y-m-d", strtotime($_POST['txttodate']));
		if ($fromdate=='1970-01-01')
		{
				//$where_date_range ="date(entry_date) BETWEEN '$cur_fy_startdate' and '$cur_fy_enddate' ";	
				$day = sanitize($_POST['opt'],SQL);
			/* date Range use  1. Today  2. Yesterday  3. Last_7_days  4. Last_30_days  5. This_month  6. Last_month  7. This_quarter  8. Last_quarter 9. This_year  10. Last_year  11. All_time */
			$where_date_range = DateRenge($day,'entry_date');
		}
		elseif ($fromdate !='1970-01-01')		
		{
				$where_date_range ="date(entry_date) BETWEEN '$fromdate' and '$todate'";
		}
		else 
		{
			$day = sanitize($_POST['opt'],SQL);
			/* date Range use  1. today  2. yesterday  3. Last_7_days  4. last_30_days  5. This_month  6. Last_month  7. This_quarter  8. Last_quarter 9. This_year  10. Last_year  11. All_time */
			$where_date_range = DateRenge($day,'entry_date');
		}
		if($day == 'All_time')
		{
		    $where_condition.="$where_date_range";
		}
		else
		{
		   if($where_date_range !='')
		    {
		        $where_condition.="where $where_date_range";
		        $limit.="";
		    }
		    else
		    {
		        $where_condition.="";
		        $limit.="limit 0,100";
		    }

		}

		
       // $sql = "select * from ".ALLFORM_TABLE." where $where_date_range order by formid DESC";
           //$sql = "select * from ".ALLFORM_TABLE." $where_condition  order by formid DESC";
           $sql = "select * from ".ALLFORM_TABLE." where domainname LIKE '%".DOMAIN_NAME."%' $where_condition order by formid ASC $limit";
        $data = $allform->getallform('','', $sql);
        $count = count($data);
        $heading = "All_website_Form_Report";
          
        $record = '';
        $csvcolumnsname ='"Sr.No","Form Name","Company Name","First-Last Name","Email Id","Mobile No.","Submission Date"'."\n";

        
        if($count > 0){
            for($i=0;$i<$count;$i++){
                
                $formid 	    = $data[$i]['formid'];
                $formname       = $data[$i]['formname'];
                $domainname     = $data[$i]['domainname'];
                $company_name   = $data[$i]['company_name'];
                $receipt_name   = $data[$i]['recipientname'];
                $formname       = $data[$i]['formname'];
                $fname          = $data[$i]['fname'];
                $lname          = $data[$i]['lname'];
                $email          = $data[$i]['email'];
                $mobile         = $data[$i]['mobile'];
                $fullname = $fname." ".$lname;
                $entry_date     = date('d-m-Y h:i:s',strtotime($data[$i]['entry_date']));
                $formtype       = $data[$i]['formtype'];
                $counter       = $i+1;
               /* if($status == 1){
                    $displaystatus = "<span class='text text-success'>Active</span>";
                }else{
                    $displaystatus = "<span class='text text-danger'>Inactive</span>";
                }
                
                if($isparentpage == 1 && $parentpageid == 0){
                    $displaypagestatus = "<span class='text text-success'>Yes</span>";
                }else{
                    $displaypagestatus = "<span class='text text-danger'>No</span>";
                } */
                
                if($formtype == '1')
                {
                    $displaydetails = "<a href='".DOC_ROOT."admin/exhibitor_details.php?formid=$formid' target='_blank'> <i class='fa fa-eye'></i> View </a>";
                }
                elseif($formtype == '3')
                {
                    $displaydetails = "<a href='".DOC_ROOT."admin/visasupport_details.php?formid=$formid' target='_blank'> <i class='fa fa-eye'></i> View </a>";
                }
                elseif($formtype == '2')
                {
                    $displaydetails = "<a href='".DOC_ROOT."admin/moreinformation_details.php?formid=$formid' target='_blank'> <i class='fa fa-eye'></i> View </a>";
                }
                elseif($formtype == '4')
                {
                    $displaydetails = "<a href='".DOC_ROOT."admin/newsletter_details.php?formid=$formid' target='_blank'> <i class='fa fa-eye'></i> View </a>";
                }
                elseif($formtype == '5')
                {
                    $displaydetails = "<a href='".DOC_ROOT."admin/contact_details.php?formid=$formid' target='_blank'> <i class='fa fa-eye'></i> View </a>";
                }
                
                $record .= "<tr>";
                    $record .= "<td>".($i+1)."</td>";
                   // $record .= "<td>".$domainname."</td>";   
                    $record .= "<td>".$formname."</td>";   
                    $record .= "<td>".$company_name."</td>";   
                    $record .= "<td>".$fullname."</td>";   
                    $record .= "<td>".$email."</td>";   
                    $record .= "<td>".$mobile."</td>";   
                    $record .= "<td>".$receipt_name."</td>";
                    $record .= "<td>".$entry_date."</td>";
                    $record .= "<td>".$displaydetails."</td>";
                $record .= "</tr>";
                $reportname = '"'.$heading.'"'."\n";
                        $csvdata .= '"'.$counter.'","'.$formname.'","'.$company_name.'","'.$fullname.'","'.$email.'","'.$mobile.'","'.$entry_date.'"'."\n";
                        $readerstatusresultCSV = $reportname.$csvcolumnsname.$csvdata;
            }    
        }
        else
        {
            $record .= "<tr>";
                $record .= "<td colspan='4'>No Record Found..!</td>";
            $record .= "</tr>";
        }
        require_once "view_form_data_display1.html";    
    }
    else
    {
        $redirect = ADMIN_DIR."auth/login.php";
        header("location:".$redirect);
        exit();
    }   
?>