<?php
/**
 *  抵扣卡列表
 *  @author  system
 *  @version    1.0
 *  @date 2018-07-12 08:35:58
 *
 */
namespace App\Http\Controllers\Admin\Card;
use App\Http\Controllers\Admin\Controller;
use App\Models\CardInfoModel;
use App\Models\ProductCategoryModel;
use App\Models\SettingInfoModel;
use EasyWeChat\Factory;
use Illuminate\Http\Request;
use App\Repositories\Base\Criteria\OrderBy;
use App\Repositories\Card\Criteria\MultiWhere;
use App\Repositories\Card\InfoRepository;

class InfoController extends Controller
{
    private $repository;

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

    function index(Request $request) {
        $search['keyword'] = $request->input('keyword');
        $query = $this->repository->pushCriteria(new MultiWhere($search));

        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('status','ASC'));
        }
        $list = $query->paginate(10);
        $product = ProductCategoryModel::all();
        return view('admin.card.info.index',compact('list','product'));
    }


    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.card.info.check',compact('list'));
    }


    
    /**
     * 
     * 修改
     * 
     * 
     */
    public function update(Request $request) {
        if($request->method() == 'POST') {
            return $this->_updateSave();
        }
        $data = $this->repository->find($request->get('id'));
        return view('admin.card.info.edit',compact('data'));
    }

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

    public function view(Request $request) {
        $data = $this->repository->find(request('id'));
        return view('admin.card.info.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) {
        $bool = $this->repository->destroy($request->get('id'));
        if($bool) {
            return  $this->showMessage('操作成功');
        }else{
            return  $this->showWarning("操作失败");
        }
    }

    public function getQrcode($id)
    {
        $this->setting = SettingInfoModel::first();

        $config = [
            'app_id' => $this->setting->app_id,
            'secret' => $this->setting->app_secret,

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ];


        $app = Factory::miniProgram($config);
        $response = $app->app_code->getQrCode('pages/mine/duihuanka?card_no='.$id);
        $path = public_path() . '/download';
        $qrcode = $response->save($path, $id.'.jpg');
        if ($qrcode) {
            $qrcode = env('APP_URL') . '/download/'. $id .'.jpg';
            $card = CardInfoModel::where('card_no',$id)->first();
            $card->qrcode = $qrcode;
            $card->save();
            return true;
        } else {
            return false;
        }
    }

    public function store(Request $request)
    {
        $data = (array) request('data');
        $product_id =(array) request('product_id');
        $data['product_id'] = implode(',',$product_id);

        $number = $request->count;

        for ($i =0 ;$i<$number;$i++){
            $data['card_no'] = time() . $i;
            $this->repository->create($data);
            $this->getQrcode($data['card_no']);
        }

        return back();
    }
}