<?php
/**
 *  经销商管理
 *  @author  system
 *  @version    1.0
 *  @date 2018-05-14 13:26:07
 *
 */
namespace App\Http\Controllers\Admin\Album;
use App\Http\Controllers\Admin\Controller;
use App\Models\AgentBannerModel;
use App\Models\AlbumAgentModel;
use App\Models\AlbumProductPriceModel;
use App\Models\AlbumUserModel;
use App\Repositories\Album\Criteria\AgentWhere;
use App\Services\OSS;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Album\Criteria\MultiWhere;
use App\Repositories\Album\AgentRepository;

class AgentController extends Controller
{
    private $repository;

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

    function index(Request $request) {
        $search['keyword'] = $request->input('keyword');
        $search['status'] = $request->input('status') ?? -1;
        $query = $this->repository->pushCriteria(new AgentWhere($search, $this->getStoreId()));

        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 = AlbumProductPriceModel::where([['agent_id',$item->id],['store_id',$this->getStoreId()]])->first();
            if(!empty($product)){
                $item->product_name = $product['mobile'];
            }

            $item->product_pic = $product['cover_pic'];
        }
        foreach ($list as $item){
            if($item->status == 0){
                $item->status = '待审核';
            }else{
                $item->status = '已审核';
            }
            $user = AlbumUserModel::where('id', $item->user_id)->first();
            $item->nickname = $user->username;
            $item->is_boss = $user->is_boss;
            $item->user_id = $user->id;
        }
        return view('admin.album.agent.index',compact('list'));
    }

    public function batch(Request $request)
    {
        $data = $request->input();
        $IdArr = $data['data'];
        foreach ($IdArr as $key => $val) {
            $check = AlbumUserModel::where([['id', $val]])->first();
            if ($check) {
                $check->is_boss = 1;
                $check->save();
            }
        }

        return response()->json(['code' => 0, 'message' => 'success', 'data' => []]);
    }


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

    /**
     * 保存修改
     */
    private function _createSave(){
        $data = (array) request('data');
        $id = $this->repository->create($data);
        if($id) {
            $url[] = array('url'=>U( 'Album/Agent/index'),'title'=>'返回列表');
            $url[] = array('url'=>U( 'Album/Agent/create'),'title'=>'继续添加');
            $this->showMessage('添加成功',$url);
        }else{
            $url[] = array('url'=>U( 'Album/Agent/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.agent.edit',compact('data'));
    }


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

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


    /**
     *
     * 状态改变
     *
     */
    public function status(Request $request) {
        $ok = $this->repository->updateStatus(request('id'),request('status'));
        if(request('status')==1){
            $agent = AlbumAgentModel::find(request('id'));
            $user = AlbumUserModel::find($agent->user_id);
            $user->is_dealer = 1;
            $user->save();
        }
        if($ok) {
            return $this->showMessage('操作成功');
        }else{
            return $this->showWarning('操作失败');
        }
    }

    /**
     * 删除
     */
    public function destroy(Request $request) {
        $agent = AlbumAgentModel::find($request->get('id'));
        $id= $agent->user_id;
        $ok = $agent->delete();
        $save['is_dealer'] = 0;
        $save['is_boss'] = 0;
        AlbumUserModel::where('id',$id)->update($save);
        if($ok) {
            return  $this->showMessage('操作成功');
        }else{
            return  $this->showWarning("操作失败");
        }
    }



}