<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Mira_service_model
 *
 * @author zmq163
 */
class Mira_servey_form_model extends CI_Model {

    //put your code here
    var $mdb;

    public function __construct() {
        parent::__construct();
        $this->mdb = $this->load->database('mdb', TRUE);

    }

    /**
     * 
     * 
     * 
     * 
     * 
     */
     
     
   public function mira_theater_data($records = null) {
    $results = [];

    if (!is_array($records) || empty($records)) {
        return [[
            'status' => FALSE,
            'message' => 'No records to process',
            'server_activity_id' => 0,
            'mobile_activity_id' => 0
        ]];
    }

    foreach ($records as $record) {
        if (empty($record['mobile_activity_id'])) {
            $results[] = [
                'status' => FALSE,
                'message' => 'Missing mobile_activity_id',
                'server_activity_id' => 0,
                'mobile_activity_id' => $record['mobile_activity_id'] ?? 0
            ];
            continue;
        }

        // Check if record exists
        if($record['dir_id']==1)
        $tablename='mira_theater_activity';
        else if($record['dir_id']==2)
        $tablename='kiosk_activity';
        else if($record['dir_id']==3)
        $tablename='poster_compaign_activity';
        else if($record['dir_id']==4)
        $tablename='stakeholder_meeting_activity';
        else if($record['dir_id']==5)
        $tablename='training_activity';
        
        $this->mdb->where('mobile_activity_id', $record['mobile_activity_id']);
        $query = $this->mdb->get($tablename);

        if ($query->num_rows() > 0) {
            $existing = $query->row();
            $results[] = [
                'status' => FALSE,
                'message' => 'Already exists',
                'server_activity_id' => $existing->id,
                'mobile_activity_id' => $existing->mobile_activity_id
            ];
        } else {
            // Insert
            
             if (empty($record['photo_path'])) {
               $record['photo_path']='NA'   ;
              }
              else
              {
              if($record['dir_id']==1)
              {
              $record['screening_date_time'] = date('Y-m-d H:i:s',strtotime(str_replace('/', '-', $record['screening_date_time'])));
              $record['photo_path']='public/storage/mira_theater_activity/'.$record['mobile_activity_id'].'.jpg';
              }
              else if($record['dir_id']==2)
              $record['photo_path']='public/storage/kiosk_activity/'.$record['mobile_activity_id'].'.jpg';
              else if($record['dir_id']==3)
              $record['photo_path']='public/storage/poster_compaign_activity/'.$record['mobile_activity_id'].'.jpg';
              else if($record['dir_id']==4)
              $record['photo_path']='public/storage/stakeholder_meeting_activity/'.$record['mobile_activity_id'].'.jpg';
              else if($record['dir_id']==5)
              {
              $record['photo_path']='public/storage/training_activity/'.$record['mobile_activity_id'].'.jpg';
              $record['training_date']=date('Y-m-d', strtotime(str_replace('/', '-', $record['training_date'])));
              }
              }
             
              unset($record['dir_id']);
            if (!$this->mdb->insert($tablename, $record)) {
                $error = $this->mdb->error();
                $results[] = [
                    'status' => FALSE,
                    'message' => 'Insert failed: ' . $error['message'],
                    'server_activity_id' => 0,
                    'mobile_activity_id' => $record['mobile_activity_id']
                ];
                continue;
            }

            $id = $this->mdb->insert_id();
            $results[] = [
                'status' => TRUE,
                'message' => 'Inserted',
                'server_activity_id' => $id,
                'mobile_activity_id' => $record['mobile_activity_id']
            ];
        }
    }

    return $results;
}




     
     
   
    public function getData($_query = NULL) {
        if ($_query != NULL and $_query->num_rows() > 0) {
            return $_query->result_array();
        } else {
            return FALSE;
        }
    }

}
