<!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-dept-edit" th:object="${goodsCategory}">
        <input name="goodsCategoryId" type="hidden" th:field="*{goodsCategoryId}" />
        <input id="treeId" name="parentCategoryId" type="hidden" th:field="*{parentCategoryId}" />
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label">上级部门：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <div class="input-group">-->
<!--                    <input class="form-control" type="text" id="treeName" onclick="selectDeptTree()" readonly="true" th:field="*{parentName}">-->
<!--                    <span class="input-group-addon"><i class="fa fa-search"></i></span>-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="form-group">-->
<!--            <label class="col-sm-3 control-label is-required">上级类目：</label>-->
<!--            <div class="col-sm-8">-->
<!--                <div class="input-group">-->
<!--                    <input class="form-control" type="text" onclick="selectDeptTree()" id="treeName" readonly="true" th:value="${goodsCategoryName}" required>-->
<!--                    <span class="input-group-addon"><i class="fa fa-search"></i></span>-->
<!--                </div>-->
<!--            </div>-->
<!--        </div>-->
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">类别名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="goodsCategoryName" th:field="*{goodsCategoryName}" id="goodsCategoryName" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">类别编码：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="goodsCategoryCode" th:field="*{goodsCategoryCode}" id="goodsCategoryCode" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">显示排序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="categorySort" th:field="*{categorySort}" required>
            </div>
        </div>
        <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('goods_category_type')}">
                    <input type="radio" th:id="${dict.dictCode}" name="type" th:value="${dict.dictValue}" 
                           th:checked="${goodsCategory.type != null ? goodsCategory.type.toString() == dict.dictValue : dict.default}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <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('sys_normal_disable')}">
                    <input type="radio" th:id="${dict.dictCode}" name="status" th:value="${dict.dictValue}" th:checked="${dict.default}">
                    <label th:for="${dict.dictCode}" th:text="${dict.dictLabel}"></label>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="col-xs-2 control-label">保险设定：</label>
                    <div class="col-xs-10">
                        <label th:each="insurance:${insurances}" class="check-box">
                            <input name="insurance" type="checkbox" th:value="${insurance.id}" th:text="${insurance.companyName}" th:checked="${insurance.flag}">
                        </label>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<script type="text/javascript">
    var prefix = ctx + "goods/category";

    $("#form-dept-edit").validate({
        onkeyup: false,
        rules:{
            deptName:{
                remote: {
                    url: prefix + "/checkDeptNameUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "parentCategoryId": function() {
                            return $("input[name='parentCategoryId']").val();
                        },
                        "goodsCategoryName" : function() {
                            return $.common.trim($("#goodsCategoryName").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            categorySort:{
                digits:true
            }
        },
        messages: {
            "goodsCategoryName": {
                remote: "类目已经存在"
            }
        },
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var data = $("#form-dept-edit").serializeArray();
            var insuranceIds = $.form.selectCheckeds("insurance");
            data.push({"name": "insuranceIds", "value": insuranceIds});
            
            // 自定义保存，不刷新表格
            customEditSave(data);
        }
    }

    // 自定义编辑保存方法
    function customEditSave(data) {
        $.ajax({
            url: prefix + "/edit",
            type: "post",
            dataType: "json",
            data: data,
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍候...");
            },
            success: function(result) {
                $.modal.closeLoading();
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess(result.msg);
                    $.modal.close();
                } else if (result.code == web_status.WARNING) {
                    $.modal.alertWarning(result.msg);
                } else {
                    $.modal.alertError(result.msg);
                }
            }
        });
    }

    /*部门管理-修改-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        if ($.common.isEmpty(treeId)) {
            $.modal.alertWarning("请先添加用户所属的类目！");
            return;
        }
        var options = {
            title: '类目选择',
            width: "380",
            url: prefix + "/selectGoodsCategoryTree/" + treeId,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero){
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowLastLevel(tree)) {
            var body = $.modal.getChildFrame(index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            $.modal.close(index);
        }
    }
</script>
</body>
</html>