//@ sourceURL=charge_detail.js
//项目类型map
var classCodeMap = null;
//核算码map
var auditCodeMap = null;
//门诊发票码map
var billItemMzMap = null;
//住院发票码map
var billItemZyMap = null;
$(function () {
    initSelect();
    initZdChargeItemList();
    //重置查询参数
    $('#btn_clean').click(function () {
        $("#name").val(null);
        $("#classCode").selectpicker('val', null);//默认选中
        $("#classCode").selectpicker('refresh');
        $("#auditCode").selectpicker('val', null);//默认选中
        $("#auditCode").selectpicker('refresh');
        $("#billItemMz").selectpicker('val', null);//默认选中
        $("#billItemMz").selectpicker('refresh');
        $("#billItemZy").selectpicker('val', null);//默认选中
        $("#billItemZy").selectpicker('refresh');
        $("#delFlag").selectpicker('val', null);//默认选中
        $("#delFlag").selectpicker('refresh');
        // $("#clCode").val(null);
        initZdChargeItemList();
    });



    $('#btn_add').click(function () {
        $("#myModalLabel").text("新增收费项目");
        $("div.bs-example-modal-lg").modal();
    });



});

/**
 * 号别类别列表
 */
function initZdChargeItemList() {
    $('#tb_table').bootstrapTable("destroy");
    $('#tb_table').bootstrapTable({
        url: '/thmz/listZdChargeItem',         //请求后台的URL（*）
        method: 'post',                      //请求方式（*）
        toolbar: '#toolbar',                //工具按钮用哪个容器
        striped: true,                      //是否显示行间隔色
        cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        pagination: true,                   //是否显示分页（*）
        sortable: true,                     //是否启用排序
        sortOrder: "desc",                   //排序方式
        queryParams: queryParams,           //传递参数（*）
        sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
        pageNumber: 1,                       //初始化加载第一页，默认第一页
        pageSize: 5,                       //每页的记录行数（*）
        pageList: [10, 15, 25, 50, 100],        //可供选择的每页的行数（*）
        search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
        strictSearch: true,
        showColumns: false,                  //是否显示所有的列
        showRefresh: false,                  //是否显示刷新按钮
        minimumCountColumns: 2,             //最少允许的列数
        clickToSelect: true,                //是否启用点击选中行
        uniqueId: "ID",                     //每一行的唯一标识，一般为主键列
        showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
        cardView: false,                    //是否显示详细视图
        detailView: false,
        //rowStyle:rowStyle,//通过自定义函数设置行样式
        ajaxOptions: {
            headers: {
                'Accept': 'application/json',
                'Authorization': 'Bearer ' + localStorage.getItem("token")
            }
        },
        columns: [
            {
                title: '操作',
                field: 'op',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    var str = '<button type="button" class="btn btn-primary  btn-sm" onclick="updateZdChargeItem(\'' + row.code + '\')">编辑</button>';
                    if (row.delFlag == 0) {
                        str += '<button type="button" class="btn btn-warning  btn-sm" onclick="updateZdChargeItemDelFlag(\'' + row.code + '\',\'' + row.name + '\',1)">停用</button>';
                    } else if (row.delFlag == 1) {
                        str += '<button type="button" class="btn btn-success  btn-sm" onclick="updateZdChargeItemDelFlag(\'' + row.code + '\',\'' + row.name + '\',0)">启用</button>';
                    }
                    return [str].join('');
                }
            },
            {
                field: 'classCode',
                title: '项目类型',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    return classCodeMap.get(value);
                }
            }, {
                field: 'ybCode',
                title: '医保扩展码',
                align: "center",
                valign: 'middle'
            }, {
                field: 'xnhCode',
                title: '新农合项目编码',
                align: "center",
                valign: 'middle'
            }, {
                field: 'xnhName',
                title: '新农合项目名称',
                align: "center",
                valign: 'middle'
            }, {
                field: 'code',
                title: '项目编码',
                align: "center",
                valign: 'middle',
            }, {
                field: 'name',
                title: '项目名称',
                align: "center",
                valign: 'middle'
            }, {
                field: 'chargeUnit',
                title: '单位',
                align: "center",
                valign: 'middle'
            }, {
                field: 'chargeAmount',
                title: '收费金额',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value != null && value != undefined && value!="") {
                        return value.toFixed(2);
                    }
                    return "";
                }

            }, {
                field: 'percentag1',
                title: '第一折扣率',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value != null && value != undefined && value!="") {
                        return value.toFixed(2);
                    }
                    return "";
                }
            }, {
                field: 'percentag2',
                title: '第二折扣率',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value != null && value != undefined && value!="") {
                        return value.toFixed(2);
                    }
                    return "";
                }
            }, {
                field: 'percentag3',
                title: '第三折扣率',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value != null && value != undefined && value!="") {
                        return value.toFixed(2);
                    }
                    return "";
                }
            }, {
                field: 'percentag4',
                title: '第四折扣率',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value != null && value != undefined && value!="") {
                        return value.toFixed(2);
                    }
                    return "";
                }
            }, {
                field: 'percentag5',
                title: '第五折扣率',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value != null && value != undefined && value!="") {
                        return value.toFixed(2);
                    }
                }
            }
            // , {
            //     field: 'effectiveDate',
            //     title: '生效日期',
            //     align: "center",
            //     valign: 'middle',
            //     formatter: function (value, row, index) {
            //         if (value == null || value == "") {
            //             return "";
            //         }
            //         return format(value, "yyyy-MM-dd");
            //     }
            // }
            , {
                field: 'auditCode',
                title: '核算码',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    return auditCodeMap.get(value);
                }
            }
            // , {
            //     field: 'inOutFlag',
            //     title: '门诊/住院',
            //     align: "center",
            //     valign: 'middle',
            //     formatter: function (value, row, index) {
            //         if (value == 1) {
            //             return "门诊";
            //         }
            //         if (value == 2) {
            //             return "住院";
            //         }
            //         return "";
            //     }
            // }
            , {
                field: 'billItemMz',
                title: '门诊发票码',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    return billItemMzMap.get(value);
                }
            }, {
                field: 'billItemZy',
                title: '住院发票码',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    return billItemZyMap.get(value);
                }
            }
            // , {
            //     field: 'percentGroupMz',
            //     title: '门诊项目分组',
            //     align: "center",
            //     valign: 'middle'
            // }, {
            //     field: 'percentGroupZy',
            //     title: '住院项目分组',
            //     align: "center",
            //     valign: 'middle'
            // }
            // , {
            //     field: 'percentGroup1',
            //     title: '一级分摊分类',
            //     align: "center",
            //     valign: 'middle'
            // }, {
            //     field: 'chargeAmountLwg',
            //     title: '劳务工限价标准',
            //     align: "center",
            //     valign: 'middle'
            // }
            // , {
            //     field: 'ynCode',
            //     title: '院内码',
            //     align: "center",
            //     valign: 'middle'
            // }
            , {
                field: 'co',
                title: '是否加价',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "否";
                    }
                    if (value == 1) {
                        return "是";
                    }
                }
            }, {
                field: 'xlCode',
                title: '加价率',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value != null && value != undefined && value!="") {
                        return parseFloat(value).toFixed(2);
                    }
                    return "";
                }
            }
            , {
                field: 'execUnitName',
                title: '执行科室',
                align: "center",
                valign: 'middle'
            }
            // , {
            //     field: 'selfFlag',
            //     title: '自费',
            //     align: "center",
            //     valign: 'middle'
            // }, {
            //     field: 'separateFlag',
            //     title: '单列',
            //     align: "center",
            //     valign: 'middle'
            // }, {
            //     field: 'suppressFlag',
            //     title: '贵重',
            //     align: "center",
            //     valign: 'middle'
            // }, {
            //     field: 'ypFlag',
            //     title: '药品处方',
            //     align: "center",
            //     valign: 'middle'
            // }
            // , {
            //     field: 'pyCode',
            //     title: '拼音码',
            //     align: "center",
            //     valign: 'middle'
            // }, {
            //     field: 'wbCode',
            //     title: '五笔码',
            //     align: "center",
            //     valign: 'middle'
            // }
            , {
                field: 'bzCode',
                title: '标准编码',
                align: "center",
                valign: 'middle'
            }, {
                field: 'discription',
                title: '项目内涵',
                align: "center",
                valign: 'middle'
            }
            // , {
            //     field: 'ybName',
            //     title: '医保名称',
            //     align: "center",
            //     valign: 'middle'
            // }, {
            //     field: 'ybComment',
            //     title: '产地品牌',
            //     align: "center",
            //     valign: 'middle'
            // }
            , {
                field: 'addDate',
                title: '创建时间',
                align: "center",
                valign: 'middle',
                formatter: function (value, row, index) {
                    if (value == null || value == "") {
                        return "";
                    }
                    return format(value, "yyyy-MM-dd HH:mm:ss");
                }
            }, {
                field: 'clCode',
                title: '耗材院内编码',
                align: "center",
                valign: 'middle'
            }
        ],
        responseHandler: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            var ress = eval(res);
            if (ress.code == -1) {
                if (ress.message != null && ress.message != '') {
                    new PNotify({
                        title: '错误提示',
                        text: ress.message,
                        type: 'error',
                        hide: true,
                        styling: 'bootstrap3'
                    });
                }
                return {
                    "total": 0,//总页数
                    "rows": {}   //数据
                };
            }
            return {
                "total": ress.total,//总页数
                "rows": ress.data   //数据
            };
        },
    });
}


