<?php

namespace App\Http\Controllers;

use App\Article;
use App\Category;
use App\Keyword;
use App\Laravue\Models\User;

/**
 * Class HomeController
 *
 * @package App\Http\Controllers
 */
class SiteMapController extends Controller
{
    private $_root_url;
    private $_config_root;
    private $_config_url;

    public function __construct()
    {
        $this->_root_url = url('/');
        $uri = str_replace("https:", "", $this->_root_url);
        $uri = str_replace("http:", "", $uri);

        $this->_config_root = [
            'template'      => '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="' . $uri . '/main-sitemap.xsl"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>',
            'caseSensitive' => false,
            'charset'       => 'utf-8',
            'rowName'       => 'sitemap',
        ];
        $this->_config_url = [
            'template'      => '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="' . $uri . '/main-sitemap.xsl"?><urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></urlset>',
            'caseSensitive' => false,
            'charset'       => 'utf-8',
            'rowName'       => 'url',
        ];
    }

    public function index()
    {
        $data = [
            ['loc' => $this->_root_url . '/sitemap-page.xml', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/sitemap-bbs.xml', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/sitemap-column.xml', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/sitemap-care.xml', 'lastmod' => '2022-11-29T09:54:20+09:00']
        ];

        return response()->xml($data, 200, $this->_config_root);
    }

    public function page()
    {
        $data = [
            ['loc' => $this->_root_url . '/', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/faq', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/contact', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/register', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/login', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/user', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/bbs_post', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/inquiry', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/completed', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/thank-you', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/privacy', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/transactions', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/company', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/community', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/advice', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/general', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/about-soudan', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/guidelines', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/forgot-password', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/401', 'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/404', 'lastmod' => '2022-11-29T09:54:20+09:00'],
        ];

        //['loc' => $this->_root_url . '/news', 'lastmod' => '2022-11-29T09:54:20+09:00'],
        //['loc' => $this->_root_url . '/hashtag', 'lastmod' => '2022-11-29T09:54:20+09:00'],

        return response()->xml($data, 200, $this->_config_url);
    }

    public function bbs()
    {
        $data = [
            ['loc' => $this->_root_url . '/bbs',                    'lastmod' => '2022-11-29T09:54:20+09:00'],
            ['loc' => $this->_root_url . '/bbs/search',             'lastmod' => '2022-11-29T09:54:20+09:00'],
        ];

        //Category
        $categories = Category::select('id', 'updated_at')
            ->where('is_activated', true)
            ->where('is_deleted', false)
            ->get();

        foreach ($categories as $item) {
            $time = '2022-11-29T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/bbs/category/' . $item->id,
                'lastmod'   => $time
            ];
        }

        //Tag
        $tags = Keyword::select('id', 'updated_at')
            ->where('is_activated', true)
            ->where('is_deleted', false)
            ->get();

        foreach ($tags as $item) {
            $time = '2022-11-29T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/bbs/tag/' . $item->id,
                'lastmod'   => $time
            ];
        }

        //Detail
        $list = Article::select('id', 'slug', 'title', 'publish_at')
            ->where('type', 'question')
            ->where('is_activated', true)
            ->where('is_deleted', false)
            ->get();

        foreach ($list as $item) {
            $slug = $item->slug != "" ? $item->slug : $item->title;
            $time = '2022-11-29T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/bbs/detail/' . $item->id . '/' . $slug,
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url);
    }

    public function column()
    {
        $data = [
            ['loc' => $this->_root_url . '/column', 'lastmod' => '2022-11-29T09:54:20+09:00'],
        ];

        //Category
        $categories = Category::select('id', 'updated_at')
            ->where('is_activated', true)
            ->where('is_deleted', false)
            ->get();

        foreach ($categories as $item) {
            $time = '2022-11-29T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/column/category/' . $item->id,
                'lastmod'   => $time
            ];
        }

        //Tag
        $tags = Keyword::select('id', 'updated_at')
            ->where('is_activated', true)
            ->where('is_deleted', false)
            ->get();

        foreach ($tags as $item) {
            $time = '2022-11-29T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/column/tag/' . $item->id,
                'lastmod'   => $time
            ];
        }

        //Detail
        $list = Article::select('id', 'slug', 'title', 'publish_at')
            ->where('type', 'article')
            ->where('is_activated', true)
            ->where('is_deleted', false)
            ->get();

        foreach ($list as $item) {
            $slug = $item->slug != "" ? $item->slug : $item->title;
            $time = '2022-11-29T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/column/detail/' . $item->id . '/' . $slug,
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url);
    }

    public function care()
    {
        $data = [['loc' => $this->_root_url . '/care-manager', 'lastmod' => '2022-11-29T09:54:20+09:00']];

        $list = User::whereHas('roles', function ($q) {
            $q->where('name', 'nursingstaff');
        })->orderBy('id', 'ASC')->get();

        foreach ($list as $item) {
            $time = '2022-11-29T09:54:20+09:00';
            if($item->updated_at != null) $time = date(\DateTime::ATOM, strtotime($item->updated_at));

            $data[] = [
                'loc'       => $this->_root_url . '/care-manager/' . $item->slug,
                'lastmod'   => $time
            ];
        }

        return response()->xml($data, 200, $this->_config_url);
    }
}
