<?php
namespace app\adminapi\controller\v1;

use app\adminapi\ApiController;
use think\facade\Db;

class Shipping extends ApiController{
    //取得支付列表
    public function list(){
        
        $do=Db::name('shipping')
        ->order('shipping_order','asc');
        
        $shipping=$do->select();

        $result = [
            'page'    => 1,  
            'total'   => $do->count(),  
            'data'    => $shipping  
        ];
            
        return $this->Success($result);
    }

    //取得支付資料
    public function getShipping(){

        $id=input('id');
        $result=Db::name('shipping')
        ->where('shipping_id',$id)
        ->find();
        
        return $this->Success($result);
    }
    
    //更新支付資料
    public function update(){
        $data=input('post.');

        try{
            Db::name('shipping')
            ->where('shipping_id',$data['shipping_id'])
            ->update($data);    
        }catch(\Exception $e){
            return $this->Error($e->getMessage());
        }
        
        return $this->Success('更新成功');
    }

    public function updateEnabled(){
        $data=input('post.');

        try{
            Db::name('shipping')
            ->where('shipping_id',$data['shipping_id'])
            ->update(['enabled'=>$data['enabled']]);    
        }catch(\Exception $e){
            return $this->Error($e->getMessage());
        }
        
        return $this->Success('更新成功');
    }

    public function updateOrder(){
        $data=input('post.');

        foreach($data as $key=>$value){
            try{
                Db::name('shipping')
                ->where('shipping_id',$value['shipping_id'])
                ->update(['shipping_order'=>$value['shipping_order']]);    
            }catch(\Exception $e){
                return $this->Error($e->getMessage());
            }
        }
        return $this->Success('更新成功');
    }
}