/**
 * 构建列表查询参数
 * @param params
 * @returns {{mzChargeDetail: {patientId: string | number | string[] | undefined | jQuery, warnDept: string | number | string[] | undefined | jQuery, doctorCode: string | number | string[] | undefined | jQuery, name: string | number | string[] | undefined | jQuery, payMark: number}, beginTime: Date, endTime: Date, pageSize: *, pageIndex: number}}
 */
function queryParams(params) {
    var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
        pageSize: params.limit,   //页面大小
        pageIndex: params.offset / params.limit,  //页码
        zdChargeItem: {
            name: $("#name").val(),
            classCode: $("#classCode").val(),
            auditCode: $("#auditCode").val(),
            billItemMz: $("#billItemMz").val(),
            billItemZy: $("#billItemZy").val(),
            delFlag: $("#delFlag").val()
            // ,clCode: $("#clCode").val()
        }
    };
    return temp;
}


/**
 * 初始化下拉选
 */
function initSelect() {
//启动Bootsrap-Select
    $(".selectpicker").selectpicker({
        dropuAuto: false
    });
    var yesNo = '<option value="1">是</option><option value="0">否</option>';
    $('#delFlag').html(yesNo);
    $('#delFlag').selectpicker('refresh');

    $('#coParams').html(yesNo);
    $('#coParams').selectpicker('refresh');
    $('#coParams').selectpicker('val', 0);
    $('#coParams').selectpicker('refresh');
    //查询所有收费项目类型
    $.ajax({
        type: "GET",
        url: '/thmz/getAllZdChargeClass',
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        async: false,
        success: function (data) {
            if (data == '401' || data == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            var html = '';
            $.each(data.data, function (commentIndex, comment) {
                html += '<option value="' + comment.code + '">' + comment.name + '</option>';
                if (classCodeMap == null) {
                    classCodeMap = new Map()
                }
                classCodeMap.put(comment.code, comment.name);
            });
            $('#classCode').empty();   //清空resText里面的所有内容
            $('#classCode').html(html);
            $('#classCode').selectpicker('refresh');

            $('#classCodeParams').empty();   //清空resText里面的所有内容
            $('#classCodeParams').html(html);
            $('#classCodeParams').selectpicker('refresh');

        }
    });


    //查询所有住院字典  核算码
    $.ajax({
        type: "GET",
        url: '/thmz/getAllZyZdAuditItem',
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        async: false,
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            var html = '';
            $.each(res.data, function (commentIndex, comment) {
                html += '<option value="' + comment.code + '">' + comment.name + '</option>';
                if (auditCodeMap == null) {
                    auditCodeMap = new Map()
                }
                auditCodeMap.put(comment.code, comment.name);
            });
            $('#auditCode').empty();   //清空resText里面的所有内容
            $('#auditCode').html(html);
            $('#auditCode').selectpicker('refresh');

            $('#auditCodeParams').empty();   //清空resText里面的所有内容
            $('#auditCodeParams').html(html);
            $('#auditCodeParams').selectpicker('refresh');

        }
    });


    //住院发票码
    $.ajax({
        type: "GET",
        url: '/thmz/getZyBillItems',
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        async: false,
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            var html = '';
            $.each(res.data, function (commentIndex, comment) {
                html += '<option value="' + comment.code + '">' + comment.name + '</option>';
                if (billItemZyMap == null) {
                    billItemZyMap = new Map()
                }
                billItemZyMap.put(comment.code, comment.name);
            });
            $('#billItemZy').empty();   //清空resText里面的所有内容
            $('#billItemZy').html(html);
            $('#billItemZy').selectpicker('refresh');

            $('#billItemZyParams').empty();   //清空resText里面的所有内容
            $('#billItemZyParams').html(html);
            $('#billItemZyParams').selectpicker('refresh');
        }
    });


    //门诊发票码
    $.ajax({
        type: "GET",
        url: '/thmz/getAllBillItems',
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        async: false,
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            var html = '';
            $.each(res.data, function (commentIndex, comment) {
                html += '<option value="' + comment.code + '">' + comment.name + '</option>';
                if (billItemMzMap == null) {
                    billItemMzMap = new Map()
                }
                billItemMzMap.put(comment.code, comment.name);
            });
            $('#billItemMz').empty();   //清空resText里面的所有内容
            $('#billItemMz').html(html);
            $('#billItemMz').selectpicker('refresh');

            $('#billItemMzParams').empty();   //清空resText里面的所有内容
            $('#billItemMzParams').html(html);
            $('#billItemMzParams').selectpicker('refresh');
        }
    });




    initDeptSelect();
}


