<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>

<script type="text/javascript">
    var rsResourceInfoDialog = {

        dialog: frameElement.dialog,
        $form: $("#div_info_form"),//表单对象
        $resourceName: $("#inp_resource_name"),//资源名称
        $resourceCode: $("#inp_resource_code"),//资源代码
        $resourceType: $('input[name="inp_resource_type"]', this.$form),//资源类型
        $resourceRate: $("#inp_resource_rate"),//资源服务频率
        $resourceSupportType: $('input[name="inp_resource_support_type"]', this.$form),//提供方式
        $resourceCategory: $("#inp_resource_category"),//资源分类
        validator: null,
        $id: $("#inp_resource_id"),//资源数据源
        $btnSave: $("#btn_save"),//保存按钮对象
        $btnCancel: $("#btn_cancel"),//关闭按钮对象

        init: function () {
            this.initForm();
            this.bindEvents();
        },
        initForm: function () {
            var mode = '${mode}';
            var data;
            if (mode == 'view') {
                $("input,select", this.$form).prop('disabled', false);
            }
            this.$resourceName.ligerTextBox({width: 240});
            this.$resourceCode.ligerComboBox({dict: true, dictName: "RESOURCE_CODE"});
            this.$resourceType.ligerRadio();
            this.$resourceSupportType.ligerRadio();
            this.$resourceRate.ligerTextBox({width: 240});
            this.$resourceCategory.ligerComboBox({dict: true, dictName: "CATEGORY_LIST"});

            if ('${mode}' == 'new') {
                data={
                    category: '${categoryId}'
                };
            } else {
                data={
                    name: '${entity.name}',
                    code: '${entity.code}',
                    inp_resource_type: '${entity.type}',
                    rate: '${entity.serviceRate}',
                    inp_resource_support_type: '${entity.dataSupportType}',
                    category: '${entity.category}',
                    id: '${entity.id}'
                };
            }
            this.$form.ligerAutoForm({
                data:data,
                validate:{
                    name:"required",
                    code:"required",
                    inp_resource_support_type:"required",
                    inp_resource_type:"required",
                    category:"required"
                }
            });
        },
        bindEvents: function () {
            this.$btnSave.click(function () {
                if (!$("#div_info_form").ligerAutoForm("validate")) {
                    return;
                }
                var data=$("#div_info_form").ligerAutoForm("getData");
                if ('${mode}' == 'new') {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url: "${contextRoot}/resource/resource/createRsResource",
                        dataType: "json",
                        data: data,
                        cache: false,
                        success: function (data) {
                            if (data.successFlg) {
                                parent.window.rsResoureManege.dialogCallBack(data.message);
                            } else {
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error: function (data) {
                            $.ligerDialog.error(data.message);
                        }
                    });
                } else {
                    $.ajax({ //ajax处理
                        type: "POST",
                        url: "${contextRoot}/resource/resource/updateRsResource",
                        dataType: "json",
                        data: data,
                        cache: false,
                        success: function (data) {
                            if (data.successFlg) {
                                parent.window.rsResoureManege.dialogCallBack(data.message);
                            } else {
                                $.ligerDialog.error(data.message);
                            }
                        },
                        error: function (data) {
                            $.ligerDialog.error(data.message);
                        }
                    });
                }
            });

            this.$btnCancel.click(function () {
                rsResourceInfoDialog. dialog.close();
            });
        }
    }
    $(function () {
        rsResourceInfoDialog.init();
    });
</script>