<?php

namespace App\Http\Controllers\Manage\Book;

use App\Modules\Book\Services\ProductService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class ProductController extends Controller
{
    /**
     * @apiDefine Product 产品/充值
     */


    /**
     * @apiVersion 1.0.0
     * @apiDescription 充值列表
     * @api {get} product/chargelists 充值列表
     * @apiGroup Product
     * @apiName getChargeLists
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:  [
     *                   {
     *                       product_id: 1,
     *                       price: "30.00",
     *                       given: 0,
     *                       is_default: 0,
     *                       type: "TICKET_RECHARGE"
     *                   },
     *                   {
     *                       product_id: 4,
     *                       price: "500.00",
     *                       given: 20000,
     *                       is_default: 0,
     *                       type: "TICKET_RECHARGE"
     *                   },
     *                   {
     *                       product_id: 5,
     *                       price: "365.00",
     *                       given: 0,
     *                       is_default: 1,
     *                       type: "TICKET_RECHARGE"
     *                   }
     *               ]
     *       }
     */
    public function getChargeLists(){
        $res = ProductService::getChargeProduct();
        return  response()->collection(new ProductTransformer,$res);
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 添加产品
     * @api {post} product 添加产品
     * @apiGroup   Product
     * @apiName createProduct
     * @apiParam   {float}     price    价格
     * @apiParam   {String}    type     类型
     * @apiParam   {int}       given    奖励
     * @apiParam   {int}       is_enabled    是否启用
     * @apiParam   {int}       [sequence]    排序
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:
     *                   {
     *                       product_id: 1,
     *                       price: "30.00",
     *                       given: 0,
     *                       is_default: 0,
     *                       type: "TICKET_RECHARGE"
     *                   }
     *
     *       }
     */
    public function createProduct(Request $request){
        $param = $request->except('_url');
        if(checkParam($param,['price','type','given','is_enabled'])){
            return response()->error('PARAM_ERROR');
        }
        $param['is_default'] = 0;
        $res = ProductService::addProduct($param);
        return response()->item(new ProductTransformer,$res);
    }


    /**
     * @apiVersion 1.0.0
     * @apiDescription 编辑产品
     * @api {post} product/update 编辑产品
     * @apiGroup Product
     * @apiName updateProduct
     * @apiParam   {int}       is_default(1|0)    是否默认(只能有一个)
     * @apiParam   {int}       is_enabled(1|0)   是否启用
     * @apiSuccess {int}         code 状态码
     * @apiSuccess {String}      msg  信息
     * @apiSuccess {object}      data 结果集
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:  [
     *                   {
     *                       product_id: 1,
     *                       price: "30.00",
     *                       given: 0,
     *                       is_default: 0,
     *                       type: "TICKET_RECHARGE"
     *                   },
     *                   {
     *                       product_id: 4,
     *                       price: "500.00",
     *                       given: 20000,
     *                       is_default: 0,
     *                       type: "TICKET_RECHARGE"
     *                   },
     *                   {
     *                       product_id: 5,
     *                       price: "365.00",
     *                       given: 0,
     *                       is_default: 1,
     *                       type: "TICKET_RECHARGE"
     *                   }
     *               ]
     *       }
     */
    public function updateProduct(Request $request){
        $id = $request->input('product_id');
        if(empty($id)) return response()->error('PARAM_ERROR');

        $default = $request->input('is_default');
        $is_enabled = $request->input('is_enabled');

        if($default == '' && $is_enabled == ''){
            return response()->error('PARAM_ERROR');
        }

        ProductService::updateProductDefault($id, $default);

        ProductService::updateProductEnabled($id, $is_enabled);

        return response()->success();

    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 根据id获取产品
     * @api {get} product/{id} 根据id获取产品
     * @apiGroup   Product
     * @apiName getProductInfoById
     * @apiSuccess   {int}         code 状态码
     * @apiSuccess   {String}      msg  信息
     * @apiSuccess   {object}      data 结果集
     * @apiSuccess   {Int}       product_id    product_id
     * @apiSuccess   {float}       price    价格
     * @apiSuccess   {String}      type     类型
     * @apiSuccess   {int}         given    奖励
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *       code: 0,
     *       msg: "",
     *       data:
     *                   {
     *                       product_id: 1,
     *                       price: "30.00",
     *                       given: 0,
     *                       is_default: 0,
     *                       type: "TICKET_RECHARGE"
     *                   }
     *
     *       }
     */
    public function getProductInfoById(Request $request,$id){
        $res = ProductService::getProductSingle($id);
        return response()->item(new ProductTransformer,$res);
    }
}
