<?php

namespace app\adminapi\controller\v1\section;

use app\adminapi\controller\AuthController;
use app\models\section\SectionModel;
use think\Request;
use think\facade\Route as Url;
use crmeb\services\{
    FormBuilder as Form, UtilService as Util
};

/**
 * 部门管理
 */
class Section extends AuthController
{
    /**
     * 显示资源列表
     */
    public function index()
    {
        $where = Util::getMore([
            ['page', 1],
            ['limit', 15],
            ['status', ''],
            ['title', ''],
        ], $this->request);
        $list = SectionModel::systemPage($where);
        return $this->success($list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $f = array();
        $f[] = Form::input('title', '部门名称');
        $f[] = Form::input('intr', '部门简介')->type('textarea');
        $f[] = Form::frameImageOne('image', '部门图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('ios-add')->width('60%')->height('435px');
        $f[] = Form::number('sort', '排序', 0);
        $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
        return $this->makePostForm('添加部门', $f, Url::buildUrl('/section/section'), 'POST');
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = Util::postMore([
            'title',
            'intr',
            ['image', []],
            ['sort', 0],
            'status',]);
        if (!$data['title']) return $this->fail('请输入部门名称');
        if (count($data['image']) != 1) return $this->fail('请选择部门图片，并且只能上传一张');
        if ($data['sort'] < 0) return $this->fail('排序不能是负数');
        $data['add_time'] = time();
        $data['image'] = $data['image'][0];
        $res = SectionModel::create($data);
        return $this->success('添加部门成功!');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        if (!$id) return $this->fail('参数错误');
        $section = SectionModel::get($id)->getData();
        if (!$section) return $this->fail('数据不存在!');
        $f = array();
        $f[] = Form::input('title', '部门名称', $section['title']);
        $f[] = Form::input('intr', '部门简介', $section['intr'])->type('textarea');
        $f[] = Form::frameImageOne('image', '部门图片', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $section['image'])->icon('ios-add')->width('60%')->height('435px');
        $f[] = Form::number('sort', '排序', $section['sort']);
        $f[] = Form::radio('status', '状态', $section['status'])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
        return $this->makePostForm('编辑部门', $f, Url::buildUrl('/section/section/' . $id), 'PUT');
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = Util::postMore([
            'title',
            'intr',
            ['image', []],
            ['sort', 0],
            'status',]);
        if (!$data['title']) return $this->fail('请输入分类名称');
        if (count($data['image']) != 1) return $this->fail('请选择分类图片，并且只能上传一张');
        if ($data['sort'] < 0) return $this->fail('排序不能是负数');
        $data['image'] = $data['image'][0];
        if (!SectionModel::get($id)) return $this->fail('编辑的记录不存在!');
        SectionModel::edit($data, $id);
        return $this->success('修改成功!');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $res = SectionModel::edit(['is_del' => 1], $id, 'id');
        if (!$res)
            return $this->fail(SectionModel::getErrorInfo('删除失败,请稍候再试!'));
        else
            return $this->success('删除成功!');
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if ($status == '' || $id == 0) return $this->fail('参数错误');
        SectionModel::where(['id' => $id])->update(['status' => $status]);
        return $this->success($status == 0 ? '隐藏成功' : '显示成功');
    }

}
