<?php

namespace app\adminapi\controller;

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

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

        $where = [];

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


        foreach($list as $key => $val){
            $list[$key]['imageurl1'] = getUrl().$val['imageurl1'];
            $list[$key]['imageurl2'] = getUrl().$val['imageurl2'];
        }

        $result = $list;

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

    public function addBanner(Request $request){
        $params = $request->post();
        

        $data = [
            'slogon' => isset($params['slogon'])?$params['slogon']:'',
            'describe' => isset($params['describe'])?$params['describe']:'',
            'imageurl1' => isset($params['imageurl1'])?$params['imageurl1']:'',
            'imageurl2' => isset($params['imageurl2'])?$params['imageurl2']:'',
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        
        try{
            $id = Db::name('banner')->insertGetId($data);

            AdminLog( $this->uid , '新增輪撥 [' . $id .']');

            return $this->Success('操作成功');
        }catch(\Exception $e){
            AdminLog( $this->uid , '新增輪撥 [' . $id . '] 失敗');
            
            return $this->Error('操作失敗:'. $e->getMessage());                
        }
    }

    public function deleteBanner(Request $request){
        $id = $request->get('id');

        try{
            $result = Db::name('banner')
            ->where('id',$id)
            ->delete();
    
            AdminLog($this->uid, '刪除輪撥 ['. $id .']');
            return $this->Success('操作成功');    
        }catch(\Exception $e){
            AdminLog($this->uid, '刪除輪撥 ['.$id.'] 失敗');
            return $this->Error('操作失敗');                
        }
    }

    public function getArticle(Request $request){
        $id = $request->get('id');
        $lang = $request->get('lang');

        $result = Db::name('article')
        ->where('cat_id',$id)
        ->where('lang',$lang)
        ->find();

        if(!$result){
            return $this->Error('資料不存在');
        }

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

    public function updateArticle(Request $request){
        $params = $request->post();
       
        $data = [
            'content' => isset($params['content'])?$params['content']:'',
            'update_time' => date('Y-m-d H:i:s',time())
        ];

        try{
            Db::name('article')
            ->where('cat_id',$params['id'])
            ->where('lang',$params['lang'])
            ->update($data);
            
            AdminLog($this->uid, '修改內容 [' . $params['id'] .']');
    
            return $this->Success('操作成功');
        }catch(\Exception $e){

            AdminLog($this->uid, '修改內容 [' . $params['id'] . '] 失敗');
            
            return $this->Error('操作失敗:'. $e->getMessage());                
        }
    }

    public function uploadImage(Request $request){
        $file = $request->file('upload');

        if ($file && $file->isValid()) {
            // 產生唯一的檔名
            $filename = uniqid() . '.' . $file->getUploadExtension();

            $file->move(public_path().'/storage/article/'. $filename);
            $savePath = getUrl().'/storage/article/'. $filename;
            return json([
                'url' => $savePath
            ]);
        }
        return $this->Error('操作失敗');
    }
}
