下載

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Tmember;
use DB;

class MemberController extends Controller
{

    // 購物街
    public function MemShopping($id) {
        $products = DB::table('product')->where("acc","!=",session('LoginAcc'))->where("sno","=",$id)->where("status","=","上架中")->get();
        return view('Member.MemShopping',['products' => $products]);
    }

    public function MemShoppingDetail($id) {
        $products = DB::table('product')->where("id","=",$id)->get();
        return view('Member.MemShoppingDetail',['product' => $products]);
    }

    // 商品管理
    public function MemProductMgt() {
        $products = DB::table('product')->where("acc","=",session('LoginAcc'))->get();
        return view('Member.MemProductMgt',['products' => $products]);
    }

    public function MemProductAdd() {
        return view('Member.MemProductAdd');
    }

    public function MemProductAddDB(Request $r) {
        $name = $r->name; // 名稱
        $sno = $r->sno; // 分類
        $price = $r->price; // 價格
        // $eval = $r->eval; // 評價
        $status = $r->status; // 狀態
        $acc = session('LoginAcc');  // 從 session 取得登入者帳號

        if ($r->hasFile('photo')) {
            $file = $r->file('photo');    
            $dstPath = public_path('images');       
            $ext = $file->getClientOriginalExtension(); 
            $f = uniqid('img_', true) . "." . $ext;   
            $file->move($dstPath, $f);         
            $photo = $f;       
        }

        DB::table('product')->insert([
            'name' => $name,
            'sno' => $sno,
            'acc' => $acc,
            'photo' => $photo,
            'price' => $price,
            'status' => $status
        ]);

        echo "<script>alert('新增成功');window.location.href='../Member/MemProductMgt';</script>";
    }

    public function MemProductEdit($id) {
        $product = DB::table('product')->where('id', $id)->first();
        return view('Member.MemProductEdit', ['product' => $product]);
    }

    public function MemProductUpdateDB(Request $r)
    {
        $id = $r->id;  // 任務的 id(可能從隱藏欄位傳來)
        $name = $r->name;
        $sno = $r->sno;
        $acc = session("LoginAcc");
        $price = $r->price;
        $status = $r->status;

        // 先查出舊照片名稱
        $oldPhoto = DB::table('product')->where('id', $id)->value('photo');
        $photo = $oldPhoto;  // 預設保留舊照片

        if ($r->hasFile('photo')) {
            $dstPath = 'images';
            $f = uniqid() . "." . $r->photo->getClientOriginalExtension();  // 產生亂數檔名
            $r->photo->move($dstPath, $f);  // 上傳新檔
            $photo = $f;  // 更新照片欄位

            // 刪除舊檔案
            if (!empty($oldPhoto)) {
                $oldFile = "$dstPath/$oldPhoto";
                if (file_exists($oldFile)) {
                    unlink($oldFile);
                }
            }
        }

        // 使用 SQL 更新 todos 表資料
        DB::update(
            "UPDATE `product` SET `name` = ?, `sno` = ?, `price` = ?, `status` = ?, `photo` = ?, `acc` = ? WHERE `id` = ?",
            [$name, $sno, $price, $status, $photo, $acc, $id]
        );

        echo "<script>alert('更新成功');window.location.href='../Member/MemProductMgt';</script>";
    }

    public function MemProductDelete(Request $r)
    {
        $id = $r->id;

        // 查詢 todo 資料
        $sql = "SELECT photo FROM product WHERE id = ?";
        $rows = DB::select($sql, [$id]);

        if (count($rows) > 0) {
            $photo = $rows[0]->photo;

            // 如果有圖片,先刪除圖片檔案
            if (!empty($photo)) {
                $oldFile = public_path('images/' . $photo);
                $oldFile = str_replace('\\', '/', $oldFile); // 確保路徑格式正確
                if (file_exists($oldFile) && is_file($oldFile)) {
                    unlink($oldFile);
                }
            }

            // 刪除 todo 資料
            $deleteSql = "DELETE FROM product WHERE id = ?";
            DB::delete($deleteSql, [$id]);

            echo "<script>alert('刪除成功');window.location.href='../../Member/MemProductMgt';</script>";
        } else {
            echo "<script>alert('查無資料');window.location.href='../../Member/MemProductMgt';</script>";
        }
    }

    /*--------------------------------------*/
    public function Index() {
        return view('Member.Index');
    }

    public function Todo() {
        $todos = DB::table('todo')->where("acc","=",session('LoginAcc'))->get();
        return view('Member.Todo',['todo' => $todos]);
    }

    public function TodoAdd() {
        return view('Member.TodoAdd');
    }

