<%@ page import="com.yihu.hos.system.model.SystemUser" %><%--
  Created by IntelliJ IDEA.
  User: llh
  Date: 2016/1/7
  Time: 14:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="utf-8"%>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script >
  var standard = {};
  var Util = $.Util;

  standard.list = {
    _url: $("#hd_url").val(),
    top: null,
    grid: null,
    columns: [],
    TypeSearch: null,
    $search: $('#inp_search'),
    init: function () {
//      this.top = $.Util.getTopWindowDOM();
      <%
                SystemUser user = (SystemUser) session.getAttribute("userInfo");
                String systemName = user.getUserName();
           %>
      var userName = "<%=systemName%>";
      //CDA 列名
      this.columns = [
        { display: '标准编码', name: 'code', hide:true},
        { display: '标准列表', name: 'name', width: '20%', align: 'left' },
        { display: '发布状态', name: 'status', width: '20%',align: 'center', render: function (rowdata, rowindex, value) {
          if(rowdata.status==1 && rowdata.type=="2"){
            return ' <div style="vertical-align:middle;margin-top: 10px;"><span>已发布  </span><img src="${contextRoot}/develop/images/button/fabu_icon.png"/></a></div>';
          }else if(rowdata.status==0 && rowdata.type=="2"){
            return ' <div style="vertical-align:middle;margin-top: 10px;"><span>未发布  </span><img src="${contextRoot}/develop/images/button/fabuwei_icon.png"/></a></div>';
          }
        }},
        { display: '发布者', name: 'publisher',width: '20%', align: 'center', type: 'int', render: function (rowdata, rowindex, value) {
          if(rowdata.publisher == '') {
            rowdata.publisher = userName;
            if(rowdata.status==1 && rowdata.type=="2"){
              return rowdata.publisher;
            }
          }else {
            return rowdata.publisher;
          }

        } },
        { display: '发布时间', name: 'date', width: '20%',align: 'center',render: function (rowdata, rowindex, value) {
          if(!Util.isStrEmpty(rowdata.date)){
            return (rowdata.date+"").substring(0,19);
          }
        }
        },
        {
          display: '操作', isSort: false, width: '20%', align: 'center',render: function (rowdata, rowindex, value) {
          var html = '<div class="m-inline-buttons" style="width:200px;">';
          if(rowdata.type=="0"){//行数据为机构数据
            html += "<a href='#' style=\"width: 80px;\" onclick='standard.list.add(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowdata.code + "\",\"" + rowindex + "\", \"0\")'>新增标准</a>";
            html += "<a class=\"m-btn-view\" onclick='standard.list.addPublisher(\"" + rowdata.id + "\", \"modify\")'></a> ";
//            html += "<a class=\"m-btn-delete\" onclick='standard.list.deleted(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowindex + "\")'></a>";
          } else if(rowdata.type=="1"){//行数据为标准数据
            html += "<a href='#' style=\"width: 80px;\" onclick='standard.list.addVersion(\"" + rowdata.pid + "\",\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowdata.code + "\",\"" + rowindex + "\", \"0\")'>新增版本</a>";
            html +=  "<a class=\"m-btn-edit\" onclick='standard.list.add(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowdata.code + "\",\"" + rowdata.description + "\", \"modify\")'></a> ";
            html +=  "<a class=\"m-btn-delete\"  onclick='standard.list.deleted(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowindex + "\")'></a>";
          } else if(rowdata.type=="2"){//版本数据
            if(rowdata.status!=0){//已发布版本
              html +=  "<a href='#' style=\"width: 80px;\" onclick='standard.list.config(\"" + rowdata.code + "\",\"" + rowdata.pid + "\",\"" + rowdata.status + "\",\"" + rowindex + "\")'>查看配置</a>";
              html +=  "<a href='#' class=\"m-btn-view\"  onclick='standard.list.addVersion(\"" + rowdata.pid + "\",\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowdata.code + "\",\"" + rowindex + "\", \"\")'></a>";
            }else{//未发布版本
              html += "<a href='#' onclick='standard.list.config(\"" + rowdata.code + "\",\"" + rowdata.pid + "\",\"" + rowdata.status + "\",\"" + rowindex + "\")'>配置</a>";
              html += "<a href='#' style=\"width: 55px;\" onclick='standard.list.releaseVersion(\"" + rowdata.id + "\", \"" + rowdata.publisher + "\")'>发布</a>";
              html += "<a class=\"m-btn-edit\" onclick='standard.list.addVersion(\"" + rowdata.pid + "\",\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowdata.code + "\",\"" + rowindex + "\", \"1\")'></a> ";
              html += "<a class=\"m-btn-delete\" onclick='standard.list.deleted(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowindex + "\")'></a>";
            }
          }
          return html;
        }
        }
      ];

      this.$search.ligerSearch({onClick: function (value) {
        standard.list.getTypeList();
      }});

      this.getTypeList();
      this.event();
    },
    getTypeList: function () {
      var u = standard.list;
      var type = $("#type").val();
      var name = $("#inp_search").val();
      $.ajax({
        url: u._url + "/standardCenter/searchStandard",
        type: "get",
        dataType: "json",
        data:{condition: JSON.stringify({"type":type,"name":name})},
        success: function (data) {
          if(data.successFlg){
            var resultArr =  data.detailModelList || [];
            standard.list.setUserList(resultArr);
          }else{
            $.ligerDialog.error(data.message);
          }
        },error:function(){
          $.ligerDialog.error("数据获取失败！");
        }
      })
    },
    setUserList: function (data) {
      var u = standard.list;
      var dataJson = [];
      //根据下拉框加载相应的数据
      dataJson = data;
      var gridData = {
        Total: dataJson != null ? dataJson.length : null,
        Rows: dataJson
      };
      u.rows = dataJson;
      // window.grid=u.grid=null;
      if (u.grid == null) {
        //$.LigerGridEx.config(
        u.grid = $("#div_cda_type_grid").ligerGrid({
          record: 'totalCount',
          root: 'detailModelList',
          pageSize:10,
          pagesizeParmName: 'rows',

          allowAdjustColWidth: true,
          usePager: false,
          scrollToPage: false,
          columns: u.columns,
          data: gridData,
          height: "100%",
          rownumbers: false,
          checkbox: false,
          root: 'Rows',
          tree: {
           // columnId: 'deptName',
            columnName: 'name',
            idField: 'id',
            parentIDField: 'pid'
          },
          onError: function (a, b) {
          },
          onGroupExtend: function () {

          }
        });

      }
      else {
        u.grid.reload(gridData);
      }
      // 自适应宽度
     // u.grid.adjustToWidth();
      if($("#inp_search").val()){//当搜索时→展开全部列表
        u.grid.expandAll();
      }else{
//        u.grid.collapseAll();//展开全部expandAll()； 收缩全部collapseAll()
      }
      window.grid = u.grid;
    },
    showDialog: function (_tital, _url, _height, _width, callback) {
      standard.list.dialog_standard_detail = $.ligerDialog.open({
        title: _tital,
        url: _url,
        height: _height,
        width: _width,
        onClosed: callback
      });
    },
    addPublisher: function (id,type) {
    var _tital = "查看机构信息";
    var _url = standard.list._url + "/integration/publisherUpdate?type=modify&id="+id;
    var callback = function () {
      standard.list.getTypeList();
    };
    standard.list.showDialog(_tital, _url, 300, 400, callback);
  },
    add: function (id,name,code,description, type) {
      var stdType = $("#type").val();
      var _tital = type=="modify"?"修改标准":"新增标准";
      var _url = type=="modify"?standard.list._url + "/integration/standardUpdate?type=modify&standardId="+id+"&name="+name+"&code="+code
              :standard.list._url + "/integration/standardUpdate"+"?&typeFlg=1&name="+name+"&code="+code;
      var callback = function () {
        standard.list.getTypeList();
      };
      standard.list.showDialog(_tital, _url, 500, 500, callback);
    },
    addVersion: function (pid,id,name,code,rowindex,type) {
      var _tital = "";
      if(type =="0"){
        _tital = "新增标准版本";
      }else if(type=="1"){
        _tital = "编辑标准版本";
      }else{
        _tital = "标准版本信息";
      }
      standard.list.grid.select(rowindex);//选中行
      var row = standard.list.grid.getParent(standard.list.grid.getSelectedRow());

      if(row!=null) {//是父节点
        code = $.parseJSON(JSON.stringify(row)).code;
        if(type != "0") {
          name = $.parseJSON(JSON.stringify(row)).name;//获取父节点的标准名称
        }
      }

      var _url = standard.list._url + "/integration/versionUpdate?standardId="+pid+"&id=" + id+"&name="+name+"&type="+type+"&code="+code;
      var callback = function () {
        standard.list.getTypeList();
      };
      standard.list.showDialog(_tital, _url, 500, 500, callback);
    },
    releaseVersion: function (versionId,publisher) {
      debugger
      versionId = parseInt(versionId.replace("version", ""));
      var  _text = "确定唯一发布当前版本？";
      $.ligerDialog.confirm(_text, function (confirm) {
        if (confirm) {
          $.ajax({
            url: standard.list._url + "/standardCenter/publishVersion",
            type: "get",
            dataType: "json",
            data: {versionId: versionId,publisher:publisher},
            success: function (data) {
              if(data.successFlg){
                $.ligerDialog.success(data.message);
                standard.list.getTypeList();
              }else{
                $.ligerDialog.error(data.message);
              }

            },
            error: function (jqXHR, textStatus, errorThrown) {
              $.ligerDialog.error("发布失败！");
            }
          })
        }
      });
    },
    config: function (code,pid,status,rowindex) {
      debugger
      standard.list.grid.select(rowindex);//选中行
      var row = standard.list.grid.getParent(standard.list.grid.getSelectedRow());
      var rootRow =  standard.list.grid.getParent(standard.list.grid.getParent(standard.list.grid.getSelectedRow()));
      var name = "";
      if(rootRow!=null && row!=null){//是父节点
        name =  encodeURIComponent(rootRow.name+"_"+row.name+"_"+standard.list.grid.getSelectedRow().name);
      }

      indexPage.openChildPage("",'${contextRoot}/integration/configuration?version='+code+"&stdId="+pid+"&type="+$("#type").val()+"&status="+status+"&name="+name);
    },
    deleted: function (id,name,rowindex) {

      standard.list.grid.select(rowindex);//选中行
      var _text;
      var row = standard.list.grid.getParent(standard.list.grid.getSelectedRow());
      if(row==null){//是root节点（删除机构）
           id = Number(id.replace("publisher",""));
          _text = "确定删除机构<br/>"+name+"？";
          standard.list.doDeletedPublisher(id, _text);
      }else{//是父节点（删除标准）
        var parentRow = standard.list.grid.getParent(row);
        if (parentRow == null) {
          id = Number(id.replace("standard",""));
          _text = "确定删除标准<br/>"+name+"？";
          standard.list.doDeletedStandard(id, _text);
        } else {
          _text = "确定删除标准版本<br/>"+name+"？";
          id = Number(id.replace("version",""));
          var version = standard.list.grid.getSelectedRow().code;
          standard.list.doDeletedVersion(id, version , _text);
        }
      }
    },
    doDeletedStandard: function (standardId, _text) {//删除标准
      $.ligerDialog.confirm(_text, function (confirm) {
        if (confirm) {
          $.ajax({
              url: standard.list._url + "/standardCenter/deleteStandard",
              type: "get",
              dataType: "json",
              data: {standardId: standardId},
              success: function (data) {
                if(data.successFlg){
                  $.ligerDialog.success(data.message);
                  standard.list.getTypeList();
                }else{
                  $.ligerDialog.error(data.message);
                }
              },
            error: function (jqXHR, textStatus, errorThrown) {
              
                $.ligerDialog.error("删除失败！");
            }
          })
        }
      });
    },
    doDeletedPublisher: function (publisherId, _text) {//删除机构
      $.ligerDialog.confirm(_text, function (confirm) {
        if (confirm) {
          $.ajax({
            url: standard.list._url + "/standardCenter/deletePublisher",
            type: "get",
            dataType: "json",
            data: {publisherId: publisherId},
            success: function (data) {
              if(data.successFlg){
                $.ligerDialog.success(data.message);
                standard.list.getTypeList();
              }else{
                $.ligerDialog.error(data.message);
              }
            },
            error: function (jqXHR, textStatus, errorThrown) {
              $.ligerDialog.error("删除失败！");
            }
          })
        }
      });
    },
    doDeletedVersion: function (versionId, version,_text) {//删除标准版本
      $.ligerDialog.confirm(_text, function (confirm) {
        if (confirm) {
          $.ajax({
            url: standard.list._url + "/standardCenter/deleteVersion",
            type: "get",
            dataType: "json",
            data: {versionId: versionId,version:version},
            success: function (data) {
              if(data.successFlg){
                $.ligerDialog.success(data.message);
                standard.list.getTypeList();
              }else{
                $.ligerDialog.error(data.message);
              }

            },
            error: function (jqXHR, textStatus, errorThrown) {
              $.ligerDialog.error("删除失败!");
            }
          })
        }
      });
    },
    event: function () {
      $("#inp_search").keyup(function (e) {
        if (e.keyCode == 13) {
          standard.list.getTypeList();
        }
      });

    }
  };

  $(function(){
    standard.list.init();
  })


</script>
