<?php

namespace app\adminapi\controller;

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

class SupplierController extends BaseController
{
    public function getSupplierList(Request $request)
    {
        $param = $request->get();

        $where[] = ['lang', $param['lang']];

        $list = Db::name('supplier')
            ->order('sort_order', 'asc')
            ->order('id', 'asc')
            ->select()
            ->toArray();


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

        $result = $list;

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

    public function addSupplier(Request $request)
    {
        $params = $request->post();

        $data = [
            'lang' => isset($params['lang']) ? $params['lang'] : 'tw',
            'name1' => isset($params['name1']) ? $params['name1'] : '',
            'name2' => isset($params['name2']) ? $params['name2'] : '',
            'name3' => isset($params['name3']) ? $params['name3'] : '',
            'imageurl' => isset($params['imageurl']) ? $params['imageurl'] : '',
            'create_time' => date('Y-m-d H:i:s', time())
        ];

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

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

        try {
            $result = Db::name('supplier')
                ->where('id', $id)
                ->delete();

            AdminLog($this->uid, '刪除供應商 [' . $id . ']');
            return $this->Success('操作成功');
        } catch (\Exception $e) {
            AdminLog($this->uid, '刪除供應商 [' . $id . '] 失敗');
            return $this->Error('操作失敗');
        }
    }

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

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

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

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

    public function updateSupplier(Request $request)
    {
        $params = $request->post();

        $data = [
            'name1' => isset($params['name1']) ? $params['name1'] : '',
            'name2' => isset($params['name2']) ? $params['name2'] : '',
            'name3' => isset($params['name3']) ? $params['name3'] : '',
            'imageurl' => isset($params['imageurl']) ? $params['imageurl'] : '',
            'sort_order' => isset($params['sort_order']) ? $params['sort_order'] : 100,
            'update_time' => date('Y-m-d H:i:s', time())
        ];

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

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

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