<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Campaign_model extends CI_Model {
    
    
    function save_data($input){
        
        $resArr = [];
        $msg="";

        // $input = (array) $input;
        // extract($input);
        $device_type = $input->device_type;
        if( strtolower($input->device_type) == 'android' || ($input->device_type) == '1'){
            $device_type = 'android';
        }else if( strtolower($input->device_type) == 'ios' || ($input->device_type) == '2'){
            $device_type = 'ios';
        }else{
            $device_type = $device_type;
        }
        
        $is_dev_or_prod = $input->is_dev_or_prod;
        if( empty($is_dev_or_prod) || strtolower($input->is_dev_or_prod) == 'development' || ($input->is_dev_or_prod) == '0'){
            $is_dev_or_prod = 'development';
        }else if( strtolower($input->is_dev_or_prod) == 'production' || ($input->is_dev_or_prod) == '1'){
            $is_dev_or_prod = 'production';
        }else{
            $is_dev_or_prod = 'development';
        }
        
        // $qryAr=[];
        if(!empty($input->referer)){
            $referer = trim($input->referer);
            parse_str($referer, $qryAr);
            // var_dump($qryAr);
            // exit;
        }
        
        $utm_source = isset($qryAr["utm_source"]) && !empty($qryAr["utm_source"]) ? $qryAr["utm_source"]:'';
        $utm_medium= isset($qryAr["utm_medium"]) && !empty($qryAr["utm_medium"]) ? $qryAr["utm_medium"]:'';
        $utm_campaign= isset($qryAr["utm_campaign"]) && !empty($qryAr["utm_campaign"]) ? $qryAr["utm_campaign"]:'';
        $utm_content= isset($qryAr["utm_content"]) && !empty($qryAr["utm_content"]) ? $qryAr["utm_content"]:'';
        $utm_term= isset($qryAr["utm_term"]) && !empty($qryAr["utm_term"]) ? $qryAr["utm_term"]:'';
        $aref_key= isset($qryAr["aref_key"]) && !empty($qryAr["aref_key"]) ? $qryAr["aref_key"]:'';
        
        $data = array(
            'device_type'   => $device_type,
            'udid'          => $input->udid,
            'referer'       => $input->referer,
            'utm_source'    => $utm_source,
            'utm_medium'    => $utm_medium,
            'utm_campaign'  => $utm_campaign,
            'utm_content'   => $utm_content,
            'utm_term'      => $utm_term,
            'aref_key'      => $aref_key,
            'package_name'  => $input->package_name,
            'is_dev_or_prod'=> $is_dev_or_prod,
            'app_version'   => $input->app_version,
            'request_time'  => time() ,
            'created_at'    => date('Y-m-d H:i:s'),
        );
        
        if(1){
            $res = $this->db->insert('utm_data', $data);
            if($res){
               $resArr[]= $this->db->insert_id();;
               $msg='success';
            }
        }
        
        
        if(!empty($resArr)){
            $finalArr = ['res'=> true , 'data'=>$resArr,'msg'=>$msg ];
        }
        else{
            $finalArr = ['res'=> false ,'data'=>$resArr,'msg'=>$msg ];
        }
        return $finalArr;
        
        
    }
    

}