<?php

namespace app\adminapi\controller;

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

class ActivityController extends BaseController
{
    public function getActivityList(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('activity')
            ->where($where)
            ->order('id', 'desc')
            ->page($page, $pageSize)
            ->select()
            ->toArray();

        $total = Db::name('activity')
            ->where($where)
            ->count();

        foreach($list as $key => $val){
            $list[$key]['imageList'] = json_decode($val['image_list']);
            unset($list[$key]['image_list']);
        }

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

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

    public function addActivity(Request $request){
        $params = $request->post();
        
        $data = [
            'lang' => 'tw',
            'title1' => isset($params['title1'])?$params['title1']:'',
            'title2' => isset($params['title2'])?$params['title2']:'',
            'title3' => isset($params['title3'])?$params['title3']:'',
            'image_list' => isset($params['imageList'])?json_encode($params['imageList']):'',
            'create_time' => date('Y-m-d H:i:s',time())
        ];

        try{
            $id = Db::name('activity')->insertGetId($data);

            AdminLog( $this->uid , '新增公司活動 ' . $id);

            return $this->Success('操作成功');
        }catch(\Exception $e){
            AdminLog( $this->uid , '新增公司活動失敗');
            
            return $this->Error('操作失敗:'. $e->getMessage());                
        }
    }

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

        try{
            // $admin_name = getNameById('admin','username',$id);

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

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

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

        if(!$result){
            return $this->Error('資料不存在');
        }
        $result['imageList'] = json_decode($result['image_list']);

        unset($result['image_list']);

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

    public function updateActivity(Request $request){
        $params = $request->post();
        
        $data = [
            'title1' => isset($params['title1'])?$params['title1']:'',
            'title2' => isset($params['title2'])?$params['title2']:'',
            'title3' => isset($params['title3'])?$params['title3']:'',
            'image_list' => isset($params['imageList'])?json_encode($params['imageList']):'',
            'update_time' => date('Y-m-d H:i:s',time())
        ];

        try{
            $result = Db::name('activity')
            ->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 uploadImage(Request $request){
        $file = $request->file('file');

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

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