<?php

class DSAmazonOption_model extends MY_Model
{

    private $id;
    private $default_id;
    private $option_name;
    private $option_title;
    private $asin;
    private $img;    
    private $created_at;


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

    public function save($options, $optionsASIN, $option_name, $default_id) {
        $db_options = $this->db->get_where_in('ds_amazon_option', ['asin' => $optionsASIN])->result(); 

        foreach($options as $k => $opt) {
            $this->option_name = $option_name;
            $this->default_id = $default_id;
            $this->asin = $optionsASIN[$k];
            $this->option_title = $opt->title;
            $this->img = $opt->img;
            $this->created_at = date('Y-m-d H:i:s');            
            if($this->isDBOption($db_options, $opt->asin)) {
                $this->db->where('asin', $opt->asin)->update('ds_amazon_option', $this);
            }else {
                $this->db->insert('ds_amazon_option', $this);
            }            
        }
    }

    private function isDBOption($db_options, $asin) {
        foreach($db_options as $o) {
            if($o->asin == $asin) {
                return true;
            }
        }
    }


   
}
