<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>优惠券修改</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">
    <link rel="stylesheet" type="text/css" href="../../layuiadmin/style/formSelects-v4.css"/>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md6">
            <div class="layui-card">

                <div class="layui-card-body">
                    <form class="layui-form" action="" lay-filter="component-form-element">
                        <div class="layui-row layui-col-space10 layui-form-item">
                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>名称：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="name" lay-verify="required" placeholder="请输入优惠券名称"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-col-lg6">
                                <label class="layui-form-label"><font color='red' size="4">*</font>金额：</label>
                                <div class="layui-input-block">
                                    <input type="text" name="amount" lay-verify="number" placeholder="请输入优惠券金额"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div>
                                <label class="layui-form-label"><font color='red' size="4">*</font>开始日期：</label>
                                <div class="layui-input-block">
                                    <input type="text" id="id_start_date" name="begin_date" placeholder="请输入开始日期"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div>
                                <label class="layui-form-label"><font color='red' size="4">*</font>结束日期：</label>
                                <div class="layui-input-block">
                                    <input type="text" id="id_end_date" name="end_date" placeholder="请输入结束日期"
                                           autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div>
                                <label class="layui-form-label"><font color='red' size="4">*</font>使用学校：</label>
                                <div class="layui-input-block">
                                    <select name="school" xm-select="schoolGroup"></select>
                                </div>
                            </div>
                            <div>
                                <label class="layui-form-label"><font color='red' size="4">*</font>使用分类：</label>
                                <div class="layui-input-block">
                                    <select name="category" xm-select="categoryGroup"></select>
                                </div>
                            </div>

                            <div>
                                <label class="layui-form-label">是否在用：</label>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="enable" lay-skin="switch" lay-text="是|否" checked=""
                                           value="1">
                                </div>
                            </div>
                        </div>
                        <button class="layui-btn" id="id_save" lay-submit lay-filter="component-form-element"
                                style="display: none">保存
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index', //主入口模块
        formSelects: 'formSelects-v4',
    }).use(['index', 'form', 'laydate', 'formSelects',], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , laydate = layui.laydate
            , formSelects = layui.formSelects;
        laydate.render({
            elem: '#id_start_date'
        });
        laydate.render({
            elem: '#id_end_date'
        });
        var id = layui.view.getParameterByName('id');
        if (id) {
            var editdata = JSON.parse(JSON.stringify(parent.layui.table.editdata)); // 框架有Bug所以这么转换
            form.val("component-form-element", editdata);
        }
        formSelects.config('categoryGroup', {
            beforeSuccess: function (eid, url, searchVal, result) {
                if (id) {
                    var category = parent.layui.table.editdata.category_ids ?? [];
                    for (var n in result.data) {
                        var item = result.data[n];
                        for (var c1 in category) {
                            if (category[c1] == item.value.toString()) {
                                item.selected = 'selected';
                            }
                        }
                        var children1 = item.children
                        for (var c in children1) {
                            for (var c2 in category) {
                                if (category[c2] == children1[c].value.toString()) {
                                    children1[c].selected = 'selected';
                                }
                            }
                            var children2 = children1[c].children
                            for (var i in children2) {
                                for (var c3 in category) {
                                    if (category[c3] == children2[i].value.toString()) {
                                        children2[i].selected = 'selected';
                                    }
                                }
                            }

                        }

                    }
                }
                return result;
            },
        });
        formSelects.data('categoryGroup', 'server', {
            url: '/option/get_category_tree/'
        });

        formSelects.config('schoolGroup', {
            beforeSuccess: function (eid, url, searchVal, result) {
                if (id) {
                    var school = parent.layui.table.editdata.school ?? [];
                    for (var n in result.data) {
                        var item = result.data[n];
                        if (school.indexOf(item.value) > -1) {
                            item.selected = 'selected';

                            var children1 = item.children
                            for (var c in children1) {
                                if (school.indexOf(children1[c].value) > -1) {
                                    children1[c].selected = 'selected';

                                    var children2 = children1[c].children
                                    for (var i in children2) {
                                        if (school.indexOf(children2[i].value) > -1) {
                                            children2[i].selected = 'selected';
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                return result;
            },
        });
        formSelects.data('schoolGroup', 'server', {
            url: '/option/get_school/'
        });
        if (id) {
            var url = '/order/coupon/' + id + '/'
            var type = 'put'
        } else {
            var url = '/order/coupon/'
            var type = 'post'
        }

        form.on('submit(component-form-element)', function (data) {
            admin.req({
                url: url
                , data: data.field
                , type: type
                , done: function (res) {
                    parent.layui.onSubmitChild(res);
                }
            });
            return false;
        });

        parent.layui.submitChild = function () {
            $("#id_save").click();
        };
    });
</script>
</body>
</html>
