<?php
/**
 *  价格列表
 *  @author  system
 *  @version    1.0
 *  @date 2018-05-19 16:10:08
 *
 */
namespace App\Http\Controllers\Admin\Album\Product;
use App\Http\Controllers\Admin\Controller;
use App\Models\AlbumCatModel;
use App\Models\AlbumProductModel;
use App\Models\AlbumProductPriceModel;
use App\Repositories\Album\Criteria\PriceWhere;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Album\Product\Criteria\MultiWhere;
use App\Repositories\Album\Product\PriceRepository;

class PriceController extends Controller
{
    private $repository;

    public function __construct(PriceRepository $repository) {
        if(!$this->repository) $this->repository = $repository;
    }

    function index(Request $request) {
        $agent_id = $request->input('id');
        //dd($agent_id);die;
        $search['keyword'] = $request->input('keyword');
        $search['cat_id'] = $request->input('cat_id');
        $query = $this->repository->pushCriteria(new PriceWhere($search, $this->getStoreId(), $agent_id));

        if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
        $query = $query->pushCriteria(new OrderBy($request['sort_field'],$request['sort_field_by']));
        }else{
            $query = $query->pushCriteria(new OrderBy('id','DESC'));
        }
        $list = $query->paginate();
        foreach ($list as $item){
            $product = AlbumProductModel::where([['id',$item->product_id],['store_id',$this->getStoreId()]])->first();
            $item->product_name = $product['name'];
            $cat = AlbumCatModel::where('id', $product['cat_id'])->first();
            $item->product_cat = $cat->name;
            $item->product_ma_price_comment = $product['ma_price_comment'];
            $item->product_pic = $product['cover_pic'];
        }
        $cat = AlbumCatModel::where([['store_id',$this->getStoreId()],['parent_id',0]])->get();
        return view('admin.album.product.price.index',compact('list', 'cat'));
    }


    function check(Request $request) {
        $request = $request->all();
        $search['keyword'] = $request->input('keyword');
        $orderby = array();
        if(isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
            $orderby[$request['sort_field']] = $request['sort_field_by'];
        }
        $list = $this->repository->search($search,$orderby);
        return view('admin.album.product.price.check',compact('list'));
    }


    /**
     * 添加
     * 
     */
    public function create(Request $request)
    {
        if($request->method() == 'POST') {
            return $this->_createSave();
        }
        return view('admin.album.product.price.edit');
    }

    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'Album/Product/Price/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'Album/Product/Price/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'Album/Product/Price/index'),'title'=>'返回列表');
            return $this->showWarning('添加失败',$url);
        }
    }
    
    /**
     * 
     * 修改
     * 
     * 
     */
    public function update(Request $request) {
        if($request->method() == 'POST') {
            return $this->_updateSave();
        }
        $data = $this->repository->find($request->get('id'));
        return view('admin.album.product.price.edit',compact('data'));
    }

    /**
     * 保存修改
     */
    private function _updateSave() {
        $data = (array) request('data');
        $ok = $this->repository->update(request('id'),$data);
        if($ok) {
            $url[] = array('url'=>U( 'Album/Product/Price/index'),'title'=>'返回列表');
            return $this->showMessage('操作成功',urldecode(request('_referer')));
        }else{
            $url[] = array('url'=>U( 'Album/Product/Price/index'),'title'=>'返回列表');
            return $this->showWarning('操作失败',$url);
        }
    }

    public function view(Request $request) {
        $data = $this->repository->find(request('id'));
        return view('admin.album.product.price.view',compact('data'));
    }


    /**
     *
     * 状态改变
     *
     */
    public function status(Request $request) {
        $ok = $this->repository->updateStatus(request('id'),request('status'));
        if($ok) {
            return $this->showMessage('操作成功');
        }else{
            return $this->showWarning('操作失败');
        }
    }
    
    /**
     * 删除
     */
    public function destroy(Request $request) {
        $cat = AlbumProductPriceModel::find($request->get('id'));
        $ok = $cat->delete();
        if($ok) {
            return  $this->showMessage('操作成功');
        }else{
            return  $this->showWarning("操作失败");
        }
    }
}