<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 网站用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>


<div class="layui-form" lay-filter="form" id="form" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <label class="layui-form-label">套餐包</label>
        <div class="layui-input-inline">
            <input type="hidden" name="id" value="0"/>
            <input type="text" name="name" lay-verify="required" maxlength="32" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-inline">
            <input type="text" name="description" lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">次数</label>
        <div class="layui-input-inline">
            <input type="text" name="maxUses" lay-verify="required|probability" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">原价</label>
        <div class="layui-input-inline">
            <input type="text" name="originalPrice" lay-verify="required|validatePrice" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">优惠价</label>
        <div class="layui-input-inline">
            <input type="text" name="price" lay-verify="required|validatePrice" autocomplete="off" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-inline">
            <input type="checkbox" lay-verify="required" name="status" lay-skin="switch" lay-text="上架|下架">
        </div>
    </div>


    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'form', 'table', 'layer', 'upload'], function () {
        const $ = layui.$, form = layui.form, table = layui.table, layer = layui.layer, upload = layui.upload;


        form.verify({
            probability: function (value, elem) {
                let test = /^(0|[1-9]\d*)$/;
                if (!test.test(value)) return true;
                if (parseInt(value) > 999999) return true;
            },
            validatePrice: function (price) {
                const priceRegex = /^\d+(\.\d{1,2})?$/;
                if (!priceRegex.test(price)) return true;

            }
        });


        form.on('submit(layuiadmin-app-form-submit)', function (obj) {
            let data = obj.field;
            data.lang = 'zh-CN';
            $.ajax({
                url: "{{ route('plan.setData') }}",
                method: 'post',
                headers: {
                    'X-CSRF-TOKEN': "{{ csrf_token() }}",
                },
                data,
                success(res) {

                    if (res.code === 0) {
                        parent.layer.close(parent.layer.index);
                    } else if (res.code === 101009) {
                        let msg = "";
                        Object.keys(res.data).forEach(key => res.data[key].forEach(m => msg += m));
                        layer.alert(msg, {
                            title: res.msg
                        });
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            });
        });


    });

</script>
</body>
</html>