<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Services\LogService;
use App\Http\Services\PurchaseRemarkService;
use App\Http\Transformers\PurchaseRemarkTransformer;
use App\Model\LogModel;
use App\Model\PurchaseRemarkModel;
use Carbon\Carbon;
use Illuminate\Http\Request;

class PurchaseRemarkApiController extends Controller
{
    public function Entrance(Request $request, $id)
    {
        $this->$id($request, $id);
    }

    //获取供应商收款信息
    public function GetPurchaseRemarkList($request)
    {
        $supplierId = $request->get('supplier_id');
        $limit = $request->get('limit', 10);
        $model = new PurchaseRemarkModel();
        $model->where('supplier_id', $supplierId)->paginate();
        $list = $model->where('supplier_id', $supplierId)->orderBy('id', 'desc')
            ->paginate($limit)->toArray();
        $transformer = new PurchaseRemarkTransformer();
        $list['data'] = $transformer->transformList($list['data']);
        $this->response(0, 'ok', $list['data'], $list['total']);
    }

    //保存附件信息
    public function SavePurchaseRemark($request)
    {

        $id = $request->input('id');
        $participateContent = $request->input('participate_content');
        $startTime = $request->input('start_time');
        $endTime = $request->input('end_time');
        $supplierId = $request->input('supplier_id');
        $remark = $request->input('remark');
        if (!$supplierId) {
            $this->response(-1, '供应商id不能为空');
        }
        if ($request->input('participate_type') != 1) {
            if (!$participateContent) {
                $this->response(-1, '参与内容不能为空');
            }
        }

        if (!$startTime || !$endTime) {
            $this->response(-1, '开始和结束时间都不能为空');
        }

        if (!$remark) {
            $this->response(-1, '备注内容不能为空');
        }

        $remark = $request->only([
            'id',
            'supplier_id',
            'participate_type',
            'participate_content',
            'start_time',
            'end_time',
            'remark',
        ]);
        $remark['start_time'] = strtotime($remark['start_time']);
        if ($remark['start_time'] < Carbon::now()->startOfDay()->timestamp) {
            $this->response(-1, '开始时间不能低于当前日期');
        }
        $remark['end_time'] = strtotime($remark['end_time']);
        if ($remark['start_time'] > $remark['end_time'] || ($remark['start_time'] == $remark['end_time'])) {
            $this->response(-1, '结束时间要大于开始时间,并且间隔要大于1天');
        }
        $remarkService = new PurchaseRemarkService();
        $result = $remarkService->savePurchaseRemark($id, $remark);
        if (!$result) {
            $this->response(-1, '操作失败');
        } else {
            $logService = new LogService();
            $content = !empty($remark['id']) ? '修改采购备注' : '添加采购备注';
            $supplierId = $remark['supplier_id'];
            $remark = json_encode($remark);
            $logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, $content, $content, $remark);
            $this->response(0, '保存成功');
        }
    }


    //删除
    public function DeletePurchaseRemark($request)
    {
        $id = $request->get('id');
        $model = new PurchaseRemarkModel();
        $remark = $model->where('id', $id)->first()->toArray();
        $result = $model->where('id', $id)->delete();
        if ($result) {
            $logService = new LogService();
            $content = "删除采购备注";
            $logService->AddLog($remark['supplier_id'], LogModel::UPDATE_OPERATE, '删除采购备注', $content);
            $this->response(0, '操作成功');
        }
        $this->response(-1, '操作失败');
    }
}