<?php

namespace app\badminapi\controller;


use app\models\system\StoreTemplate;
use crmeb\basic\BaseModel;
use crmeb\services\UtilService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\Response;

/**
 * 店铺模板类
 * Class Template
 * @package app\adminapi\controller
 */
class Template extends AuthController
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 获取模板列表
     * @return mixed
     * @throws DbException
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function get_list()
    {
        $search = UtilService::getMore([
            ['name', ''],
            ['page', 1],
            ['limit', 10],
            ['use_version', ''],
            ['is_ban', ''],
        ], $this->request, false);
        $list = StoreTemplate::getList($search);
        return $this->success($list);
    }

    /**
     * 修改状态
     * @param string $id
     * @param string $is_ban
     * @return Response
     */
    public function set_ban($id = '')
    {
        $is_ban = $this->request->post('is_ban', 1);
        if (!in_array($is_ban, [1, -1])) {
            $this->fail('参数错误');
        }
        ($is_ban == '' || $id == '') && $this->fail('缺少参数');
        if (StoreTemplate::setTemplateBan($id, (int)$is_ban)) {
            return $this->success($is_ban == 1 ? '启用成功' : '禁用成功');
        } else {
            return $this->fail(StoreTemplate::getErrorInfo($is_ban == 1 ? '启用失败' : '禁用失败'));
        }
    }

    /**
     * 快速编辑
     * @param string $id
     * @return Response
     * @throws \Exception
     */
    public function set_template($id)
    {
        $data = UtilService::postMore([
            ['field', 'name', '', '', 'empty_check', '缺少参数'],
            ['value', '', '', '', 'empty_check', '缺少参数']
        ]);
        if (!$id) return $this->fail('缺少参数');
        if (StoreTemplate::where('id', $id)->update([$data['field'] => $data['value']]))
            return $this->success('保存成功');
        else
            return $this->fail('保存失败');
    }

    /**
     * 新增/修改模板
     * @param int $id
     * @return mixed
     * @throws DbException
     */
    public function add_template($id = 0)
    {
        $data = UtilService::postMore([
            ['name', '', '', '', 'empty_check', '请输入模板名'],
            ['code', '', '', '', 'empty_check', '请上传模板'],
            ['use_version', 1],
            ['is_ban', 1],
            ['sort', 0],
        ]);
        if ($id) {
            if (!StoreTemplate::vaildWhere()->where('id', $id)->find()) {
                return $this->fail('所选模板不存在');
            }
        }
        BaseModel::beginTrans();
        try {
            if ($id) {
                $res = StoreTemplate::edit($data, $id);
            } else {
                $data['add_time'] = time();
                $res = StoreTemplate::create($data);
            }
            if ($res) {
                BaseModel::commitTrans();
                return $this->success('操作成功');
            } else {
                BaseModel::rollbackTrans();
                return $this->fail('操作失败');
            }
        } catch (Exception $e) {
            BaseModel::rollbackTrans();
            return $this->fail($e->getMessage());
        } catch (DbException $e) {
            BaseModel::rollbackTrans();
            return $this->fail($e->getMessage());
        }
    }

    /**
     * 获取指定资源
     *
     * @param int $id
     * @return Response
     */
    public function get_template($id)
    {
        return $this->success('ok', StoreTemplate::get($id) ? StoreTemplate::get($id)->toArray() : []);
    }


    /**
     * 删除指定资源
     *
     * @param int $id
     * @return Response
     */
    public function delete($id)
    {
        if (!StoreTemplate::delTemplate($id))
            return $this->fail(StoreTemplate::getErrorInfo('删除失败,请稍候再试!'));
        else
            return $this->success('删除成功!');
    }
}