<!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-edit" th:object="${prodAttrVo}">
            <input id="prodId" name="prodId" th:value="${prodId}" type="hidden">
            <input id="prodAttrId" name="prodAttrId" th:value="${prodAttrId}" 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" th:field="*{attrName}">
                    </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-box" th:each="dict:${@dict.getType('prod_attr_type')}">
                            <input type="radio" th:id="${dict.dictCode}" name="attrType" th:value="${dict.dictValue}" th:field="*{attrType}">
                            <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></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 + "/jointList",
            modalName: "商品属性列表",
            showSearch: false,
            showPageGo: false,
            showRefresh: true,
            showColumns: false,
            showToggle: false,
            showExport: false,
            queryParams: queryParams,
            columns: [
                {
                    checkbox: true,
                    formatter: function(value, row, index) {
                        if (row.jointId !== null) {
                            return {
                                checked : 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.jointId = $("#prodAttrId").val();
            search.attrType = 'attr';
            search.prodAttrId = null;
            search.attrName = null;
            return search;
        }

        function submitHandler() {
            if ($.validate.form()) {
                var attrType = $("input[name='attrType']:checked").val();
                if (attrType === "attr") {
                    var data = {
                        "prodId" : $("#prodId").val(),
                        "prodAttrId" : $("#prodAttrId").val(),
                        "attrName" : $("input[name='attrName']").val(),
                        "attrType" : attrType
                    }
                    $.ajax({
                        url: prefix + "/editAttr",
                        contentType: 'application/json',
                        data: JSON.stringify(data),
                        type: "post",
                        processData: false,
                        success: function(result) {
                            $.operate.successCallback(result);
                        }
                    });
                } else {
                    var prodAttrJoints = [];
                    var selectedRows = $("#prodAttr-table").bootstrapTable('getSelections');
                    selectedRows.forEach((row) => {
                        prodAttrJoint = {
                            "prodAttrJointId" : row.prodAttrJointId,
                            "prodId" : $("#prodId").val(),
                            "jointProdId" : $("#prodId").val(),
                            "jointProdAttrId" : row.prodAttrId,
                        }
                        prodAttrJoints.push(prodAttrJoint);
                    });

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