<?php

namespace app\adminapi\controller;

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

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

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


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

        $result = $list;

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

    public function addBranch(Request $request){
        $params = $request->post();
        
        $data = [
            'lang' => isset($params['lang'])?$params['lang']:'tw',
            'name' => isset($params['name'])?$params['name']:'',
            'address' => isset($params['address'])?$params['address']:'',
            'tel' => isset($params['tel'])?$params['tel']:'',
            'fax' => isset($params['fax'])?$params['fax']:'',
            'email' => isset($params['email'])?$params['email']:'',
            'url' => isset($params['url'])?$params['url']:'',
            'imageurl' => isset($params['imageurl'])?$params['imageurl']:'',
            'status' => 1
        ];
        
        try{
            $id = Db::name('branch')->insertGetId($data);

            // Db::name('branch')
            // ->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 deleteBranch(Request $request){
        $id = $request->get('id');

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

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

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

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

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

    public function updateBranch(Request $request){
        $params = $request->post();
       
        $data = [
            'lang' => isset($params['lang'])?$params['lang']:'tw',
            'name' => isset($params['name'])?$params['name']:'',
            'address' => isset($params['address'])?$params['address']:'',
            'tel' => isset($params['tel'])?$params['tel']:'',
            'fax' => isset($params['fax'])?$params['fax']:'',
            'email' => isset($params['email'])?$params['email']:'',
            'url' => isset($params['url'])?$params['url']:'',
            'imageurl' => isset($params['imageurl'])?$params['imageurl']:'',
            'sort_order' => isset($params['sort_order'])?$params['sort_order']:100,
        ];

        try{
            Db::name('branch')
            ->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/branch/'. $filename);
            $savePath = '/storage/branch/'. $filename;
            return $this->Success($savePath);
        }
        return $this->Error('操作失敗');
    }
}