/**
 * 停用或者启用收费项目
 * @param code
 */
function updateZdChargeItemDelFlag(code, name,delFlag) {
    if (delFlag == 1 && !confirm("确认要停用项目【"+name+"】信息吗？")) {
        return;
    }
    $.ajax({
        type: "GET",
        url: '/thmz/updateZdChargeItemDelFlag?code=' + code + "&delFlag=" + delFlag,
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            if (res.code == 0) {
                successMesage(res);
                $('#tb_table').bootstrapTable("refresh");
            } else {
                errorMesage(res);
            }
        }
    });
}



/**
 * 修改收费项目
 * @param code
 */
function updateZdChargeItem(code) {
    $.ajax({
        type: "GET",
        url: '/thmz/getZdChargeItemByCode?code=' + code,
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            if (res.code == 0) {
                $('#classCodeParams').selectpicker('val', res.data.classCode);
                $('#classCodeParams').selectpicker('refresh');
                $("#ybCodeParmas").val(res.data.ybCode);
                $("#xnhCodeParams").val(res.data.xnhCode);
                $("#xnhNameParams").val(res.data.xnhName);
                $("#namePamras").val(res.data.name);
                $("#chargeUnitParams").val(res.data.chargeUnit);
                $("#chargeAmountParams").val(res.data.chargeAmount);
                $("#percentag1Params").val(res.data.percentag1);
                $("#percentag2Params").val(res.data.percentag2);
                $("#percentag3Params").val(res.data.percentag3);
                $("#percentag4Params").val(res.data.percentag4);
                $("#percentag5Params").val(res.data.percentag5);
                $('#auditCodeParams').selectpicker('val', res.data.auditCode);
                $('#auditCodeParams').selectpicker('refresh');
                $('#billItemMzParams').selectpicker('val', res.data.billItemMz);
                $('#billItemMzParams').selectpicker('refresh');
                $('#billItemZyParams').selectpicker('val', res.data.billItemZy);
                $('#billItemZyParams').selectpicker('refresh');
                $('#execUnitParams').selectpicker('val', res.data.execUnit);
                $('#execUnitParams').selectpicker('refresh');
                $("#bzCodeParams").val(res.data.bzCode);
                $("#discriptionParams").val(res.data.discription);
                $("#clCodeParams").val(res.data.clCode);
                $('#coParams').selectpicker('val', res.data.co);
                $('#coParams').selectpicker('refresh');
                $("#xlCodeParams").val(res.data.xlCode);
                $("#code").val(res.data.code);
                $("div.bs-example-modal-lg").modal();
            } else {
                errorMesage(res);
            }
        }
    });
}


