<?php

namespace app\controller\manager;

use app\model\CouponMain;
use app\repository\BusinessRepository;
use app\repository\CmsRepository;
use app\model\CouponType as CouponTypeModel;

use Exception;
use think\facade\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\response\Json;
use think\response\View;

/**
 * 签到券类型
 *
 * Class CouponType
 * @package app\controller\manager
 */
class CouponType extends Base
{
    /**
     * 编辑
     *
     * @return Json|View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws Exception
     */
    public function edit()
    {
        $id = input('id/d', 0);

        if (!$info = CouponTypeModel::findById($id)) {
            return $this->json(4001, '记录不存在');
        }

        if ($this->request->isPost()) {
            $item     = input('post.');
            $validate = $this->validateByApi($item, [
                'name|标题'  => 'require|max:100|unique:coupon_type,name,'.$id,
            ]);

            if ($validate !== true) {
                return $validate;
            }

            Db::startTrans();
            try {
                $info->save($item);
                Db::commit();
                return $this->json();
            } catch (ValidateException $e) {
                Db::rollback();
                return $this->json(4001, $e->getError());
            }
        }

        $this->data['item']      = $info;

        return $this->view();
    }

    /**
     * 添加
     *
     * @return Json|View
     * @throws Exception
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $item     = input('post.');
            $validate = $this->validateByApi($item, [
                'name|标题' => 'require|max:100',
            ]);

            if ($validate !== true) {
                return $validate;
            }
            try {
                CouponTypeModel::create($item);
                return $this->json();
            } catch (ValidateException $e) {
                return $this->json(4001, $e->getError());
            }
        }

        return $this->view();
    }


    /**
     * 删除
     * @return Json
     */
    public function del()
    {
        if (!$this->request->isPost()) {
            return $this->json(4000, '非法请求');
        }

        $ids = $this->request->param('ids/a', []);

        $couponMain = CouponMain::findOne([["type","in",$ids]]);
        if(!empty($couponMain)){
            return $this->json(4001,"该签到券绑定了该类型,不能删除");
        }

        CouponTypeModel::destroy($ids);
        return $this->json();
    }

    /**
     * 列表
     *
     * @return Json|View
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        if ($this->request->isPost()) {
            $menus = CouponTypeModel::getList();
            $res   = [
                'code'  => 0,
                'msg'   => 'success',
                'count' => $menus->count(),
                'data'  => $menus->toArray(),
            ];
            return json($res);
        }
        return $this->view();
    }

}