$(function () {
    $("#jqGrid").jqGrid({
        url: '../attribute/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '所属商户', name: 'merchName', index: 'merchName', width: 100, align: 'center'},
            {label: '所属门店', name: 'storeName', index: 'storeName', width: 80, align: 'center'},
            {label: '所属分类', name: 'categoryName', index: 'attribute_category_id', width: 80},
            {label: '名称', name: 'name', index: 'name', width: 80},
            // {label: '类型', name: 'inputType', index: 'input_type', width: 80},
            // {label: '值', name: 'value', index: 'value', width: 80},
            {label: '排序', name: 'sortOrder', index: 'sort_order', width: 80}],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        attribute: { sortOrder: '' },
        ruleValidate: {
            /*name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]*/
        },
        q: {
            name: '',
            categoryName: ''
        },
        categories: [],
        storeList: [],
        merchList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.attribute = {};
            vm.attribute = { sortOrder: '' };
            vm.categories = [];
            this.getCategories();
            vm.storeList = [];
            vm.merchList = [];
            vm.getMerchList();
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.attribute = {};
            vm.attribute = { sortOrder: '' };
            vm.categories = [];
            vm.storeList = [];
            vm.merchList = [];

            vm.getInfo(id);
            vm.getMerchList();
            vm.getStoresByMerch();
            this.getCategories();
        },
        saveOrUpdate: function (event) {
            var url = vm.attribute.id == null ? "../attribute/save" : "../attribute/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.attribute),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../attribute/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../attribute/info/" + id, function (r) {
                vm.attribute = r.attribute;
            });
        },
        getStoresByMerch: function (opt) {
            var value = opt.value;
            $.get("../store/getStoresByMerch?merchSn=" + value, function (r) {
                vm.storeList = r.list;
            });
        },
        getMerchList: function() {
            $.get("../merch/queryAll", function (r) {
                vm.merchList = r.list;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name, 'categoryName': vm.q.categoryName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        getCategories: function () {
            $.get("../category/getCategory?isL2=0", function (r) {
                vm.categories = r.list;
            });
        },
        handleSubmit: function (name) {
            vm.saveOrUpdate();
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});