<?php
namespace app\appapi\controller;

use app\appapi\BaseController;
use support\Request;
use think\facade\Db;

class SocialController extends BaseController
{
    public function getNewsList(Request $request)
    {
        $param = $request->post();

        $page       = isset($param['page']) ? $param['page'] : 1;
        $pageSize   = isset($param['pageSize']) ? $param['pageSize'] : 10;
        $search     = isset($param['search']) ? $param['search'] : [];

        $where = [];

        $list = Db::name('news')
            ->where($where)
            ->where('lang', $param['lang'])
            ->order('create_time', 'desc')
            ->page($page, $pageSize)
            ->select()
            ->toArray();

        $total = Db::name('news')
            ->where($where)
            ->where('lang', $param['lang'])
            ->count();

        // foreach($list as $key => $val){
        // }

        $result = [
            'list' => $list,
            'total' => $total
        ];

        return $this->Success($result);
    }

    public function getNews(Request $request)
    {
        $param = $request->get();

        if (empty($param['id'])) {
            return $this->Error('參數錯誤');
        }

        $where = [];

        $news = Db::name('news')
            ->where('id', $param['id'])
            ->find();

        $result = $news;

        return $this->Success($result);
    }

    public function getActivityList(Request $request)
    {
        $param = $request->post();

        $result = \app\common\GetData::getPageList('activity', $param);

        foreach ($result['list'] as $key => $val) {
            switch ($param['lang']) {
                case 'tw':
                    $result['list'][$key]['title'] = $val['title1'];
                    break;
                case 'cn':
                    $result['list'][$key]['title'] = $val['title2'];
                    break;
                case 'en':
                    $result['list'][$key]['title'] = $val['title3'];
                    break;
                default:
                    $result['list'][$key]['title'] = $val['title1'];
                    break;
            }

            $result['list'][$key]['imageList'] = json_decode($val['image_list']);
            $result['list'][$key]['create_time'] = date('Y-m-d', strtotime($val['create_time']));
            unset($result['list'][$key]['image_list']);
        }

        return $this->Success($result);
    }

    public function getActivity(Request $request)
    {
        $param = $request->get();

        if (empty($param['id'])) {
            return $this->Error('參數錯誤');
        }

        $where = [];

        $activity = Db::name('activity')
            ->where('id', $param['id'])
            ->find();

        $activity['imageList'] = json_decode($activity['image_list']);
        unset($activity['image_list']);

        switch ($param['lang']) {
            case 'tw':
                $activity['title'] = $activity['title1'];
                break;
            case 'cn':
                $activity['title'] = $activity['title2'];
                break;
            case 'en':
                $activity['title'] = $activity['title3'];
                break;
            default:
                $activity['title'] = $activity['title1'];
            break;
        }

        $result = $activity;

        return $this->Success($result);
    }

    public function getEsgList(Request $request)
    {
        $param = $request->post();

        $page       = isset($param['page']) ? $param['page'] : 1;
        $pageSize   = isset($param['pageSize']) ? $param['pageSize'] : 10;
        $search     = isset($param['search']) ? $param['search'] : [];

        $where = [];

        $list = Db::name('esg')
            ->where($where)
            ->where('lang', $param['lang'])
            ->order('create_time', 'desc')
            ->page($page, $pageSize)
            ->select()
            ->toArray();

        $total = Db::name('esg')
            ->where($where)
            ->where('lang', $param['lang'])
            ->count();

        // foreach($list as $key => $val){
        // }

        $result = [
            'list' => $list,
            'total' => $total
        ];

        return $this->Success($result);
    }

    public function getEsg(Request $request)
    {
        $param = $request->get();

        if (empty($param['id'])) {
            return $this->Error('參數錯誤');
        }

        $where = [];

        $news = Db::name('esg')
            ->where('id', $param['id'])
            ->find();

        $result = $news;

        return $this->Success($result);
    }

}
