<?php

namespace app\adminapi\controller;

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

class EsgController extends BaseController
{
    public function getEsgList(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('esg')
            ->where($where)
            ->where('lang', $param['lang'])
            ->order('create_time', 'desc')
            ->page($page, $pageSize)
            ->select()
            ->toArray();

        $total = Db::name('esg')
            ->where($where)
            ->where('lang', $param['lang'])
            ->count();

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

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

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

    public function addEsg(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('esg')->insertGetId($data);

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

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

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

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

            $result = Db::name('esg')
            ->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 getEsg(Request $request){
        $id = $request->get('id');

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

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

        unset($user['password']);

        $result = $user;

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

    public function updateEsg(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('esg')
            ->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());                
        }
    }

}
