<?php

namespace app\adminapi\controller;

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

class NewsController 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 addNews(Request $request){
        $params = $request->post();
        
        $data = [
            'title' => isset($params['title'])?$params['title']:'',
            'content' => isset($params['content'])?$params['content']:'',
            'lang' => isset($params['lang'])?$params['lang']:'tw',
            'create_time' => date('Y-m-d H:i:s',time())
        ];
        
        try{
            $id = Db::name('news')->insertGetId($data);

            AdminLog( $this->uid , '新增產業動態 ' . $id);

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

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

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

            $result = Db::name('news')
            ->where('id',$id)
            ->delete();
    
            AdminLog($this->uid, '刪除管理員 '. $admin_name);
            return $this->Success('操作成功');    
        }catch(\Exception $e){
            AdminLog($this->uid, '刪除管理員'.$admin_name.'失敗');
            return $this->Error('操作失敗');                
        }
    }

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

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

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

        unset($user['password']);

        $result = $user;

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

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

        try{
            $result = Db::name('news')
            ->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());                
        }
    }

}
