<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/20
 * Time: 16:17
 */

namespace App\Http\Controllers\Manage\SendOrder;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Manage\SendOrder\Transformers\HeadlinelTransformer;
use App\Modules\Promotion\Models\Headline;
use App\Modules\Promotion\Services\PromotionService;
use DB;
use Illuminate\Http\Request;

class HeadlineController extends Controller
{
    /**
     *  标题
     */

    /**
     * @api {post} SendOrder/uploadHeadlins 导入标题
     * @apiParam  {File}  [headlines]  标题文件
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":[]
     *     }
     */
    function uploadHeadlins(Request $request)
    {
        if (!$request->hasFile('file')) {
            return response()->error('PARAM_EMPTY');
        }

        $file = $request->file('file');
        $currentIndex = 0;
        if (null != $file) {
            $file_name = date('YmdHis') . '.csv';
            $path = $request->file->storeAs('file', $file_name);
            $path = storage_path('app/' . $path);
            $file = fopen($path, "r");
            $serial_number = Headline::getMaxSerialNumber();
            while ($data = fgetcsv($file)) {
                if ($currentIndex > 0) {
                    \Log::info($data);
                    $title = mb_convert_encoding(trim($data[0]), "UTF-8", "GBK");
                    $category = mb_convert_encoding(trim($data[1]), "UTF-8", "GBK");
                    $remark = mb_convert_encoding(trim($data[2]), "UTF-8", "GBK");
                    $type = mb_convert_encoding(trim($data[3]), "UTF-8", "GBK");
                    $quality = mb_convert_encoding(trim($data[4]), "UTF-8", "GBK");
                    $serial_number = $serial_number + 1;
                    if (!$title || !$remark || !$category || !$serial_number || !$quality || !$type) {
                        return response()->error('PARAM_EMPTY');
                    }
                    Headline::addHeadline(compact('title', 'category', 'remark', 'type', 'quality', 'serial_number'));
                }
                $currentIndex++;
            }
            return response()->success();
        }
    }

    /**
     * @api {post} SendOrder/deleteHeadlines 删除标题
     * @apiParam  {Number}  [id]  标题id(可不传，多个之间用逗号隔开)
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":[]
     *     }
     */
    function deleteHeadlines(Request $request)
    {
        $id = $request->has('id') ? $request->input('id') : '';
        if (empty($id)) {
            return response()->error("PARAM_EMPTY");
        }
        $ids = explode(',', $id);
        $result = Headline::delHeadlines($ids);
        if ($result) {
            return response()->success();
        }
    }

    /**
     * @api {GET} sendOrder/getHeadlines 获取标题
     * @apiParam  {Number}  [id]  标题id(可不传)
     * @apiParam  {String}  [remark]  标题的备注(可不传)
     * @apiParam  {String}  [quality]  质量(可不传)
     * @apiParam  {Number}  [type]  类型(1: 推文标题， 2：客服专用)(可不传)
     *
     * @apiSuccess {Number}  id 标题id.
     * @apiSuccess {String}  title 标题内容id
     * @apiSuccess {String}  category 类型(男频、女频)
     * @apiSuccess {String}  remark 备注
     * @apiSuccess {Number}  serial_number 序号
     * @apiSuccess {String}  updated_at 更新时间
     * @apiSuccess {String}  created_at 创建时间
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": [
     *           {
     *               "id": 5,
     *               "title": "标题111",
     *               "category": "男频",
     *               "remark": "1",
     *               "serial_number": 11111,
     *               "updated_at": "2018-08-22 14:14:20",
     *               "created_at": "2018-08-22 14:14:20"
     *            },
     *           {
     *                "id": 6,
     *               "title": "标题2222",
     *               "category": "女频",
     *               "remark": "2",
     *               "serial_number": 111111,
     *               "updated_at": "2018-08-22 14:14:20",
     *               "created_at": "2018-08-22 14:14:20"
     *            }
     *        ],
     *          "meta": {
     *                 "total": 1,
     *                 "per_page": 15,
     *                 "current_page": 1,
     *                 "last_page": 1,
     *                 "next_page_url": "",
     *                 "prev_page_url": ""
     *             }
     *     }
     */
    function getHeadlines(Request $request)
    {
        $id = $request->has('id') ? $request->input('id') : '';
        $remark = $request->has('remark') ? $request->input('remark') : '';
        $title = $request->has('title') ? $request->input('title') : '';
        $type = $request->has('type') ? $request->input('type') : '';
        $category = $request->has('category') ? $request->input('category') : '';
        $quality = $request->has('quality') ? $request->input('quality') : '';

        $param = [];
        if ($id) {
            $param['id'] = $id;
        }

        if ($remark) {
            $param['remark'] = $remark;
        }

        if ($title){
            $param['title'] = $title;
        }

        if ($type) {
            $param['type'] = $type;
        }

        if ($category) {
            $param['category'] = $category;
        }

        if ($quality) {
            $param['quality'] = $quality;
        }
        $result = PromotionService::getHeadlineByParams($param);
        return response()->pagination(new HeadlinelTransformer(), $result);
    }
}