/**
 * 挂号列表中的科室列表
 */
function initDeptSelect() {
    //科室列表
    $.ajax({
        type: "GET",
        url: '/thmz/allUnitCode',
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        success: function (data) {
            if (data == '401' || data == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            var html = '';
            $.each(data.data, function (commentIndex, comment) {
                html += '<option value="' + comment.code + '">' + comment.name + '(' + comment.pyCode + ')</option>';
            });
            $('#execUnitParams').empty();
            $('#execUnitParams').html(html);
            $('#execUnitParams').selectpicker('refresh');
        }
    });
}










//保存收费项目
$('#save').click(function () {
    $.ajax({
        type: "POST",
        url: '/thmz/saveZdChargeItem',
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        headers: {'Accept': 'application/json', 'Authorization': 'Bearer ' + localStorage.getItem("token")},
        data: JSON.stringify({"classCode": $("#classCodeParams").val(),"ybCode": $("#ybCodeParmas").val(),"xnhCode": $("#xnhCodeParams").val(),
            "xnhName": $("#xnhNameParams").val(),"name": $("#namePamras").val(),"chargeUnit": $("#chargeUnitParams").val(),
            "chargeAmount": $("#chargeAmountParams").val(),"percentag1": $("#percentag1Params").val(),"percentag2": $("#percentag2Params").val(),
            "percentag3": $("#percentag3Params").val(),"percentag4": $("#percentag4Params").val(),"percentag5": $("#percentag5Params").val(),
            "auditCode": $("#auditCodeParams").val(),"billItemMz": $("#billItemMzParams").val(),
            "billItemZy": $("#billItemZyParams").val(),"execUnit": $("#execUnitParams").val(),"bzCode": $("#bzCodeParams").val(),
            "discription": $("#discriptionParams").val(),"clCode": $("#clCodeParams").val(),"co": $("#coParams").val(),"xlCode": $("#xlCodeParams").val(),
            "code": $("#code").val()}),
        success: function (res) {
            if (res == '401' || res == 401) {
                window.location.href = '/thmz/login/view'
                return;
            }
            if (res.code == 0) {
                $("div.bs-example-modal-lg").modal("hide");
                clearInput();
                successMesage(res);
                $('#tb_table').bootstrapTable("refresh");
            } else {
                errorMesage(res);
            }
        }
    });
});



/**
 * 清空输入框
 */
function clearInput() {
    $('#classCodeParams').selectpicker('val', null);
    $('#classCodeParams').selectpicker('refresh');
    $("#ybCodeParmas").val(null);
    $("#xnhCodeParams").val(null);
    $("#xnhNameParams").val(null);
    $("#namePamras").val(null);
    $("#chargeUnitParams").val(null);
    $("#chargeAmountParams").val(null);
    $("#percentag1Params").val(null);
    $("#percentag2Params").val(null);
    $("#percentag3Params").val(null);
    $("#percentag4Params").val(null);
    $("#percentag5Params").val(null);
    $('#auditCodeParams').selectpicker('val', null);
    $('#auditCodeParams').selectpicker('refresh');
    $('#billItemMzParams').selectpicker('val', null);
    $('#billItemMzParams').selectpicker('refresh');
    $('#billItemZyParams').selectpicker('val', null);
    $('#billItemZyParams').selectpicker('refresh');
    $('#execUnitParams').selectpicker('val', null);
    $('#execUnitParams').selectpicker('refresh');
    $("#bzCodeParams").val(null);
    $("#discriptionParams").val(null);
    $("#clCodeParams").val(null);
    $('#coParams').selectpicker('val', 0);
    $('#coParams').selectpicker('refresh');
    $("#xlCodeParams").val(null);
    $("#code").val(null);
}