<?php
/**
 * @package merchant
 * @Author: Qinii
 * @Date: 2020/5/27
 */

namespace app\controller\api\store\product;

use app\common\repositories\store\order\StoreOrderProductRepository;
use app\validate\api\ProductReplyValidate;
use think\App;
use crmeb\basic\BaseController;
use app\common\repositories\store\product\ProductReplyRepository as repository;

class StoreReply extends BaseController
{
    /**
     * @var repository
     */
    protected $repository;
    protected $userInfo;
    protected $merId;
    protected $source;
    /**
     * StoreProduct constructor.
     * @param App $app
     * @param repository $repository
     */
    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
        $this->userInfo = $this->request->isLogin() ? $this->request->userInfo() : null;
        $this->source = $this->request->header('source');
        $this->merId =  $this->request->header('merId');
    }

    /**
     * 获取评论列表
     * mer_id 商铺id
     * @Author:Qinii
     * @Date: 2020/5/28
     * @return mixed
     */
    public function lst($id)
    {
        [$page, $limit] = $this->getPage();
        $where = $this->request->params(['product_id','type']);
        $mer_id= $this->request->params(['mer_id']);
        $where['product_id'] = $id;
        return app('json')->success($this->repository->getApiList($where,$page, $limit,$mer_id['mer_id']));
    }

    public function product(StoreOrderProductRepository $orderProductRepository)
    {
        $id =  $mer_id= $this->request->params(['id']);
        $orderProduct = $orderProductRepository->userOrderProducts($id['id'], $this->request->uid());

        foreach($orderProduct as $item){
        if (!$item || !$item->orderInfo)
            return app('json')->fail('订单不存在');
        if ($item->is_reply)
            return app('json')->fail('该商品已评价');
        }
        return app('json')->success($orderProduct->toArray());
    }

    public function reply(ProductReplyValidate $validate)
    {

        $id =  $mer_id= $this->request->params(['id']);
        $id= explode(",",$id['id']) ;
        foreach($id as $item){
            $data = $this->request->params(['comment', 'product_score', 'service_score', 'postage_score', ['pics', []]]);
            $validate->check($data);
            $user = $this->request->userInfo();
            $data['uid'] = $this->request->uid();
            $data['order_product_id'] = (int)$item;
            $data['nickname'] = $user['nickname'];
            $data['avatar'] = $user['avatar'];
            $this->repository->create($data);
        }
        return app('json')->success('评价成功');
    }

    /**
     * 删除评论
     */
    public function delreply($id){
        $id= $this->request->params(['id']);
        return app('json')->success($this->repository->delreply($id['id']));
    }


    /*评论列表*/
    public function list(){
        $mer_id= $this->request->params(['mer_id']);
        if(!empty($mer_id['mer_id'])){
            $where['mer_id']=$mer_id['mer_id'];
        }else if($this->source=='yhc'){
            $where['mer_id']=$this->merId;
        }

        $where['uid'] =$this->request->uid();
        $where['is_virtual'] = 0 ;
        $where['is_del'] = 0 ;
        [$page, $limit] = $this->getPage();
        $data = $this->repository->getReplyList($where,$page, $limit);
        return app('json')->success($data);
    }
}
