 <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1 class="m-0 text-dark"><?php echo $title ?></h1>
          </div><!-- /.col -->
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="<?php echo base_url() ?>admin/admin/dashboard">Dashboard</a></li>
              <li class="breadcrumb-item active"><a href="#"><?php echo ucwords(str_replace("_", " ", $this->uri->segment(2))); ?></a></li>
             

            </ol>
          </div><!-- /.col -->
        </div><!-- /.row -->
      </div><!-- /.container-fluid -->
    </div>
    <!-- /.content-header -->
  <?php
    $errors = $this->session->userdata('errors');
    if (isset($errors)) {
        ?>
        <script>
            swal ( "Oops" ,  "<?php echo $errors ?>" ,  "error" )
        </script>
        <?php echo $errors ?>
        <?php
        $this->session->unset_userdata('errors');
    }
    $error = $this->session->userdata('error');
    if (isset($error)) {
        ?>
         <script>
            swal ( "Oops" ,  "<?php echo $error ?>" ,  "error" )
        </script>
        <?php
        $this->session->unset_userdata('error');
    }
    $success = $this->session->userdata('success');
    if (isset($success)) {
        ?>
         <script>
            swal({
          title: "Done!",
          text: "<?php echo $success ?>",
          icon: "success",
        });
        </script>
        <?php
        $this->session->unset_userdata('success');
    }
    ?>
    <!-- Main content -->
    
    <section class="content">
      <div class="container-fluid">
        <!-- Info boxes -->

          <div class="row">
            <div class="col-lg-3 col-6">
              <div class="small-box bg-warning">
                <div class="inner" style="color: #fff!important;">
                  <h3><?php echo $this->user_model->get_all_support_ticket_by_role_id_num_rows(0); ?></h3>
                  <p>Open Ticket</p>
                </div>
              </div>
            </div>
            <div class="col-lg-3 col-6">
              <div class="small-box bg-info">
                <div class="inner">
                  <h3><?php echo $this->user_model->get_all_support_ticket_by_role_id_num_rows(1); ?></h3>
                  <p>In Process Ticket</p>
                </div>
              </div>
            </div>
            <div class="col-lg-3 col-6">
              <div class="small-box bg-success">
                <div class="inner">
                  <h3><?php echo $this->user_model->get_all_support_ticket_by_role_id_num_rows(2); ?></h3>
                  <p>Close Ticket</p>
                </div>
              </div>
            </div>
            <div class="col-lg-3 col-6">
              <div class="small-box bg-danger">
                <div class="inner">
                  <h3><?php echo $this->user_model->get_all_support_ticket_by_role_id_num_rows(3); ?></h3>
                  <p>Rejected Ticket</p>
                </div>
              </div>
            </div>
          </div>
       

        <?php if(user()->role == 'User'):?>
          <div class="card card-default">
            <div class="card-header">
              <h3 class="card-title">All Ticket History</h3>
              <div class="card-tools">
                <a href="#" data-toggle="modal" data-target="#create-ticket"><button type="button" class="btn btn-info">Create Ticket</button></a>
              </div>
            </div>
              <!-- /.card-header -->
              <div class="card-body">
                <table id="example3" class="table table-bordered table-striped">
                  <thead>
                  <tr>
                    <th>id</th>
                    <th>Ticket ID</th>
                    <th>Submit Date</th>
                    <th>Department</th>
                    <th>Agent Name</th>
                    <th>Subject</th>
                    <th>Status</th>
                    <th></th>
                  </tr>
                  </thead>
                  <tbody>
                    <?php $i=1; foreach ($all_support_ticket as $key => $ticket):
                  $role = $this->user_model->get_role_by_id($ticket->department);
                  $user_d = $this->user_model->get_user_by_id($ticket->transfer);?>
                  <tr>
                  
                    <td><?php echo $i ?></td>
                    <td><?php echo $ticket->ticket_id ?></td>
                    <td><?php echo $ticket->date ?></td>
                    <td><?php echo $role->role_name ?></td>
                    <td><?php if($user_d->name){echo $user_d->name;}else{echo '<span class="badge badge-danger">Agent Not Assigned</span>';}?></td>
                    <td><?php echo $ticket->subject ?></td>
                    <td>
                      <?php if($ticket->status == 0): ?>
                        <span class="badge badge-warning">Open</span>
                      <?php elseif ($ticket->status == 1):?>
                        <span class="badge badge-primary">In-Process</span>
                      <?php elseif ($ticket->status == 2):?>
                        <span class="badge badge-success">Close</span>
                      <?php elseif ($ticket->status == 3):?>
                        <span class="badge badge-danger">Reject</span>
                      <?php endif; ?>
                    </td>
                    <td><a href="<?php echo base_url() ?>admin/support_ticket/view_details/<?php echo $ticket->rand_id ?>">View Details</a></td>
                  </tr>
                  <?php $i++; ?>
                  <?php endforeach; ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        <?php endif; ?>
        <?php if(user()->role == 'Admin'):?>
        <div class="card card-default">
          <div class="card-header">
            <h3 class="card-title">All User Ticket History</h3>
            <div class="card-tools">
              <div style="display: inline-block;">
                <label>Filter Data By Status : </label>
              </div>
              <div style="display: inline-block;">
                <select  class="form-control" id="ticket-user-filter">
                  <option value="open_01">Open</option>
                  <option value="reject_01">Reject</option>
                  <option value="close_01">Close</option>
                  <option value="process_01">In-Process</option>
                </select>
              </div>
            </div>
          </div>
          
            <div class="card-body">
              <table id="example7" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>id</th>
                  <th>Ticket ID</th>
                  <th>User Name</th>
                  <th>Submit Date</th>
                  <th>Department</th>
                  <th>Agent Name</th>
                  <th>Subject</th>
                  <th>Status</th>
                  <th></th>
                  <th>Action</th>
                </tr>
                </thead>
                <tbody>
                  <?php $i=1; foreach ($all_support_ticket as $key => $ticket):
                $role = $this->user_model->get_role_by_id($ticket->department);
                $user_d = $this->user_model->get_user_by_id($ticket->transfer);
                $user = $this->user_model->get_user_by_id($ticket->user_id);
                if($user->role == 'User'):
                ?>
                <tr>
                  <td><?php echo $i ?></td>
                  <td><?php echo $ticket->ticket_id ?></td>
                  <td><?php echo $user->name ?></td>
                  <td><?php echo $ticket->date ?></td>
                  <td><?php echo $role->role_name ?></td>
                  <td><?php if($user_d->name){echo $user_d->name;}else{echo '<span class="badge badge-danger">Agent Not Assigned</span>';}?></td>
                  <td><?php echo $ticket->subject ?></td>

                  <td>
                    <b style="display:none;">
                      <?php if($ticket->status == 0): ?>
                          open_01
                        <?php elseif ($ticket->status == 1):?>
                         process_01
                        <?php elseif ($ticket->status == 2):?>
                          close_01
                        <?php elseif ($ticket->status == 3):?>
                          reject_01
                        <?php endif; ?>
                    </b>
                    <select onchange="change_support_status(this, '<?php echo $ticket->id ?>')" class="form-control">
                        <option <?php if($ticket->status == 0){echo 'selected';} ?> value="0">Open</option>
                        <option <?php if($ticket->status == 1){echo 'selected';} ?> value="1">In-Process</option>
                        <option <?php if($ticket->status == 2){echo 'selected';} ?> value="2">Close</option>
                        <option <?php if($ticket->status == 3){echo 'selected';} ?> value="3">Reject</option>
                    </select>
                  </td>
                  <td><a href="<?php echo base_url() ?>admin/support_ticket/view_details/<?php echo $ticket->rand_id ?>">View Details</a></td>
                  <td>
                    <div class="input-group-prepend">
                      <button type="button" class="btn btn-warning dropdown-toggle" data-toggle="dropdown">
                        Action
                      </button>
                      <ul class="dropdown-menu">
                        <li class="dropdown-item"><a href="#" data-toggle="modal" data-target="#assigned-ticket<?php echo $ticket->id ?>">Assigned Agent</a></li>
                        <li class="dropdown-divider"></li>
                        <li class="dropdown-item"><a onclick="return confirm('Are you sure?')" href="<?php echo base_url();?>admin/support/delete_support_ticket/<?php echo $ticket->rand_id ?>">Delete</a></li>
                      </ul>
                    </div>
                  </td>
                </tr>
                <?php endif; $i++; ?>
                <?php endforeach; ?>
                </tbody>
              </table>
            </div>
          </div>
       
      
      <?php endif; ?>
    </div>
  </section>





    <!-- /.content -->
  </div>

  <!-- /.content-wrapper -->
