<?php
declare(strict_types=1);

namespace app\finance\controller;

use app\base\BaseController;
use app\finance\model\CreditWriteoff as CreditWriteoffModel;
use app\finance\validate\CreditWriteoffValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Creditwriteoff extends BaseController
{
    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new CreditWriteoffModel();
    }

    public function datalist()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $uid      = $this->uid;
            $where    = [];
            $whereOr  = [];
            $where[]  = ['delete_time', '=', 0];

            $tab = isset($param['tab']) ? (int) $param['tab'] : 0;
            if ($tab === 0) {
                $whereOr[] = ['admin_id', '=', $uid];
                $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_uids)")];
                $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_history_uids)")];
                $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_copy_uids)")];
            } elseif ($tab === 1) {
                $where[] = ['admin_id', '=', $uid];
            } elseif ($tab === 2) {
                $where[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_uids)")];
            } elseif ($tab === 3) {
                $where[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_history_uids)")];
            } elseif ($tab === 4) {
                $where[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_copy_uids)")];
            }

            if (!empty($param['keywords'])) {
                $where[] = ['customer_id', 'like', '%' . $param['keywords'] . '%'];
            }
            if (isset($param['check_status']) && $param['check_status'] !== '') {
                $where[] = ['check_status', '=', $param['check_status']];
            }
            if (!empty($param['diff_time'])) {
                $diff = explode('~', $param['diff_time']);
                $where[] = ['create_time', 'between', [strtotime(urldecode($diff[0])), strtotime(urldecode($diff[1] . ' 23:59:59'))]];
            }

            $list = $this->model->datalist($param, $where, $whereOr);
            return table_assign(0, '', $list);
        }
        return view();
    }

    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if (!empty($param['id']) && $param['id'] > 0) {
                try {
                    validate(CreditWriteoffValidate::class)->scene('edit')->check($param);
                } catch (ValidateException $e) {
                    return to_assign(1, $e->getError());
                }
                $this->model->edit($param);
            } else {
                try {
                    validate(CreditWriteoffValidate::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    return to_assign(1, $e->getError());
                }
                $param['admin_id'] = $this->uid;
                $param['did']      = $this->did;
                $this->model->add($param);
            }
        } else {
            $id = isset($param['id']) ? (int) $param['id'] : 0;
            $detail = [
                'id'              => 0,
                'customer_id'     => '',
                'trade_scene'     => '',
                'daily_trade_vol' => '',
                'credit_amount'   => '',
                'return_date'     => '',
                'remark'          => '',
                'file_ids'        => '',
                'check_status'    => 0,
                'check_flow_id'   => 0,
            ];
            if ($id > 0) {
                $detail = $this->model->getById($id);
            }
            View::assign('detail', $detail);
            return view();
        }
    }

    public function view($id)
    {
        $detail = $this->model->getById($id);
        if (!empty($detail)) {
            View::assign('create_user', get_admin($detail['admin_id']));
            View::assign('detail', $detail);
            return view();
        }
        return view(EEEOR_REPORTING, ['code' => 404, 'warning' => '找不到页面']);
    }

    public function del()
    {
        $param = get_params();
        $id    = isset($param['id']) ? (int) $param['id'] : 0;
        if (request()->isDelete()) {
            return $this->model->delById($id);
        }
        return to_assign(1, '错误的请求');
    }
}
