<?php

namespace App\Web\Controllers;

use App\Http\Controllers\BaseController;
use App\Web\Services\ProductService;
use Illuminate\Http\Request;


class ProductController extends BaseController
{

    protected $service = null;

    /**
     * 构造函数
     * ProductController constructor.
     * @param ProductService $service
     */
    public function __construct(ProductService $service)
    {
        $this->service = $service;
    }

    /**
     * 获取产品类型列表
     * */
    public function getProductTypeList(Request $request){
        $params=$request->only(['page_size','page','status','keyword']);
        $ret=$this->service->getProductTypeList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    public function getTreeProductList(Request $request){
        $ret=$this->service->getTreeProductList([]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    public function sortProductTypes(Request $request){
        $types=$request->input('types',[]);
        $ret=$this->service->sortTypes($types);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 产品类型保存
     * */
    public function saveProductType(Request $request){
        $this->validate($request, [
            'type_name' => 'required',
        ], [
            'type_name.required' => '类型名称不能为空',
        ]);
        $params=$request->only(['id','type_name','description','status','p_id','sort','seo_data', 'seo_id','expand_content']);
        $ret=$this->service->saveProductType($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 变更产品类型状态
     * */
    public function changeProductTypeStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveProductType($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 保存产品类型关系
     * */
    public function typeRelationSave(Request $request){
        $this->validate($request, [
            'product_id' => 'required',
        ], [
            'product_id.required' => '产品id不能为空',
        ]);
        $params=$request->only(['product_id','type_ids']);
        $ret=$this->service->typeRelationSave($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据产品类型 保存产品关系
     * */
    public function addRelationByTypeId(Request $request){
        $this->validate($request, [
            'product_ids' => 'required',
            'type_id' => 'required',
        ], [
            'product_ids.required' => '产品id不能为空',
            'type_id.required' => '产品类型id不能为空',
        ]);
        $typeId=$request->input('type_id',0);
        $productIds=$request->input('product_ids',[]);
        $ret=$this->service->addRelationByTypeId($typeId,$productIds);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据类型id及产品id 删除关联关系
     * */
    public function delTypeRelation(Request $request){
        $this->validate($request, [
            'product_id' => 'required',
            'type_id' => 'required',
        ], [
            'product_id.required' => '产品id不能为空',
            'type_id.required' => '类型id不能未空',
        ]);
        $productId=$request->input('product_id','');
        $typeId=$request->input('type_id','');
        $ret=$this->service->delTypeRelation($typeId,$productId);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 获取产品标签列表
     * */
    public function getProductTagList(Request $request){
        $params=$request->only(['page_size','page','status','keyword']);
        $ret=$this->service->getProductTagList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 产品标签保存
     * */
    public function saveProductTag(Request $request){
        $this->validate($request, [
            'tag_name' => 'required',
        ], [
            'tag_name.required' => '类型名称不能为空',
        ]);
        $params=$request->only(['id','tag_name','is_hot','status','sort','type','seo_id','seo_data']);
        $ret=$this->service->saveProductTag($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 变更产品类型状态
     * */
    public function changeProductTagStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveProductTag($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 保存产品标签关系
     * */
    public function tagRelationSave(Request $request){
        $this->validate($request, [
            'product_id' => 'required',
        ], [
            'product_id.required' => '产品id不能为空',
        ]);
        $params=$request->only(['product_id','tag_ids']);
        $ret=$this->service->tagRelationSave($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据产品标签 保存产品关系
     * */
    public function addRelationByTagId(Request $request){
        $this->validate($request, [
            'product_ids' => 'required',
            'tag_id' => 'required',
        ], [
            'product_ids.required' => '产品id不能为空',
            'tag_id.required' => '产品标签id不能为空',
        ]);
        $tagId=$request->input('tag_id',0);
        $productIds=$request->input('product_ids',[]);
        $ret=$this->service->addRelationByTagId($tagId,$productIds);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 根据标签id及产品id 删除关联关系
     * */
    public function delTagRelation(Request $request){
        $this->validate($request, [
            'product_id' => 'required',
            'tag_id' => 'required',
        ], [
            'product_id.required' => '产品id不能为空',
            'tag_id.required' => '标签id不能未空',
        ]);
        $productId=$request->input('product_id','');
        $tagId=$request->input('tag_id','');
        $ret=$this->service->delTagRelation($tagId,$productId);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取产品板块列表
     * */
    public function getProductPlateList(Request $request){
        $params=$request->only(['page_size','page','status','keyword']);
        $ret=$this->service->getProductPlateList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 产品板块保存
     * */
    public function saveProductPlate(Request $request){
        $this->validate($request, [
            'plate_name' => 'required',
        ], [
            'plate_name.required' => '类型名称不能为空',
        ]);
        $params=$request->only(['id','plate_name','status','sort','seo_id','seo_data']);
        $ret=$this->service->saveProductPlate($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 变更产品板块状态
     * */
    public function changeProductPlateStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveProductPlate($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 获取产品列表
     * */
    public function getProductList(Request $request){
        $params=$request->only(['page_size','page','status','keyword','sort']);
        $ret=$this->service->getProductList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取产品详情
     * */
    public function getProductInfo(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => '产品id不能为空',
        ]);
        $id=$request->input('id',0);
        $ret=$this->service->getProductInfo(['id'=>$id]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取产品分类未关联的产品
     * */
    public function getUnrelatedProductByTypeIds(Request $request){
        $this->validate($request, [
            'type_id' => 'required',
        ], [
            'type_id.required' => 'id不能为空',
        ]);
        $params=$request->only(['page_size','page','status','keyword','type_id']);
        $ret=$this->service->getUnrelatedProductByTypeIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 获取产品标签未关联的产品
     * */
    public function getUnrelatedProductByTagIds(Request $request){
        $params=$request->only(['page_size','page','status','keyword','tag_id','exclude_tag_id']);
        $ret=$this->service->getUnrelatedProductByTagIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据类型id 获取产品列表
     * */
    public function getProductListByType(Request $request){
        $params=$request->only(['page_size','page','status','keyword','type_id','is_admin']);
        $ret=$this->service->getProductByTypeIds($request);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }
    /**
     * 根据类型id 获取产品列表
     * */
    public function getAdminProductListByType(Request $request){
        $params=$request->only(['page_size','page','status','keyword','type_id','is_admin']);
        $ret=$this->service->getAdminProductByTypeIds($request);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }
    /**
     * 根据标签id 获取产品列表
     * */
    public function getProductListByTag(Request $request){
        $params=$request->only(['page_size','page','status','keyword','tag_id']);
        $ret=$this->service->getProductByTagIds($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }
    public function changeProductStatus(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $params=$request->only(['id','status']);
        $ret=$this->service->saveProduct($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }
    /**
     * 保存产品数据
     * */
    public function saveProduct(Request $request){
        $this->validate($request, [
            'title' => 'required',
            'content' => 'string',
            'type_ids' => 'array',
            'tag_ids' => 'array',
        ]);
        $params = $request->only(['id','pub_date','title','content','brand','model_nb','package','spec','description',
            'image_url',  'image_alt',  'main_image',  'main_image_alt','sub_image_urls','gallery_imgs','size_imgs','re_price','market_price',
            'sku','virtual_view','download_url','seo_id','sort','seo_data','score','status','tag_ids','type_ids', 'is_top','expand_content']);
        $ret=$this->service->saveProduct($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }



    /**
     * 产品推荐设置
     * */
    public function recommendSet(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ]);
        $params = $request->only(['id','is_recommend']);
        $ret=$this->service->recommendSet($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }


    /**
     * 更新产品的浏览量
     * */
    public function upProductPv(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $productId=$request->input('id',0);
        $incValue=$request->input('inc_val',1);
        $ret=$this->service->upProductPv($productId,$incValue);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 产品预览
     * */
    public function previewProduct(Request $request){
        $this->validate($request, [
            'id' => 'required',
        ], [
            'id.required' => 'id不能为空',
        ]);
        $productId=$request->input('id',0);
        $ret=$this->service->getProductRenderData(['id'=>$productId]);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据类型id获取产品
     * */
    public function getPublishProductListByTypeId(Request $request){
        $params=$request->only(['page_size','page','type_id']);
        $ret=$this->service->getPublishProductListByTypeId($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 根据标签id获取产品
     * */
    public function getPublishProductListByTagId(Request $request){
        $params=$request->only(['page_size','page','tag_id']);
        $ret=$this->service->getPublishProductListByTagId($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 搜索获取已发布的产品
     * */
    public function getSearchProductList(Request $request){
        $params=$request->only(['page_size','keyword']);
        $ret=$this->service->getPublishProductList($params);
        return $this->jsonResponse(
            'ok',
            $ret
        );
    }

    /**
     * 新闻排序上下移
     * @url /api/product/change-sort
     */
    public function changeProductSort(Request $request)
    {
        $this->validate($request, [
            'id' => 'required',
            'sort' => 'required',
        ]);
        $ret = $this->service->changeProductSort($request->input('id'), $request->input('sort'));
        return $this->jsonResponse('ok', $ret);
    }

    public function productSync(Request $request)
    {
        $this->validate($request, [
            'list' => 'required',
            'user_id' => 'required'
        ]);
        $params = $request->only(['list','user_id','product_model_data','product_type_data','product_tag_data', 'is_replace_oss']);
        $rst = $this->service->productCenterSync($params);
        return $this->jsonResponse('ok', $rst);
    }

    // 批量修改分类
    public function typePluralEdit(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret= $this->service->typePluralEdit($request->all());
        return $this->jsonResponse('ok', $ret);
    }
    // 批量修改标签
    public function tagPluralEdit(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret= $this->service->tagPluralEdit($request->all());
        return $this->jsonResponse('ok', $ret);
    }
    // 批量删除
    public function batchDeleteProduct(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret= $this->service->batchDelete($request->all(),2);
        return $this->jsonResponse('ok', $ret);
    }
    // 批量修改状态
    public function batchStatusProduct(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required',
        ]);
        $ret= $this->service->batchStatus($request->all());
        return $this->jsonResponse('ok', $ret);
    }
}
