<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/22
 * Time: 下午3:23
 */

namespace App\Http\Controllers\Channel\Finance;


use App\Http\Controllers\Channel\Finance\Transformers\PaymentDetailTransformer;
use App\Http\Controllers\Channel\Finance\Transformers\PaymentTransformer;
use App\Modules\Finance\Services\FinancialConfigService;
use App\Modules\Finance\Services\PaymentService;
use App\Modules\Finance\Services\WithdrawCashService;
use Illuminate\Http\Request;
use DB;

class PaymentController extends BaseController
{

    /**
     * @apiDefine Finance 结算提现模块
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 打款列表
     * @api {GET} payments 打款列表
     * @apiGroup Finance
     * @apiName payments
     * @apiParam   {Number}  withdraw_cash_id 提现id.
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiSuccess {Number}  id 打款 id.
     * @apiSuccess {Number}  withdraw_cash_id 提现 id.
     * @apiSuccess {Number}  amount 打款金额.
     * @apiSuccess {String}  remark 备注
     * @apiSuccess {String}  pay_time 打款时间
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *              "list": [
     *                      {
     *                          "id": 1,
     *                          "withdraw_cash_id": 1,
     *                          "amount": "1000.0000",
     *                          "remark": "dfsdfssd",
     *                          "pay_time": "2017-11-20 14:28:28"
     *                      }
     *                   ],
     *                     "meta": {
     *                           "total": 1,
     *                           "per_page": 15,
     *                           "current_page": 1,
     *                          "last_page": 1,
     *                          "next_page_url": "",
     *                          "prev_page_url": ""
     *                   }
     *              }
     *     }
     */
    function get_list(Request $request) {
        $withdraw_cash_id = $request->has('withdraw_cash_id') ? $request->input('withdraw_cash_id') : '';
        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd',strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd',strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

        $payments = PaymentService::getPaymentList($withdraw_cash_id, $start_time, $end_time);

        return response()->pagination(new PaymentTransformer(), $payments);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 打款列表详情
     * @api {GET} listPayments 打款列表详情
     * @apiGroup Finance
     * @apiName listPayments
     * @apiParam   {Number}  withdraw_cash_id 提现 id.
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiParam   {String}  [account_name] 账户名称
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {Number}  [status] 状态 查看接口 api/getWithdrawCashStatus (可不传,获取所有状态)
     *
     * @apiSuccess {Number}  id 打款 id.
     * @apiSuccess {Number}  withdraw_cash_id 提现 id.
     * @apiSuccess {Number}  amount 打款金额.
     * @apiSuccess {String}  remark 备注
     * @apiSuccess {String}  pay_time 打款时间
     * @apiSuccess {String}  status 打款状态
     * @apiSuccess {Number}  check_user_id 审核人ID
     * @apiSuccess {String}  check_user_name 审核人名称
     * @apiSuccess {String}  account_name 银行卡户主名称
     * @apiSuccess {String}  identity_card 银行卡户主身份证
     * @apiSuccess {String}  card_number 银行卡账号
     * @apiSuccess {String}  account_bank 银行支行
     * @apiSuccess {String}  bank 银行名称
     * @apiSuccess {String}  province 银行地址
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *              "list": [
     *                      {
     *                           "id": 1,
     *                           "withdraw_cash_id": 1,
     *                           "amount": "1000.0000",
     *                           "pay_time": "2017-11-20 14:28:28",
     *                           "status": "已打款",
     *                           "remark": "哈哈哈,有钱",
     *                           "check_user_id": 1,
     *                           "check_user_name": "张大妈",
     *                           "account_name": "宋晓",
     *                           "identity_card": "33038119930901821X",
     *                           "card_number": "6222520177654916",
     *                           "account_bank": "钱江支行",
     *                           "bank": "杭州银行",
     *                           "province": "北京市"
     *                      }
     *                   ],
     *                     "meta": {
     *                           "total": 1,
     *                           "per_page": 15,
     *                           "current_page": 1,
     *                          "last_page": 1,
     *                          "next_page_url": "",
     *                          "prev_page_url": ""
     *                   }
     *              }
     *     }
     */
    function get_listDetail(Request $request) {
        $distribution_channel_id = $this->getChannelId();
        $distribution_channel_name = $this->getChannelName();
        $distribution_channel_name = '';

        $withdraw_cash_id = $request->has('withdraw_cash_id') ? $request->input('withdraw_cash_id') : '';
        $start_time = $request->has('start_time') && !empty($request->input('start_time')) ? date('Ymd',strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') && !empty($request->input('end_time')) ? date('Ymd',strtotime($request->input('end_time'))) : '';

        $account_name = $request->has('account_name') ? $request->input('account_name') : '';
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';
        $status = $request->has('status') ? $request->input('status') : '';

        $params = [
            'withdraw_cash_id'=>$withdraw_cash_id,
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'start_date'=>$start_time,
            'end_date'=>$end_time,
            'account_name'=>$account_name,
            'search_name'=>$search_name,
        ];
        $payments = PaymentService::getPaymentDetailList($params, $status);

        return response()->pagination(new PaymentDetailTransformer(), $payments);
    }
}