<?php
declare(strict_types=1);

namespace app\finance\controller;

use app\base\BaseController;
use app\finance\model\PriceAdjustCate as PriceAdjustCateModel;
use think\facade\View;

class Priceadjustcate extends BaseController
{
    protected $model;

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

    public function datalist()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $type  = isset($param['type']) ? (int) $param['type'] : 0;
            $where = [['delete_time', '=', 0]];
            if ($type > 0) {
                $where[] = ['type', '=', $type];
            }
            $list = $this->model->where($where)->order('sort asc, id asc')->select();
            return to_assign(0, '', $list);
        }
        return view();
    }

    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if (!empty($param['id']) && $param['id'] > 0) {
                $this->model->edit($param);
            } else {
                $this->model->add($param);
            }
        } else {
            $id = isset($param['id']) ? (int) $param['id'] : 0;
            if ($id > 0) {
                View::assign('detail', $this->model->getById($id));
            }
            return view();
        }
    }

    public function set()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $res   = $this->model->strict(false)->field('id,status')->update($param);
            if ($res) {
                add_log($param['status'] == 1 ? 'recovery' : 'disable', $param['id'], $param);
                return to_assign();
            }
            return to_assign(0, '操作失败');
        }
        return to_assign(1, '错误的请求');
    }

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