<?php

namespace app\admin\controller\user;

use app\admin\controller\AuthController;
use app\models\system\SystemGroupLevel;
use think\facade\Route as Url;
use crmeb\traits\CurdControllerTrait;
use crmeb\services\{UtilService, JsonService, FormBuilder as Form};

/**
 * 会员设置
 * Class UserLevel
 * @package app\admin\controller\user
 */
class UserGroupLevel extends AuthController
{
    use CurdControllerTrait;

    /*
     * 等级展示
     * */
    public function index()
    {
        return $this->fetch();
    }

    /*
     * 创建form表单
     * */
    public function create($id = 0)
    {
        if ($id) $vipinfo = SystemGroupLevel::get($id);
        $field[] = Form::input('name', '等级名称', isset($vipinfo) ? $vipinfo->name : '')->col(Form::col(24));
        $field[] = Form::number('suit_award', "套装极差奖", isset($vipinfo) ? $vipinfo->suit_award : 0)->min(0)->col(8);
        $field[] = Form::number('group_award', "普通商品极差奖（%）", isset($vipinfo) ? $vipinfo->group_award : 0)->min(0)->col(8);
        $field[] = Form::number('integral_group_award', "积分商品极差奖（%）", isset($vipinfo) ? $vipinfo->integral_group_award : 0)->min(0)->col(8);
        $field[] = Form::number('level_num', "伞下套装单数", isset($vipinfo) ? $vipinfo->level_num : 0)->min(0)->col(8);
        $field[] = Form::radio('system_award', '参与加权分红', isset($vipinfo) ? $vipinfo->system_award : 0)->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8);
        $field[] = Form::radio('is_show', '是否显示', isset($vipinfo) ? $vipinfo->is_show : 0)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])->col(8);
        $field[] = Form::textarea('explain', '等级说明', isset($vipinfo) ? $vipinfo->explain : '');

        $form = Form::make_post_form('添加等级设置', $field, Url::buildUrl('save', ['id' => $id]), 2);
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }

    /*
     * 会员等级添加或者修改
     * @param $id 修改的等级id
     * @return json
     * */
    public function save($id = 0)
    {
        $data = UtilService::postMore([
            ['name', ''],
            ['suit_award', 0],
            ['group_award', 0],
            ['integral_group_award', 0],
            ['level_num', 0],
            ['is_show', ''],
            ['system_award', 0],
            ['explain', ''],
        ]);
        if (!$data['name']) JsonService::fail('请输入等级名称');
        SystemGroupLevel::beginTrans();
        try {
            //修改
            if ($id) {
                if (SystemGroupLevel::edit($data, $id)) {
                    SystemGroupLevel::commitTrans();
                    JsonService::successful('修改成功');
                } else {
                    SystemGroupLevel::rollbackTrans();
                    JsonService::fail('修改失败');
                }
            } else {
                //新增
                $data['add_time'] = time();
                if (SystemGroupLevel::create($data)) {
                    SystemGroupLevel::commitTrans();
                    JsonService::successful('添加成功');
                } else {
                    SystemGroupLevel::rollbackTrans();
                    JsonService::fail('添加失败');
                }
            }
        } catch (\Exception $e) {
            SystemGroupLevel::rollbackTrans();
            JsonService::fail($e->getMessage());
        }
    }

    /*
     * 获取系统设置的vip列表
     * @param int page
     * @param int limit
     * */
    public function get_system_vip_list()
    {
        $where = UtilService::getMore([
            ['page', 0],
            ['limit', 10],
            ['title', ''],
            ['is_show', ''],
        ]);
        JsonService::successlayui(SystemGroupLevel::getSystemList($where));
    }

    /*
     * 删除会员等级
     * @param int $id
     * */
    public function delete($id = 0)
    {
        if (SystemGroupLevel::edit(['is_del' => 1], $id))
            JsonService::successful('删除成功');
        else
            JsonService::fail('删除失败');
    }


    public function set_show($is_show = '', $id = '')
    {
        ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
        $res = SystemGroupLevel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
        if ($res) {
            JsonService::successful($is_show == 1 ? '显示成功' : '隐藏成功');
        } else {
            JsonService::fail($is_show == 1 ? '显示失败' : '隐藏失败');
        }
    }

    public function set_value($field = '', $id = '', $value = '')
    {
        $field == '' || $id == '' || $value == '' && JsonService::fail('缺少参数');
        if (SystemGroupLevel::where(['id' => $id])->update([$field => $value]))
            JsonService::successful('保存成功');
        else
            JsonService::fail('保存失败');
    }
}