<?php if(user()->role == 'User'):?>
  <div class="modal fade" id="create-ticket" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Create New Ticket</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <?php echo form_open_multipart('admin/support_ticket/add_new_ticket'); ?>
          <div class="form-group">
            <label for="recipient-name" class="col-form-label">Subject: <span style="color: red">*</span></label>
            <input type="text" class="form-control"  placeholder="Enter Ticket Subject" required="" name="subject">
          </div>
          <div class="form-group">
            <label for="recipient-name" class="col-form-label">Department: <span style="color: red">*</span></label>
            <select type="text" class="form-control" required="" name="department">
              <option value="">Select Department</option>
                <?php foreach ($role_for_tickey_system as $key => $role):
                  if($role->role_name != 'Super Admin' && $role->role_name != 'Admin' && $role->role_name != 'User' && $role->role_name != 'Store'): ?>
                  <option value="<?php echo $role->id ?>"><?php echo $role->role_name ?></option>
                <?php endif;?>
                <?php endforeach; ?>
            </select>
          </div>
          <div class="form-group">
            <label for="message-text" class="col-form-label">Details Description: <span style="color: red">*</span></label>
            <textarea class="form-control" rows="7" required="" placeholder="Enter Details Description" name="description"></textarea>
          </div>
          <div class="form-group">
            <label for="recipient-name" class="col-form-label">Upload Image:</label>
            <input type="file" class="form-control"  placeholder="Enter Ticket Subject" name="image">
            <input type="hidden" name="user_id" value="<?php echo user()->id ?>">
          </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Send Ticket</button>
      </div>
      <?php echo form_close() ?></form>
    </div>
  </div>
