<div class="content-wrapper" >
 <!-- Content Header (Page header) -->
 <section class="content-header">
  <h1>
     <?= $page_title ?>
      <small><?= $page_desc ?></small>
  </h1>
  <ol class="breadcrumb">
   <li><a href="<?= base_url() ?>"><i class="fa fa-star-o" aria-hidden="true"></i>Home</a></li>
   <li><?= $menu ?></li>
   <li class="active"><?= $sub_menu ?></li>
 </ol>
</section>
<!-- Main content -->
<section class="content">
  <div class="row">
    <div class="col-md-12">
        <?php if($this->session->flashdata('message')) { 
            $flashdata = $this->session->flashdata('message'); ?>
            <div class="alert alert-<?= $flashdata['class'] ?>">
               <button class="close" data-dismiss="alert" type="button">×</button>
               <?= $flashdata['message'] ?>
            </div>
        <?php } ?>
    </div>
    <div class="col-xs-12">
     <div class="box box-warning">
      <div class="box-header">
       <h3 class="box-title">Search Conditions</h3>
      </div>
      <div class="box-body">
       <form id="where_cond">
        <div class="col-md-12">
         <?php if($this->session->userdata['user_type'] == 1){ ?>
           <div class="col-md-3">
             <div class="form-group ">
               <label>Select Company</label>
               <select name="company_id" class="form-control" data-parsley-trigger="change">
                 <option selected disabled>Select Company</option>
                 <?php 
                 if(!empty($company_data)){
                   foreach ($company_data as $company) {
                     echo '<option value="'.$company->company_id.'">'.$company->company_name.'</option>';
                   } 
                 }
                 ?>
               </select> 
             </div> 
           </div>
         <?php }else{ ?>
           <div class="col-md-3">
             <div class="form-group ">
               <label>Select Broker</label>
               <select name="broker_id" class="form-control" data-parsley-trigger="change">
                 <option selected disabled>Select Broker</option>
                 <?php 
                 if(!empty($broker_data)){
                   foreach ($broker_data as $broker) {
                     echo '<option value="'.$broker->broker_id.'">'.$broker->broker_name.'</option>';
                   } 
                 }
                 ?>
               </select> 
             </div> 
           </div>
           <input type="hidden" name="company_id" value="<?= $this->session->userdata['id'] ?>">
         <?php } ?>
         <div class="col-md-3">
           <div class="form-group ">
             <label>Start Date</label>
             <div class="input-group date" data-provide="datepicker">
               <input id="datepicker" type="text" class="form-control" data-parsley-trigger="change" data-parsley-minlength="2" name="start_date" placeholder="Pick Appointment Date" autocomplete="off">
               <div class="input-group-addon">
                 <i class="fa fa-calendar"></i>
               </div>
             </div>
           </div>
         </div>
         <div class="col-md-3">
           <div class="form-group ">
             <label>End Date</label>
             <div class="input-group date" data-provide="datepicker">
               <input id="datepicker" type="text" class="form-control" data-parsley-trigger="change" data-parsley-minlength="2" name="end_date" placeholder="Pick Appointment Date" autocomplete="off">
               <div class="input-group-addon">
                 <i class="fa fa-calendar"></i>
               </div>
             </div>
           </div>
         </div>
         <div class="col-md-3">
           <div class="form-group ">
             <label>Select Ride Status</label>
             <select name="status" class="form-control" data-parsley-trigger="change">
               <option selected disabled>Select Ride Status</option>
               <?php 
               $status = array('0'=>'Inactive','1'=>'Yet to assign Driver','2'=>'Deleted',
                 '3'=>'Waiting For Driver Approval','4'=>'Driver Accepted','5'=>'Processing',
                 '6'=>'Ride Completed','7'=>'Driver Canceled','8'=>'Driver Manually Assigned');
               foreach ($status as $key => $stat) {
                 echo '<option value="'.$key.'">'.$stat.'</option>';
               }
               ?>
             </select> 
           </div> 
          </div>
         </div>
       </form>
       <div class="with-border" style="padding-top:70px;">
         <h4 class="box-title" >Fields to View In Report</h4>
       </div>
       <div class="col-md-12" style="padding-top:20px;">
         <div class="col-md-3">
           <input name="fieldType" type="radio" checked="checked" style="padding-left: 10px;" action="hide"
           value="all">
           <label style="padding-left:15px;">All Fields</label> 
         </div>
         <div class="col-md-3">
          <input name="fieldType" type="radio" onclick="checkChild($(this));" action="show" value="custom">
          <label style="padding-left:15px;">Custom Fields</label> 
         </div>
       </div>
       <div class="col-md-12">
        <form id="field_list">
         <table class="table table-striped hide" id="customFields">
           <tbody>
            <?php
            $html = '';
            $count = 0;
            $rowFlg = 0;
            $tables = array('patient_details' => 
                            array('name' => 'Patient Details',
                                  'fields' => 
                                   array('TDLS.transport_id AS Order_ID'=>'Order ID',
                                         'TDLS.medical_no AS Medical_Number'=>'Medical Number',
                                         'TDLS.patient_name AS Patient_Name'=>'Patient Name',
                                         'TDLS.age AS Patient_Age'=>'Patient Age',
                                         'TDLS.phone AS Patient_Phone'=>'Patient Phone',
                                         'APRES.reason_id AS Appointment_Reason_ID'=>'Appointment Reason ID',
                                         'APRES.reason AS Appointment_Reason'=>'Appointment Reason',
                                         'TDLS.appointment_time AS Appointment_Time'=>'Appointment Time',
                                         'TDLS.status AS Order_Status'=>'Order Status'
                                   )
                            ),
                            'drivers' => 
                            array('name' => 'Driver Details',
                                  'fields' => 
                                   array('DRV.driver_id AS Driver_ID'=>'Driver ID',
                                         'DRV.first_name AS Driver_First_Name'=>'Driver First Name',
                                         'DRV.last_name AS Driver_Last_Name'=>'Driver Last Name',
                                         'DRV.email_id AS Driver_Email_ID'=>'Driver Email ID',
                                         'VEH.vehicle_id AS Vehicle_ID'=>'Vehicle ID',
                                         'VEH.vehicle_type AS Vehicle_Type'=>'Vehicle Type'
                                 )
                            ),
                            'company' => 
                            array('name' => 'Company Details',
                                  'fields' => 
                                   array('CMP.company_id AS Company_ID'=>'Company ID',
                                         'CMP.company_name AS Company_Name'=>'Company Name',
                                         'CMP.address AS Company_Address'=>'Company Address',
                                         'CMP.phone AS Company_Phone'=>'Company Phone',
                                         'CMP.fax AS Company_Fax'=>'Company Fax',
                                         'CMP.email_id AS Company_Email_ID'=>'Company Email ID',
                                         'CMP.company_contact AS Company_Contact'=>'Company Contact',
                                         'CMP.company_info AS Company_Contact_Person'=>'Company Contact Person',
                                         'CMP.company_federal_id AS Company_Federal_ID'=>'Company Federal ID'
                                   )
                            ),
                            'brokers' => 
                            array('name' => 'Broker Details',
                                  'fields' => 
                                   array('BRKR.broker_id AS Broker_ID'=>'Broker ID',
                                         'BRKR.broker_name AS Broker_Name'=>'Broker Name',
                                         'BRKR.broker_email AS Broker_Email_ID'=>'Broker Email ID',
                                         'BRKR.broker_phone AS Broker_Phone'=>'Broker Phone'
                                   )
                            ),
                            'company_payment_details' => 
                            array('name' => 'Payment Details',
                                  'fields' => 
                                   array('CPAY.booking_charge AS Charge_for_Booking'=>'Charge for Booking',
                                         'CPAY.sms_charge AS Amount_Per_SMS'=>'Amount Per SMS',
                                         'CPAY.push_charge AS Amount_Per_Notification'=>'Amount Per Notification',
                                         'CPAY.report_charge AS Amount_Per_Report'=>'Amount Per Report',
                                         'CPAY.booking_count AS Total_No_of_Booking'=>'Total No. of Booking',
                                         'CPAY.sms_count AS Total_No_of_SMS_Send'=>'Total No. of SMS Send',
                                         'CPAY.push_count AS Push_Notification_Count'=>'Push Notification Count',
                                         'CPAY.report_count AS Report_Generated_Count'=>'Report Generated Count',
                                         'CPAY.paid_amount AS Total_Amount_Paid'=>'Total Amount Paid',
                                         'CPAY.outstanding_amount AS Amount_Outstanding'=>'Amount Outstanding',
                                         'CPAY.total_earnings AS Total_Earnings'=>'Total Earnings',
                                         'CPAY.last_paid_date AS Last_Payment_Date'=>'Last Payment Date'
                                   )
                            )

            );
            $baseHtml = '<tr>{:baseHtml}</tr>';
            $ottrHtml = '<td><div class="box-body" style="padding-left: 100px">
                          <label>
                          <input type="checkbox" id="table_{:table}" table="{:table}"  onclick="checkChild($(this));" >
                          {:table_name}
                          </label> 
                          {:innerHtml}
                         </div></td>';
            $innerHtml = '<div class="form-group"><div class="col-sm-10"><div class="checkbox">
                            <label>
                            <input type="checkbox" name="{:field_alias}" id="table_{:table}_{:field_alias}" value="{:field_alias}">
                            {:field_name}
                            </label>
                          </div></div></div>';
            $htmlArr = array();
            $allFields = array();
            foreach($tables AS $table => $tableDetails){
              $fieldsHtml = '';
              foreach ($tableDetails['fields'] as $alias => $field) {
                $allFields[] = $alias;
                $fieldsHtml .= str_replace(array('{:field_alias}','{:field_name}'), 
                 array($alias,$field), $innerHtml);
              }
              $fieldsHtml = str_replace('{:table}',$table,$fieldsHtml);
              $html .= str_replace(array('{:table}','{:table_name}','{:innerHtml}'), 
               array($table,$tableDetails['name'],$fieldsHtml,), $ottrHtml);

              if($count == 2){
                $rowFlg = 1; $count = 0;
                $htmlArr[] = str_replace('{:baseHtml}',$html,$baseHtml);
                $html = '';
              }else{
                $count+=1;
              }
            }
            if($rowFlg == 0){
              $html = str_replace('{:baseHtml}',$html,$baseHtml);
            }else{
              $lstRw = str_replace('{:baseHtml}',$html,$baseHtml);
              $html = implode('', $htmlArr);
              $html .= $lstRw;
            }
            echo $html;
            ?>
           </tbody>
         </table>
        </form>
       </div>
       <div class="col-md-12" style="padding-top:20px;">          
         <div class="box-footer" style="padding-left:40%;">
           <input type="hidden" id="all_fields" all_fields="<?= implode(',',$allFields) ?>">
           <button id="rGenerate" type="submit" action="view" dmclick="0" class="btn btn-primary">
             View Report
           </button>
          <button id="rGenerate" type="submit" action="export" dmclick="0" class="btn btn-info">
             Export Report
          </button>
         </div>        
       </div>
      </div>
     </div>
   </div>
  <div class="col-md-12">
    <div class="box box-warning hide" id="report_table_html">
      <div class="box-body">
       <div class="col-md-12">
         <table id="report_table" class="table table-bordered table-striped datatable">
          <thead>
            <tr>
              <th width="60px;">Order ID</th>
              <th width="100px;">Medical Number</th> 
              <th width="100px;">Patient Name</th> 
              <th width="100px;">Patient Age</th> 
              <th width="100px;">Patient Phone</th>
              <th width="100px;">Reason</th>
              <th width="100px;">Time</th>
              <th width="50px;">Action</th>
            </tr>
          </thead> 
          <tbody id="report_table_body">
              <!-- REPORT TABLE BODY -->
          </tbody>
         </table>
       </div>
     </div>
    </div>
  </div>
 </section>
</div>

<script type="text/javascript">
  jQuery(document).ready(function(){
    jQuery('[name^="TDLS.transport_id"],[name^="TDLS.medical_no"],[name^="TDLS.patient_name"],[name^="TDLS.age"],[name^="TDLS.phone"],[name^="APRES.reason"],[name^="TDLS.appointment_time"]').prop("checked", true).parent().closest('div').addClass('disable-block');
  });
</script>