<?php

namespace app\adminapi\controller;

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

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

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

        $result = $list;

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

    public function addBanner(Request $request){
        $params = $request->post();
        
        $data = [
            'lang' => isset($params['lang'])?$params['lang']:'tw',
            '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);

            // Db::name('banner')
            // ->where('id',$id)
            // ->update(['sort_order'=>($id * 10)]);

            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 getBanner(Request $request){
        $id = $request->get('id');

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

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

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

    public function updateBanner(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']:'',
            'sort_order' => isset($params['sort_order'])?$params['sort_order']:100,
            'update_time' => date('Y-m-d H:i:s',time())
        ];

        try{
            Db::name('banner')
            ->where('id',$params['id'])
            ->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 uploadBannerImage(Request $request){
        $file = $request->file('file');

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

            $file->move(public_path().'/storage/banner/'. $filename);
            $savePath = '/storage/banner/'. $filename;
            return $this->Success($savePath);
        }
        return $this->Error('操作失敗');
    }
}
