<?php

namespace app\api\controller;

use app\api\lists\approve\ApproveLists;
use app\common\logic\approve\ApproveLogic;
use app\common\model\Approve;
use Symfony\Component\HttpClient\HttpClient;
use think\Exception;
use think\facade\Db;

class ApproveController extends BaseApiController
{
    public function lists()
    {
        return $this->dataLists(new ApproveLists());
    }

    public function approveType()
    {
        $list = Db::name('flow_type')->where(['status' => 1])->field(['id, title'])->select()->toArray();
        return $this->success('成功', $list);
    }

    /**
     * 前台通用审批接口  审批后的业务逻辑,需要具体拆解
     */
    public function audit()
    {
        try {
            $params = $this->request->param(); // id check_status remark
            $approve = Approve::find($params['id']);
            if (!$approve) {
                throw new Exception('数据不存在');
            }
            ApproveLogic::audit($approve, $params, $this->userInfo);
            return $this->success('操作成功');
        } catch (Exception $exception) {
            return $this->fail(ApproveLogic::getError() ?? $exception->getMessage());
        }
    }
    public function detail()
    {
        $param = $this->request->param();
        $detail = ApproveLogic::detail($param['id']);
        if (!$detail) {
            $this->fail('数据不存在');
        }
        return $this->success('成功', $detail->toArray());
    }
}