<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增商品属性')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-prodAttr-add">
            <input id="prodId" name="prodId" th:value="${prodId}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">属性名称：</label>
                    <div class="col-sm-8">
                        <input name="attrName" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">属性种类 ：</label>
                    <div class="col-sm-8">
                        <div class="radio radio-box">
                            <input type="radio" name="attrType" value="attr" id="rAttrType1" checked=""><label>属性</label>
                        </div>
                        <div class="radio radio-box">
                            <input type="radio" name="attrType" value="joint" id="rAttrType2"><label>属性组合</label>
                        </div>
                    </div>
                </div>
            </div>
        </form>
        <div class="col-sm-12" id="prodAttr-area">
            <div class="btn-group-sm" id="toolbar" role="group">
            </div>
            <div class="col-sm-8 select-table table-striped">
                <table id="prodAttr-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "master/product/prodAttr";

        var options = {
            id: "prodAttr-table",
            url: prefix + "/list",
            modalName: "商品属性列表",
            showSearch: false,
            showPageGo: false,
            showRefresh: true,
            showColumns: false,
            showToggle: false,
            showExport: false,
            queryParams: queryParams,
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'attrName',
                    title: '属性名称'
                },
                {
                    field: 'attrType',
                    title: '属性分类'
                }
            ]
        };
        $.table.init(options);

        $("#form-prodAttr-add").validate({
            rules: {
                attrName: {
                    required: true
                }
            },
            messages: {
                attrName: icon + "请输入属性名称"
            },
            focusCleanup: true
        });

        $("input[name='attrType']").change(function() {
            if ($(this).is(":checked")) {
                if ($("input[name='attrType']:checked").val() === "attr") {
                    $("#prodAttr-area").hide();
                } else {
                    $("#prodAttr-area").show();
                    $.table.refresh();
                }
            }
        });

        function queryParams(params) {
            var search = $.table.queryParams(params);
            search.prodId = $("#prodId").val();
            search.attrType = 'attr';
            search.attrName = null;
            return search;
        }

        function submitHandler() {
            if ($.validate.form()) {
                var attrType = $("input[name='attrType']:checked").val();
                if (attrType === "attr") {
                    $.operate.save(prefix + "/addAttr", $('#form-prodAttr-add').serialize());
                } else {
                    var prodAttrJoints = [];
                    var selectedRows = $("#prodAttr-table").bootstrapTable('getSelections');
                    selectedRows.forEach((row) => {
                        prodAttrJoint = {
                            "prodId" : $("#prodId").val(),
                            "jointProdId" : $("#prodId").val(),
                            "jointProdAttrId" : row.prodAttrId,
                        }
                        prodAttrJoints.push(prodAttrJoint);
                    });

                    var data = {
                        "prodId" : $("#prodId").val(),
                        "attrName" : $("input[name='attrName']").val(),
                        "attrType" : attrType,
                        "prodAttrJoints" : prodAttrJoints
                    }
                    $.ajax({
                        url: prefix + "/addAttrs",
                        contentType: 'application/json',
                        data: JSON.stringify(data),
                        type: "post",
                        processData: false,
                        success: function(result) {
                            $.operate.successCallback(result);
                        }
                    });
                }
            }
        }
        $("#prodAttr-area").hide();
    </script>
</body>
</html>