<?php

namespace App\Http\Controllers\Manage\Finance;


use App\Http\Controllers\Manage\Finance\Transformers\TransformAccountRecordTransformer;
use App\Modules\Finance\Services\TransformAccountRecordService;
use Illuminate\Http\Request;


class TransformAccountRecordController extends BaseController
{
    /**
     * @apiDefine TransformAccountRecord 到账记录
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 添加到账记录
     * @api {POST} addTransformAccountRecord 添加到账记录
     * @param {fee} 金额
     * @param {day} 日期
     * @param {company_name} 公司名称
     * @param {trade_number} 流水号
     * @param {status} 状态(成功、失败)
     * @return mixed
     */
    function add_record(Request $request)
    {
        $fee = $request->has('fee') ? $request->input('fee') : '';
        $day = $request->has('day') ? $request->input('day') : '';
        $status = $request->has('status') ? $request->input('status') : '';
        $remark = $request->has('remark') ? $request->input('remark') : '';
        $account = $request->has('trade_number') ? $request->input('trade_number') : '';
        $company_name = $request->has('company_name') ? $request->input('company_name') : '';

        if (!$fee || !$day || !$company_name || !$account || !$status) {
            return response()->error('PARAM_EMPTY');
        } else {
            $result = TransformAccountRecordService::addRecord(compact('day', 'company_name', 'fee', 'account', 'remark', 'status'));
            if ($result) {
                return response()->success();
            } else {
                return response()->error("HANDLE_FAILED");
            }
        }
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 修改到账记录
     * @api {POST} updateTransformAccountRecord 修改到账记录
     * @param {id} id 必传
     * @param [fee] 金额  可不传
     * @param [day] 日期  可不传
     * @param [company_name] 公司名称  可不传
     * @param [trade_number] 流水号  可不传
     * @param [status] 状态(成功、失败)  可不传
     * @return mixed
     */
    function update_record(Request $request)
    {
        $id = $request->has('id') ? $request->input('id') : '';
        $fee = $request->has('fee') ? $request->input('fee') : '';
        $day = $request->has('day') ? $request->input('day') : '';
        $remark = $request->has('remark') ? $request->input('remark') : '';
        $status = $request->has('status') ? $request->input('status') : '';
        $company_name = $request->has('company_name') ? $request->input('company_name') : '';
        $account = $request->has('trade_number') ? $request->input('trade_number') : '';

        if ($id) {
            $result = TransformAccountRecordService::updateRecord($id, $fee, $day, $status, $company_name, $account, $remark);
            if ($result) {
                return response()->success();
            } else {
                return response()->error("HANDLE_FAILED");
            }
        } else {
            return response()->error('PARAM_EMPTY');
        }
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取到账记录
     * @api {GET} getTransformAccountRecord 获取到账记录
     * @param [start_time] 开始时间  可不传
     * @param [end_time] 结束时间  可不传
     * @param [company_name] 公司名称  可不传
     * @param [trade_number] 流水号 可不传
     * @param [status] 状态(成功、失败) 可不传
     * @return mixed
     */
    function get_record(Request $request)
    {
        $status = $request->has('status') ? $request->input('status') : '';
        $end_time = $request->has('end_time') ? $request->input('end_time') : '';
        $start_time = $request->has('start_time') ? $request->input('start_time') : '';
        $account = $request->has('trade_number') ? $request->input('trade_number') : '';
        $company_name = $request->has('company_name') ? $request->input('company_name') : '';
        $params = ['status' => $status, 'company_name' => $company_name, 'account' => $account, 'start_time' => $start_time, 'end_time' => $end_time];
        $data = TransformAccountRecordService::getRecord($params, false);
        return response()->pagination(new TransformAccountRecordTransformer(), $data);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 导出到账记录
     * @api {GET} exportTransformAccountRecord 导出到账记录
     * @param [start_time] 开始时间  可不传
     * @param [end_time] 结束时间  可不传
     * @param [company_name] 公司名称  可不传
     * @param [trade_number] 流水号  可不传
     * @param [status] 状态(成功、失败)  可不传
     * @return mixed
     */
    function export_record(Request $request)
    {
        $status = $request->has('status') ? $request->input('status') : '';
        $end_time = $request->has('end_time') ? $request->input('end_time') : '';
        $start_time = $request->has('start_time') ? $request->input('start_time') : '';
        $account = $request->has('trade_number') ? $request->input('trade_number') : '';
        $company_name = $request->has('company_name') ? $request->input('company_name') : '';
        $params = ['start_time' => $start_time, 'end_time' => $end_time, 'status' => $status, 'company_name' => $company_name, 'account' => $account];
        $data = TransformAccountRecordService::getRecord($params, true);

        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "到账记录" . date("YmdHis") . ".csv");

        echo iconv("UTF-8", "GBK", "\"id\",\"公司名称\",\"流水号\",\"金额\",\"日期\",\"创建时间\",\"最近修改时间\",\"备注\",\"状态\"\r\n");
        if ($data) {
            foreach ($data as $item) {
                echo("\"" . ($item->id ? $item->id : "-1") . "\",");
                echo("\"" . iconv("UTF-8", "GBK", ($item->company_name ? $item->company_name : "未填")) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", ($item->account ? $item->account : "未填")) . "\",");
                echo("\"" . ($item->fee ? $item->fee : "-1") . "\",");
                echo("\"" . $item->day ? $item->day : "未填" . "\",");
                echo("\"" . $item->created_at->toDateTimeString() . "\",");
                echo("\"" . $item->updated_at->toDateTimeString() . "\",");
                echo("\"" . iconv("UTF-8", "GBK", ($item->remark ? $item->remark : "未填")) . "\",");
                echo("\"" . iconv("UTF-8", "GBK", ($item->status ? $item->status : "未填")) . "\"\r\n");
            }
        }
        exit();
    }
}