<?php
  $headerArr = array("Medicaid_Number","Members_Last_Name","Members_First_Name","Members_Date_of_Birth",
    "Members_Age","Members_Phone_Number","Members_Alt_Phone","Trip_Number","Appointment_Date","Trip_Type",
    "Appointment_Day_of_Week","Appointment_Time","Trip_Reason_Code","Trip_Status","Vehicle_Type",
    "Wheelchair_Flag","Crutches_/_Walker_/_Cane_Flag","Number_of_Car_Seats_Required","Pregnant_Flag",
    "Number_of_Additional_Passengers","Additional_Passengers_With_Appointments","Trip_Mileage","Trip_Cost",
    "Pickup_Address","Pickup_City","Pickup_State","Pickup_Zip_Code","Delivery_Name","Delivery_Address",
    "Delivery_City","Delivery_State","Delivery_Zip_Code","Delivery_Phone_Number","Special_Needs",
    "Inst_/_Directions","Return_Time","Attendant_Flag","Trip_Bid_Status","Trip_Status_Date","Copay",
    "Date_Trip_Bid_Status_Was_Changed","Confirmation_Number");
?>
<div class="content-wrapper" >
  <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>User</li>
     <li class="active">View User</li>
   </ol>
  </section>
  <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-md-12">
      <div class="box box-warning">
       <div class="box-header with-border">
         <h3 class="box-title">Ride Management</h3>
       </div>
       <div class="box-body">
          <form role="form" id="getRideDataForm" action="<?=base_url('Ride/view_rides')?>" method="post" 
                class="validate" data-parsley-validate="" enctype="multipart/form-data">
            <?php if($this->session->userdata['user_type'] != 1){ ?>
              <div class="col-md-12">
                <div class="form-group">
                 <label>Choose Booking Source</label>
                 <select name="broker_id" class="form-control" onchange="getRideData()">
                   <option selected value="">All Rides</option>
                   <?php 
                     if(!empty($broker_data)){
                       foreach ($broker_data as $broker) {
                         $chkFlg = ($broker_id == $broker->broker_id)?'selected':'';
                         echo '<option value="'.$broker->broker_id.'" '.$chkFlg.'>
                                '.$broker->broker_name.
                              '</option>';
                       } 
                     }
                   ?>
                   <option value="0" <?= ($broker_id != '' && $broker_id == 0)?'selected':'' ?>>
                      Phone Bookings
                   </option>
                 </select> 
               </div>
              </div>
            <?php } else { ?>
              <div class="col-md-5">
                <div class="form-group">
                 <label>Select Company</label>
                 <select id="searchCompanyDrop" name="company_id" class="form-control" 
                         onchange="getBrokers(1,1)">
                   <option selected value="">All Company</option>
                   <?php 
                     if(!empty($company_data)){
                       foreach ($company_data as $company) {
                         $chkFlg = ($company_id == $company->company_id)?'selected':'';
                         echo '<option value="'.$company->company_id.'" '.$chkFlg.'>
                                '.$company->company_name.
                              '</option>';
                       } 
                     }
                   ?>
                 </select> 
               </div>
              </div>
              <div id="searchBrokerBlock" class="col-md-5 <?= (empty($company_id))?'disable-div':'' ?>">
                <div class="form-group">
                 <label>Choose Booking Source</label>
                 <select id="searchBrokerDrop" name="broker_id" class="form-control">
                   <option selected value="">All Brokers</option>
                   <?php 
                     if(!empty($broker_data)){
                       foreach ($broker_data as $broker) {
                         $chkFlg = ($broker_id == $broker->broker_id)?'selected':'';
                         echo '<option value="'.$broker->broker_id.'" '.$chkFlg.'>
                                '.$broker->broker_name.
                              '</option>';
                       } 
                     }
                   ?>
                   <option value="0" <?= ($broker_id != '' && $broker_id == 0)?'selected':'' ?>>Phone Bookings</option>
                 </select> 
               </div>
              </div>
              <div class="col-md-2">
                <a onclick="getRideData()" class="btn btn-sm btn-primary" style="margin-top:27px;">Search</a>
              </div>
            <?php } ?>
          </form>
        </div>
      </div>
     </div>

      <div class="col-xs-12" id="selectDriverBlock">
        <div class="box">
          <?php if($this->session->userdata['user_type'] != 1 && !empty($drivers_data)){ ?>
            <div class="box-header with-border">
              <div class="col-md-6"><h3 class="box-title">Available Drivers</h3></div>
            </div>
            <div class="box-body driver-list-body">
              <div class="padLeft20">
                <li id="driverRequired" class="parsley-required padBottom10" style="display: none;">
                  Select <b>Atlest one Driver</b> to continue.
                </li>
                <input id="selectAllDriver" type="checkbox" class="Cards">
                <label class="select-all-input">Select All</label>

                <div class="enable-nodriver-blk">
                  <input id="checkNoDriver" type="checkbox" name="checkNoDriver" value="1">
                  <label class="enable-no-driver">Enable No Driver</label>
                </div>
              </div>
              <div class="col-xs-12 driver-list-blk">
                <?php foreach($drivers_data AS $driver){ ?>
                  <div class="col-xs-3">
                    <div class="col-xs-2">
                      <input type="checkbox" id="avlDriver_<?= $driver->driver_id ?>" class="Cards" 
                             name="avlDrivers[]" value="<?= $driver->driver_id ?>">
                    </div>
                    <div class="col-xs-10">
                      <label><?= $driver->first_name.' '.$driver->last_name ?></label>
                    </div>
                  </div>
                <?php } ?>
              </div>
            </div>
          <?php } ?>
          <div class="box-header with-border">
            <div class="col-md-6"><h3 class="box-title">All Rides</h3></div>
          </div>
          <div class="box-body">
            <table class="table table-bordered table-striped datatable ">
              <thead>
                <tr>
                  <th class="hidden">ID</th>
                  <th width="100px"></th>
                  <?php
                    foreach ($headerArr AS $head) {
                      echo '<th width="100%;">'.str_replace('_',' ',$head).'</th> ';
                    }
                  ?>
                  <th width="150px;">Data Source</th>
                </tr>
              </thead> 
              <tbody>
              <?php 
                $ride_ids = array();
                $report_data = array();
                if(!empty($ride_data)){
                  foreach($ride_data as $ride) {
                    $ride_ids[] = $ride->transport_id;
                    $json_ride = json_decode($ride->data,true);
                    if(empty($json_ride)){
                      continue; 
                    }
                    ?>
                      <tr>
                        <th class="hidden"><?= $ride->transport_id ?></th>
                        <th class="center">
                          <?php if($this->session->userdata['user_type'] != 1 && !empty($drivers_data)){ 
                            $style = 'style="color:red;";';
                            $ride->is_scheduled = (isset($ride->is_scheduled))?$ride->is_scheduled:0;
                            if($ride->is_scheduled == 1){
                              $style = 'style="color:green;";';
                            } ?>
                            <a id="markSchedule_<?= $ride->transport_id ?>" transport_id="<?= $ride->transport_id ?>"
                               is_scheduled="<?= $ride->is_scheduled ?>" class="cpoint">
                              <i class="fa fa-fw fa-check" <?=$style?> ></i>
                            </a>
                          <?php } ?>
                          <a id="viewRideDetails" key="<?= $ride->transport_id ?>" class="cpoint">
                            <i class="fa fa-fw fa-eye"></i>
                          </a>
                          <a href="<?= base_url("Ride/changeStatus/".encode_param($ride->transport_id))."/2" ?>" 
                            onClick="return doconfirm()"><i class="fa fa-fw fa-trash" style="color:#dd4b39;"></i></a>
                        </th>

                        <?php 
                          $json_ride = array_merge(array_flip($headerArr),$json_ride);
                          foreach($json_ride AS $key => $data){
                            if(!in_array($key,$headerArr)){
                              continue;
                            }
                            switch ($key){
                              case 'Wheelchair_Flag': $json_ride[$key] = ($data == 1)?'YES':'NO'; break;
                              case 'Attendant_Flag': $json_ride[$key] = ($data == 1)?'YES':'NO'; break;
                              case 'Pregnant_Flag': $json_ride[$key] = ($data == 1)?'YES':'NO'; break;
                              case 'Trip_Reason_Code': $json_ride[$key] = $ride->reason; break;
                              case 'Vehicle_Type': $json_ride[$key] = $ride->vehicle_type; break;
                              case 'Trip_Type': $json_ride[$key] = $ride->trip_type; break;
                              case 'Trip_Status': $json_ride[$key] = $ride->trip_status; break;
                              case 'Members_Date_of_Birth':$json_ride[$key] = ($data == 1)?'YES':'NO';break; 
                              case 'Crutches_/_Walker_/_Cane_Flag':$json_ride[$key]=($data==1)?'YES':'NO'; 
                                    break;
                            }
                            echo '<td class="center">'.$data.'</td>';
                            $report_data[$ride->transport_id] = $json_ride; 
                          } ?>
                        <th class="center"><?= (!empty($ride->broker_name)?$ride->broker_name:'Phone Booking') ?></th>
                      </tr>
                  <?php }
                }?>
              </tbody>
            </table>
            <?php if($this->session->userdata['user_type'] != 1){ 
              $customAttr = 'id="scheduleAll"';
              if(empty($drivers_data)){
                $customAttr = 'onclick="modalTrigger(\'Schedulle Error\',\'There is no active driver exists, Activate a driver and try again...!\')"';
              } ?>
              <div class="col-md-1" style="float:right;padding:initial;">
                <a <?= $customAttr ?> class="btn btn-sm btn-primary">Schedule All</a>
              </div>
            <?php } ?>
          </div>
        </div>
      </div>
    </div>
  </section>
</div>
<script type="text/javascript">
   ride_ids = <?= json_encode($ride_ids); ?>;
   report_data = <?= json_encode($report_data); ?>;
</script>
