<?php
    /*This class contains all functions related to affiliate sign up, get affiliate list*/
  class Admin_video_manager_model extends CI_Model
  {

      //constructor
      public function __construct()
      {
          parent::__construct();
      }

      //insert a new affiliate into the system/db
      function saveAdminVideo($data)
      {

          if($data['admin_video_id'])
          {
              $this->db->where('admin_video_id', $data['admin_video_id']);
              $this->db->update('tbl_admin_video', $data);
              return $data['admin_video_id'];

          }
          else
          {
              $this->db->set('created_on', getCurrentDateTimeForDB());
              $this->db->insert('tbl_admin_video', $data);
              $video_id = $this->db->insert_id();
              //get highest sequence number for active video
              $highest_sequence_number = $this->getLastSequenceNumber();

              //set sequence number for newly added video
              $this->db->set('sequence_number', $highest_sequence_number);
              $this->db->where('admin_video_id', $video_id);
              $this->db->update('tbl_admin_video', $data);
              return $video_id;
          }
      }

      function getLastSequenceNumber()
      {
          $sql = 'SELECT IFNULL(sequence_number,1) as sequence_no FROM tbl_admin_video ORDER BY status DESC, sequence_number DESC';
          //echo $sql;
          $resource = $this->db->query($sql);
          $result = $resource->row_array();
          return $result['sequence_no']?($result['sequence_no']+1):1;

      }
      function getAdminVideoDetailsByVideoRowId($video_id)
      {
          $this->db->select('*');
          $this->db->from('tbl_admin_video');
          $this->db->where('admin_video_id',$video_id);
          $res = $this->db->get();
          $result = $res->row_array();
          return $result;
      }

      function showManageAdminVideo($admin_user_id ,$qs = '')
      {
          $cls="";
          if($qs)
          {
              $cls = " AND (title LIKE '%".$qs."%')";
          }
          $datetime_fmt = $this->config->item('show_datetime_fmt');
          $sql = 'SELECT title,
                description,
                admin_video_id,
                link,
                admin_user_id,
                CASE is_recurring
                WHEN 1 THEN \'Yes\'
                ELSE \'No\'
                END as recurring_status,
                CASE is_broadcast
                WHEN 1 THEN \'Yes\'
                ELSE \'No\'
                END as broadcast_status,
                DATE_FORMAT(created_on,\''.$datetime_fmt.'\') AS created_on,
                DATE_FORMAT(broadcast_start_date,\''.$datetime_fmt.'\') AS broadcast_start,
                DATE_FORMAT(broadcast_end_date,\''.$datetime_fmt.'\') AS broadcast_end,
                login_quantity
        FROM tbl_admin_video
        WHERE tbl_admin_video.admin_user_id = ' . $admin_user_id . ' AND status=1 AND is_broadcast!=1'.$cls. ' ORDER BY sequence_number ASC';
        //echo $sql;
        $resource = $this->db->query($sql);
        $result = $resource->result_array();
        return $result;
      }

      function showManageAdminBroadcastVideo($admin_user_id ,$qs = '')
      {
          $cls="";
          if($qs)
          {
              $cls = " AND (title LIKE '%".$qs."%')";
          }
          $datetime_fmt = $this->config->item('show_datetime_fmt');
          $sql = 'SELECT title,
                description,
                admin_video_id,
                link,
                admin_user_id,
                CASE is_recurring
                WHEN 1 THEN \'Yes\'
                ELSE \'No\'
                END as recurring_status,
                CASE is_broadcast
                WHEN 1 THEN \'Yes\'
                ELSE \'No\'
                END as broadcast_status,
                DATE_FORMAT(created_on,\''.$datetime_fmt.'\') AS created_on,
                DATE_FORMAT(broadcast_start_date,\''.$datetime_fmt.'\') AS broadcast_start,
                DATE_FORMAT(broadcast_end_date,\''.$datetime_fmt.'\') AS broadcast_end,
                login_quantity
        FROM tbl_admin_video
        WHERE tbl_admin_video.admin_user_id = ' . $admin_user_id . ' AND status=1 AND is_broadcast=1'.$cls. ' ORDER BY sequence_number ASC';
          //echo $sql;
          $resource = $this->db->query($sql);
          $result = $resource->result_array();
          return $result;

      }
      function deleteVideo($video_id)
      {
          $sql = "UPDATE tbl_admin_video SET status=0 WHERE admin_video_id=".$video_id;
          $result = $this->db->query($sql);
          if($this->db->affected_rows())
          {
              echo 1;
          }
          else
          {
              return 0;
          }
      }

      function deleteMultipleVideo($video_ids)
      {
          $sql = "UPDATE tbl_admin_video SET status=0 WHERE admin_video_id IN (".$video_ids .')';
          $result = $this->db->query($sql);
          if($this->db->affected_rows())
          {
              return 1;
          }
          else
          {
              return 0;
          }
      }

      function sortVideoOrder($video_id, $sequence_number)
      {
          $sql = "UPDATE tbl_admin_video SET sequence_number=".$sequence_number." WHERE admin_video_id=".$video_id;
          $result = $this->db->query($sql);
          if($this->db->affected_rows())
          {
              return 1;
          }
          else
          {
              return 0;
          }
      }
      function getVideoForPopup($merchant_row_id)
      {
          $merchant_watchlist_exists = $this->db->query("SELECT * FROM tbl_merchant_video_watchlist WHERE merchant_row_id=".$merchant_row_id)->row_array();
          $broadcast_video_id = $merchant_watchlist_exists['broadcast_video_id'];
          $normal_video_id = $merchant_watchlist_exists['normal_video_id'];
          if($broadcast_video_id)
          {   
              $video = $this->getAdminVideoDetailsByVideoRowId($broadcast_video_id);
              if($video && ($merchant_watchlist_exists['broadcast_video_display_count'] >= $video['login_quantity'] || !$video['status']))
              {        
                  $broadcastSql = $this->db->query("SELECT * FROM tbl_admin_video WHERE is_broadcast = 1 AND status = 1 AND sequence_number > ".$video['sequence_number']." ORDER BY sequence_number ASC LIMIT 0,1")->row_array();
                  //next in sequence exists
                  if($broadcastSql)
                      return $broadcastSql;
              }
              else
              {    
                 return $video;
              }   
          }
          else 
          {      
              $broadcastSql = $this->db->query("SELECT * FROM tbl_admin_video WHERE is_broadcast=1 AND status = 1 ORDER BY sequence_number ASC LIMIT 0,1")->row_array();
              //if broadcast exists, then load broadcast video
              if($broadcastSql)
                 return $broadcastSql;
          }
          
          if($normal_video_id)
          {
              $video = $this->getAdminVideoDetailsByVideoRowId($normal_video_id);  
              if($video && ($merchant_watchlist_exists['normal_video_display_count'] >= $video['login_quantity'] || !$video['status']))
              {
                 $normalSql = $this->db->query("SELECT * FROM tbl_admin_video WHERE is_broadcast = 0 AND status = 1 AND sequence_number > ".$video['sequence_number']." ORDER BY sequence_number ASC LIMIT 0,1")->row_array();
                 //next in sequence exists
                 if($normalSql)
                    return $normalSql;
              }
              elseif($video)
              {
                 return $video;
              }   
          }
          else
          {
              //if Normal exists, then load Normal video
              $normalSql = $this->db->query("SELECT * FROM tbl_admin_video WHERE is_broadcast=0 AND status = 1 ORDER BY sequence_number ASC LIMIT 0,1")->row_array();
              if($normalSql)
                 return $normalSql;
          }
      }

      function incCountForVideo($merchant_row_id, $video_id, $is_broadcast=0)
      {
          $merchant_watchlist_exists = $this->db->query("SELECT * FROM tbl_merchant_video_watchlist WHERE merchant_row_id=".$merchant_row_id)->row_array();
          if($merchant_watchlist_exists)
          {
              $data['modified_on'] = date('Y-m-d H:i:s');
              if($is_broadcast)
              {
                  if($merchant_watchlist_exists['broadcast_video_id']==$video_id)
                  {
                      $data['broadcast_video_display_count']=$merchant_watchlist_exists['broadcast_video_display_count'] + 1;
                  }
                  else
                  {
                      $data['broadcast_video_id'] = $video_id;
                      $data['broadcast_video_display_count']= 1;
                  }

              }
              else
              {
                  if($merchant_watchlist_exists['normal_video_id']==$video_id)
                  {
                      $data['normal_video_display_count']=$merchant_watchlist_exists['normal_video_display_count'] + 1;
                  }
                  else
                  {
                      $data['normal_video_id'] = $video_id;
                      $data['normal_video_display_count']= 1;
                  }

              }

              $this->db->where('merchant_row_id', $merchant_row_id);
              $this->db->update('tbl_merchant_video_watchlist',$data);
              echo $merchant_watchlist_exists['merchant_video_watchlist_id'];
          }
          else
          {
              $data['merchant_row_id'] = $merchant_row_id;
              $data['created_on'] = date('Y-m-d H:i:s');
              if($is_broadcast)
              {
                  $data['broadcast_video_id'] = $video_id;
                  $data['broadcast_video_display_count']=1;
              }
              else
              {
                  $data['normal_video_id'] = $video_id;
                  $data['normal_video_display_count']=1;
              }
              $this->db->insert('tbl_merchant_video_watchlist',$data);
              echo $this->db->insert_id();
          }

      }
      function resetCountForVideo($merchant_row_id, $video_id, $is_broadcast=0)
      {
          $video_info = $this->getAdminVideoDetailsByVideoRowId($video_id);
          $data['modified_on'] = date('Y-m-d H:i:s');

          if($is_broadcast)
          {
              $data['broadcast_video_display_count']= $video_info['login_quantity'];
          }
          else
          {
              $data['normal_video_display_count']= $video_info['login_quantity'];
          }
          $this->db->where('merchant_row_id', $merchant_row_id);
          $this->db->update('tbl_merchant_video_watchlist',$data);
          return $video_info['link'];
      }

  }
  ?>