    public function TodoAddSave(Request $r) {
        $title = $r->title;
        $content = $r->content;
        $level = $r->level;
        $deadline = $r->deadline;
        $acc = session('LoginAcc');  // 從 session 取得登入者帳號

        if ($r->hasFile('photo')) {
            $file = $r->file('photo');    
            $dstPath = public_path('images');       
            $ext = $file->getClientOriginalExtension(); 
            $f = uniqid('img_', true) . "." . $ext;   
            $file->move($dstPath, $f);         
            $photo = $f;       
        }

        DB::table('todo')->insert([
            'title'    => $title,
            'content'  => $content,
            'level'    => $level,
            'deadline' => $deadline,
            'photo'    => $photo,
            'acc'      => $acc
        ]);

        echo "<script>alert('任務新增成功');window.location.href='../Member/Todo';</script>";
    }

    public function TodoEdit($id) {
        $todo = DB::table('todo')->where('id', $id)->first();
        return view('Member.TodoEdit', ['todo' => $todo]);
    }

    public function TodoUpdate(Request $r)
    {
        $id = $r->id;  // 任務的 id(可能從隱藏欄位傳來)
        $title = $r->title;
        $content = $r->content;
        $level = $r->level;
        $deadline = $r->deadline;

        // 先查出舊照片名稱
        $oldPhoto = DB::table('todo')->where('id', $id)->value('photo');
        $photo = $oldPhoto;  // 預設保留舊照片

        if ($r->hasFile('photo')) {
            $dstPath = 'images';
            $f = uniqid() . "." . $r->photo->getClientOriginalExtension();  // 產生亂數檔名
            $r->photo->move($dstPath, $f);  // 上傳新檔
            $photo = $f;  // 更新照片欄位

            // 刪除舊檔案
            if (!empty($oldPhoto)) {
                $oldFile = "$dstPath/$oldPhoto";
                if (file_exists($oldFile)) {
                    unlink($oldFile);
                }
            }
        }

        // 使用 SQL 更新 todos 表資料
        DB::update(
            "UPDATE `todo` SET `title` = ?, `content` = ?, `level` = ?, `deadline` = ?, `photo` = ? WHERE `id` = ?",
            [$title, $content, $level, $deadline, $photo, $id]
        );

        echo "<script>alert('任務更新成功');window.location.href='../Member/Todo';</script>";
    }

    public function TodoDelete(Request $r)
    {
        $id = $r->id;

        // 查詢 todo 資料
        $sql = "SELECT photo FROM todo WHERE id = ?";
        $rows = DB::select($sql, [$id]);

        if (count($rows) > 0) {
            $photo = $rows[0]->photo;

            // 如果有圖片,先刪除圖片檔案
            if (!empty($photo)) {
                $oldFile = public_path('images/' . $photo);
                $oldFile = str_replace('\\', '/', $oldFile); // 確保路徑格式正確
                if (file_exists($oldFile) && is_file($oldFile)) {
                    unlink($oldFile);
                }
            }

            // 刪除 todo 資料
            $deleteSql = "DELETE FROM todo WHERE id = ?";
            DB::delete($deleteSql, [$id]);

            echo "<script>alert('刪除成功');window.location.href='../../Member/Todo';</script>";
        } else {
            echo "<script>alert('查無資料');window.location.href='../../Member/Todo';</script>";
        }
    }



    public function MemberInfo() {
        $acc = session('LoginAcc');
        $sql = "SELECT * FROM member WHERE acc = ?";
        $users = DB::select($sql, [$acc]);
        // $users = Tmember::all(); 
        if ($users == NULL){
            return "NO DATA........";
        }else{
            return view('Member.MemberInfo', ['users' => $users]);
        }
        return view('Member.MemberInfo');
    }

    public function MemberUpdate(Request $r)
    {
        $acc = session('LoginAcc');
        $pwd = $r->newPWD;
        $sex = $r->sex;
        $name = $r->name;

        // 先查出舊照片名稱
        $oldPhoto = DB::table('member')->where('acc', $acc)->value('photo');
        $photo = $oldPhoto;  // 預設先保留原照片

        if ($r->hasFile('upFile')) {
            $dstPath = 'images';
            $f = uniqid() . "." . $r->upFile->getClientOriginalExtension();  // 產生亂數檔名
            $r->upFile->move($dstPath, $f);  // 上傳檔案
            $photo = $f;  // 新照片檔名

            // 刪除舊檔
            if (!empty($oldPhoto)) {
                $oldFile = "$dstPath/$oldPhoto";
                if (file_exists($oldFile)) {
                    unlink($oldFile);
                }
            }
        }

        // 使用 SQL 語法更新資料
        DB::update(
            "UPDATE `member` SET `pwd` = ?, `sex` = ?, `name` = ?, `photo` = ? WHERE `acc` = ?",
            [$pwd, $sex, $name, $photo, $acc]
        );

        echo "<script>alert('編輯成功');window.location.href='../Member/MemberInfo';</script>";
    }

}