<?php

namespace app\appapi\controller;

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

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;

class IndexController extends BaseController
{
    public function getConfig(Request $request)
    {
        $param = $request->get();
        // $where[] = ['lang', $param['lang']];

        $list = Db::name('config')
            // ->where($where)
            ->select()
            ->toArray();

        $result = [];
        foreach ($list as $key => $val) {
            $result[$val['lang']][$val['code']] = $val['value'];
        }

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

    public function getBannerList(Request $request)
    {
        $param = $request->get();
        // $where[] = ['lang', $param['lang']];

        $list = Db::name('banner')
            ->where('lang', $param['lang'])
            ->order('sort_order', 'asc')
            ->select()
            ->toArray();


        $result = $list;

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

    // 取得父類別列表
    public function getParentCateList(Request $request)
    {
        $param = $request->get();

        $where = [];

        $list = Db::name('category')
            ->where('lang', $param['lang'])
            ->where('parent_id', 0)
            ->order('sort_order', 'asc')
            ->select()
            ->toArray();


        foreach($list as $key => $val){
            // 取得子類別編號
            $list[$key]['cid'] = Db::name('category')
                ->where('lang', $param['lang'])
                ->where('parent_id', $val['id'])
                ->order('sort_order', 'asc')
                ->limit(1)
                ->value('id');
        }

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

    public function getSupplierList(Request $request)
    {
        $param = $request->get();
        // $where[] = ['lang', $param['lang']];

        $list = Db::name('supplier')
            ->order('sort_order', 'asc')
            ->order('id', 'asc')
            ->select()
            ->toArray();


        foreach ($list as $key => $val) {
            switch ($param['lang']) {
                case 'tw':
                    $list[$key]['name'] = $val['name1'];
                    break;
                case 'en':
                    $list[$key]['name'] = $val['name2'];
                    break;
                case 'cn':
                    $list[$key]['name'] = $val['name3'];
                    break;
            }

            $list[$key]['imageurl'] = $val['imageurl'];
        }

        $result = $list;

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

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

        $where = [];

        $list = Db::name('category')
            ->where('lang', $param['lang'])
            ->order('sort_order', 'asc')
            ->order('id', 'asc')
            ->select()
            ->toArray();

        $list = $this->genCategoryTree($list);

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

        // }

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

    public function genCategoryTree($list, $parent_id = 0)
    {
        $tree = [];
        foreach ($list as $key => $val) {
            if ($val['parent_id'] == $parent_id) {
                $val['children'] = $this->genCategoryTree($list, $val['id']);
                $tree[] = $val;
            }
        }
        return $tree;
    }

    public function sendEmail()
    {
        $to      = 'wwayne.hsu@gmail.com';
        $subject = 'the subject';
        $message = 'hello123';
        $headers = 'From: prodiosystem'       . "\r\n" .
                    'X-Mailer: PHP/' . phpversion();
    
        $rtn = mail($to, $subject, $message, $headers);

        return $rtn;
    }
}