</div>
<?php endif;?>
<?php 
if(is_n_admin()):
$i=1; foreach ($all_support_ticket as $key => $ticket):
$role = $this->user_model->get_role_by_id($ticket->department);
$user_details = $this->user_model->get_user_by_role_for_suport($role->role_name);

?>

  <div class="modal fade" id="assigned-ticket<?php echo $ticket->id; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Assigned Agent</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <?php echo form_open_multipart('admin/support/assign_a_agent'); ?>
          <div class="form-group">
            <label for="recipient-name" class="col-form-label">Agents: <span style="color: red">*</span></label>
            <select type="text" class="form-control" required="" name="user_id">
              <option value="">Select Agent</option>
                <?php foreach ($user_details as $key => $user): ?>
                  <option value="<?php echo $user->id ?>"><?php echo $user->name ?></option>
                <?php endforeach; ?>
            </select>
          </div>
        <input type="hidden" name="ticket_id" value="<?php echo $ticket->id ?>">
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Send Ticket</button>
      </div>
      <?php echo form_close() ?></form>
    </div>
  </div>
</div>
<?php endforeach;
 endif;?>
 <?php if(!is_user()):?>
  <script type="text/javascript">
    function change_support_status(value, tid) 
    {
        var val = value.value;
        var id = tid;
        $.ajax({
        url:"<?php echo base_url(); ?>admin/support/change_support_status",
        method:"POST",
        data:{val:val, id:id},
          success:function(data)
          {
            if(data == 1)
            {
              alert("Status Successfully Change");
              location.reload();
            }
            else
            {
              alert("Status Change Failed");
            }
          }
      });
    }
  </script>
  <?php endif;?>
<style type="text/css">
  .dataTables_length
  {
    display: none;
  }
</style>