<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('编辑商品属性')" />
</head>
<body class="white-bg">
<form id="formId" class="form-horizontal m-t">
<input id="prodId" th:value="${prodId}" type="hidden">
</form>
<div class="btn-group-sm" id="toolbar" role="group">
    <a class="btn btn-success" onclick="addAttr()" shiro:hasPermission="master:product:prodAttr:add">
        <i class="fa fa-plus"></i> 添加
    </a>
</div>
<div class="col-sm-12 select-table table-striped">
    <table id="bootstrap-table"></table>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var prefix = ctx + "master/product/prodAttr";
    var datas = [[${@dict.getType('prod_attr_type')}]];
    var editFlag = [[${@permission.hasPermi('master:product:prodAttr:edit')}]];
    var removeFlag = [[${@permission.hasPermi('master:product:prodAttr:remove')}]];
    var editPriceFlag = [[${@permission.hasPermi('master:product:prodAttrPrice:viewPrice')}]];

    $("#form-prod-edit").validate({
        focusCleanup: true
    });

    $(function() {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add/{id}",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "商品属性",
            detailView: true,
            queryParams : {
                "prodId": $("#prodId").val()
            },
            onExpandRow : function(index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [
                {
                    field: 'attrName',
                    title: '属性名称'
                },
                {
                    field: 'attrType',
                    title: '类型',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(datas, value);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.prodAttrId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-info btn-xs ' + editPriceFlag + '" href="javascript:void(0)" onclick="showEditPrice(\'' + row.prodAttrId + '\')"><i class="fa fa-money"></i>设定价格</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.prodAttrId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                        return actions.join('');
                    }
                }
            ]
        };
        $.table.init(options);
    });

    initChildTable = function(index, row, $detail) {
        var childTable = $detail.html('<table style="table-layout:fixed"></table>').find('table');
        $(childTable).bootstrapTable({
            url: ctx + "master/product/prodAttrJoint/list",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",
            queryParams : {
                "prodId": $("#prodId").val(),
                "prodAttrId": row.prodAttrId
            },
            columns: [
                {
                    field : 'attrName',
                    title : '属性名称'
                }
            ]
        });
    };

    function addAttr() {
        $.operate.add($("#prodId").val());
    }

    function showEditPrice(prodAttrId) {
        var prodId = $("#prodId").val();
        layer.open({
            type: 2,
            area: ['800px', '600px'],
            fix: false,
            //不固定
            maxmin: true,
            shade: 0.3,
            title: "设定商品价格",
            content: `${ctx}master/product/prodAttrPrice?prodId=${prodId}&prodAttrId=${prodAttrId}`,
            btn: ['确定', '关闭'],
            // 弹层外区域关闭
            shadeClose: true,
            yes: function(index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler(index, layero);
            }
        });
    }
</script>
</body>
</html>
