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

class Page_objects_model extends CI_Model{

    public $object_type = [
        'MAINTITLE' => '主標題',
        'SUBTITLE' => '子標題',
        'PICTURES' => '圖片',
    ];

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

    }

    public function get($id)
    {
        $result = $this->db->from('_page_objects')
            ->where('id', $id)
            ->get()->row_array();
        $this->load->model('Page_object_title_model', 'object_title');
        $this->load->model('Page_object_picture_model', 'object_picture');
        $result['objects'] = [];
        $objects = [];
        switch ($result['page_object_type']) {
            case "TITLE":
                $result['objects'][] = $this->object_title->get($result['object_id']);
                break;
            case "PICTURES":
                $result['objects'][] = $this->object_picture->get($result['object_id']);
                break;
        }
        return $result;
    }

    public function load_objects($main_id, $submenu_id)
    {
        if ($submenu_id === 0) {
            $this->db->where([
                'main_id' => $main_id,
            ]);
        }
        else {
            $this->db->where([
                'submenu_id' => $submenu_id,
            ]);
        }
        $this->db->order_by('displayorder', 'ASC');
        $result = $this->db->get('_page_objects')->result_array();
        $this->load->model('Page_object_title_model', 'object_title');
        $this->load->model('Page_object_picture_model', 'object_picture');
        foreach ($result as $idx => $res) {
            $result[$idx]['objects'] = [];
            $objects = [];
            switch ($res['page_object_type']) {
                case "TITLE":
                    $result[$idx]['objects'][] = $this->object_title->get($res['object_id']);
                    break;
                case "PICTURES":
                    $result[$idx]['objects'][] = $this->object_picture->get($res['object_id']);
                    break;
            }
        }
        return $result;
    }

    function update_rank($id, $rank)
    {
        $data = array(
            'displayorder' => $rank
        );

        $this->db->where('id', $id);
        $this->db->update('_page_objects', $data);
    }

    function delete($id)
    {
        $result = $this->db->from('_page_objects')
            ->where('id', $id)
            ->get()->row_array();
        switch ($result['page_object_type']) {
            case "TITLE":
                $this->db->delete('_page_object_title', ['id' => $result['object_id']]);
                break;
            case "PICTURES":
                $this->db->delete('_page_object_picture_info', ['id' => $result['object_id']]);
                $this->db->delete('_page_object_pictures', ['picture_info_id' => $result['object_id']]);
                break;
        }
        $this->db->where('id', $id);
        $this->db->delete('_page_objects');
    }
}
