$(function () {
    $("#jqGrid").jqGrid({
        url: '../helpissue/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '商户编号', name: 'merchSn', index: 'merchSn', width: 60},
            {label: '门店名称', name: 'storeName', index: 'storeName', width: 80},
            {label: '问题分类', name: 'typeName', index: 'type_id', width: 60},
            {label: '问', name: 'question', index: 'question', width: 80},
            {label: '答', name: 'answer', index: 'answer', width: 280},
            {label: '排序', name: 'sort', index: 'sort', 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 () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        helpIssue: {},
        ruleValidate: {
            question: [
                {required: true, message: '问题不能为空', trigger: 'blur'}
            ],
            answer: [
                {required: true, message: '答案不能为空', trigger: 'blur'}
            ],
        },
        q: {
            typeName: ''
        },
        helpTypes: [],
        storeList: [],
        merchList: []
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.helpIssue = {};

            vm.getHelpType();
            vm.storeList = [];
            vm.merchList = [];
            vm.getMerchList();
        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
            vm.getMerchList();
            var rowData = $("#jqGrid").jqGrid('getRowData', id);
            vm.getStoresByValue(rowData.merchSn);
        },
        getStoresByValue: function (value) {
            $.get("../store/getStoresByMerch?merchSn=" + value, function (r) {
                vm.storeList = r.list;
            });
        },
        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;
            });
        },
        saveOrUpdate: function (event) {
            let url = vm.helpIssue.id == null ? "../helpissue/save" : "../helpissue/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.helpIssue),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../helpissue/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("../helpissue/info/" + id, function (r) {
                vm.helpIssue = r.helpIssue;
                vm.getHelpType();
            });
        },
        reloadSearch: function () {
            vm.q = {
                typeName: ''
            }
            vm.reload();
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'typeName': vm.q.typeName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getHelpType:function () {
            $.get("../helptype/queryAll", function (r) {
                vm.helpTypes = r.list;
            });
        }
    }
});