<!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-form-item">
                            <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-form-item">
                            <div class="layui-form-item">
                                <label class="layui-form-label"><font color='red' size="4">*</font>商品类别：</label>
                                <div class="layui-input-block">
                                    <select id="id_category" name="category" lay-verify="required">
                                        <option value="">请选择商品类别</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        
                        <div class="layui-form-item">
                            <div class="layui-form-item">
                                <label class="layui-form-label"><font color='red' size="4">*</font>价格类型：</label>
                                <div class="layui-input-block">
                                    <select id="id_type" name="type" lay-filter="id_type">
                                        <option value="1" selected>现金商品</option>
                                        <option value="2">积分商品</option>
                                    </select>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item" id="price_item">
                            <label class="layui-form-label"><font color='red' size="4">*</font>销售价格：</label>
                            <div class="layui-input-block">
                                <input type="text" id="id_price" name="price" lay-verify="numberGtZ|required" placeholder="请输入原销售价" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item" id="vip_price_item">
                            <label class="layui-form-label"><font color='red' size="4">*</font>会员价格：</label>
                            <div class="layui-input-block">
                                <input type="text" id="id_vip_price" name="vip_price" lay-verify="numberGtZ|required" placeholder="请输入销售限价折扣" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item" id="point_price_item" hidden>
                            <label class="layui-form-label"><font color='red' size="4">*</font>积分价格：</label>
                            <div class="layui-input-block">
                                <input type="text" id="id_point_price" name="point_price" lay-verify="numberGeZ" placeholder="请输入销售限价折扣" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">起始销量：</label>
                            <div class="layui-input-block">
                                <input type="text" name="initial_sale_count" lay-verify="intGtz|required" placeholder="请输入" value="0" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">排序：</label>
                            <div class="layui-input-block">
                                <input type="text" name="sort" lay-verify="intGtz|required" placeholder="请输入排序" value="0" class="layui-input">
                                <div class="layui-word-aux">越大越靠后。</div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label"><font color='red' size="4">*</font>是否上架：</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="status" lay-skin="switch" lay-text="是|否" checked="" value="1">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">备注：</label>
                            <div class="layui-input-block">
                                <textarea type="text" name="notes" class="layui-textarea" placeholder="请输入备注"></textarea>
                            </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', 'utils', 'formSelects', ], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form;

        form.on('select(id_type)', function(data){
            var val = data.value;
            var price = '';
            var vip_price = '';
            var point_price = '';
            if (editdata) {
                if (editdata.type === 1) {
                    price = editdata.price;
                    vip_price = editdata.price;
                } else if (editdata.type === 2) {
                    point_price = editdata.point_price;
                }
            }
            if (val === '1') {
                $('#vip_price_item').show();
                $('#id_vip_price').val(vip_price);
                $('#id_vip_price').attr('lay-verify', 'numberGtZ|required');

                $('#price_item').show();
                $('#id_price').val(price);
                $('#id_price').attr('lay-verify', 'numberGtZ|required');

                $('#point_price_item').hide();
                $('#id_point_price').val('');
                $('#id_point_price').attr('lay-verify', 'numberGeZ');
            } else if (val === '2') {
                $('#point_price_item').show();
                $('#id_point_price').attr('lay-verify', 'numberGtZ|required');
                $('#id_point_price').val(point_price);

                $('#vip_price_item').hide();
                $('#id_vip_price').val('');
                $('#id_vip_price').attr('lay-verify', 'numberGeZ');

                $('#price_item').hide();
                $('#id_price').val('');
                $('#id_price').attr('lay-verify', 'numberGeZ');
            }
        });


        var id = layui.view.getParameterByName('id');


        admin.req({
            url: '/commodity/dict/',
            done: function (res) {
                var commodity_categorys = res.data.category;
                var category = $('#id_category');
                for (var i in commodity_categorys) {
                    var pid1 = commodity_categorys[i].value;
                    var name1 = commodity_categorys[i].name;
                    category.append("<option value='" + pid1 + "'>" + name1 + "</option>");
                }
                form.val("component-form-element", editdata);
            }
        });

        form.render(null, 'component-form-element');

        if (id) {
            var editdata = JSON.parse(JSON.stringify(parent.layui.table.editdata)); // 框架有Bug所以这么转换
            form.val("component-form-element", editdata);

            if (editdata.type === 1) {
                $('#vip_price_item').show();
                $('#id_vip_price').attr('lay-verify', 'numberGtZ|required');

                $('#price_item').show();
                $('#id_price').attr('lay-verify', 'numberGtZ|required');
                $('#point_price_item').hide();

                $('#id_point_price').val('');
                $('#id_point_price').attr('lay-verify', 'numberGeZ');
            } else if (editdata.type === 2) {
                $('#point_price_item').show();
                $('#id_point_price').attr('lay-verify', 'numberGtZ|required');

                $('#vip_price_item').hide();
                $('#id_vip_price').val('');
                $('#id_vip_price').attr('lay-verify', 'numberGeZ');

                $('#price_item').hide();
                $('#id_price').val('');
                $('#id_price').attr('lay-verify', 'numberGeZ');
            }

        }
        var url = id ? '/commodity/' + id + '/' : '/commodity/',
            method = id ? 'put' : 'post';

        form.on('submit(component-form-element)', function (data) {
            if(!data.field.status){
                data.field.status = '0'
            }

            admin.req({
                url: url
                , data: data.field
                , type: method
                , done: function (res) {
                    parent.layui.onSubmitChild(res);
                }
            });
            return false;
        });

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