<?php

declare (strict_types=1);

namespace app\adminapi\controller;

use think\facade\Db;

class CateController extends BaseAdminController
{

    public function index()
    {
        [$page, $limit] = $this->getPage();
        $query = Db::name('FlowType');
        $cate = $query->order('id asc')->page($page, $limit)->select()->toArray();
        $count = $query->count();
        $type = get_config('approve.type');
        foreach ($cate as $key => &$value) {
            foreach ($type as $k => $val) {
                if ($value['type'] == $val['id']) {
                    $value['type_name'] = $val['title'];
                }
            }
            $value['department'] = '全公司';
            if ($value['department_ids'] != '') {
                $department = Db::name('Department')->whereIn('id', $value['department_ids'])->column('title');
                $value['department'] = implode(',', $department);
            }
        }
        return $this->success('success', ['data' => $cate, 'count' => $count]);
    }

    public function create()
    {
        $param = get_params();
        if (!empty($param['id']) && $param['id'] > 0) {
            $data['update_time'] = time();
            $res = Db::name('FlowType')->strict(false)->field(true)->update($param);
            return $this->success('修改成功', [], 1, 1);
        } else {
            $param['create_time'] = time();
            $insertId = Db::name('FlowType')->strict(false)->field(true)->insertGetId($param);
            return $this->success('添加成功', [], 1, 1);
        }
    }

    public function status()
    {
        $param = get_params();
        $res = Db::name('FlowType')->where('id', $param['id'])->strict(false)->field('id,status')->update($param);
        if ($res) {
            return $this->success('修改成功', [], 1, 1);
        } else {
            return $this->fail('修改失败');
        }
    }

}