<?php


namespace App\Http\Transformers;


use App\Http\Services\LogService;
use App\Model\SupplierReceiptModel;

class LogTransformer
{
    public function transformList($list)
    {
        foreach ($list as &$item) {
            $item = $this->transformInfo($item);
            unset($item['remark']);
        }
        unset($item);
        return $list;
    }

    public function transformInfo($item)
    {
        $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
        $item['type'] = array_get(config('fixed.LogType'), $item['type']);
        $logService = new LogService();
        //如果action是修改供应商,就要做对比处理
        if ($item['action'] === '修改供应商基本资料') {
            if ($item['content'] === '修改供应商') {
                $remark = json_decode($item['remark'], true);
                $oldSupplier = array_get($remark, 'old_supplier');
                $newSupplier = array_get($remark, 'new_supplier');
                $content = $logService->compareSupplierChange($newSupplier, $oldSupplier);
                $item['content'] = $content;
            }

            //如果是和银行信息有关的,还要把具体的信息展示出来
            if (is_string($item['content'])) {
                if (strpos($item['content'], '银行信息') !== FALSE) {
                    if (!empty($item['remark'])) {
                        //获取当前的信息
                        $oldReceipt = json_decode($item['remark'], true);
                        //新增
                        if (empty($oldReceipt['receipt_id'])) {
                            $type = array_get($oldReceipt, 'receipt_type', 1);
                            $type = $type == 1 ? "大陆" : "香港以及国外";
                            $swiftCodeText = ($type =='香港以及国外') ? ' | 国际代码 : ' . array_get($oldReceipt, 'swift_code') : '';
                            $item['content'] .= ' : ' . "银行所在地 : {$type} | 银行名称 : {$oldReceipt['bank_name']} | 账户号码 : {$oldReceipt['account_no']} | 银行地址 : {$oldReceipt['account_adderss']}" . $swiftCodeText;
                        } else {
                            $newReceipt = SupplierReceiptModel::where('receipt_id', $oldReceipt['receipt_id'])->first();
                            if (!empty($newReceipt)) {
                                $newReceipt = $newReceipt->toArray();
                                $type = array_get($newReceipt, 'receipt_type', 1);
                                $type = $type == 1 ? "大陆" : "香港以及国外";
                                $swiftCodeText = ($type =='香港以及国外') ? ' | 国际代码 : ' . array_get($newReceipt, 'swift_code') : '';
                                $item['content'] .= ' : ' . "银行所在地 : {$type} | 银行名称 : {$newReceipt['bank_name']} | 账户号码 : {$newReceipt['account_no']} | 银行地址 : {$newReceipt['account_adderss']}".$swiftCodeText;
                            }

                        }
                    }

                }
            }


            if (!is_array($item['content']) && strpos($item['content'], '联系') !== false) {
                $item['content'] = $item['content'] . ':' . $logService->getContactDesc($item['remark']);
            }
            $item['content'] = $item['content'] ?: '没有内容变化';
        }
        return $item;
    }


    public static function transformReceiptLog($oldReceipt, $receipt)
    {
        if (empty($oldReceipt['receipt_id'])) {
            //新增日志
            $content = "银行名称 : {$oldReceipt['bank_name']} | 账户号码 : {$oldReceipt['account_no']}";
        } else {
            if ($receipt) {
                //修改日志

            } else {
                //删除日志
            }
        }


    }

}