<%--
  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 adapterplan = {};
  var Util = $.Util;

  //应用管理弹框
  var applicationMsgDialog = null;
  adapterplan.list = {
    _url: $("#hd_url").val(),
    top: null,
    grid: null,
    columns: [],
    TypeSearch: null,
    adapterCustomize:null,
    init: function () {
//      this.top = $.Util.getTopWindowDOM();
      //CDA 列名
      this.columns = [
        { display: 'stdVersion', name: 'stdVersion', hide:true},
        { display: 'adapterStdVersion', name: 'adapterStdVersion', hide:true},
        { display: '适配方案', name: 'name', width: '15%', align: 'left' },
        { display: '发布状态', name: 'status', width: '5%',align: 'center', render: function (rowdata, rowindex, value) {
          if(rowdata.status==1 && rowdata.type=="1"){
            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=="1"){
            return ' <div style="vertical-align:middle;margin-top: 10px;"><span>未发布  </span><img src="${contextRoot}/develop/images/button/fabuwei_icon.png"/></a></div>';
          }
        }},
        { display: '目标发布机构', name: 'stdPublisher', width: '10%', align: 'center' },
        { display: '目标标准名称', name: 'stdName',width: '10%', type: 'int', align: 'center' },
        { display: '目标标准版本', name: 'stdVersionName', width: '10%', align: 'center' },
        { display: '源发布机构', name: 'publisher', width: '10%', align: 'center' },
        { display: '源标准', name: 'adapterName', width: '10%', align: 'center' },
        { display: '源标准版本', name: 'adapterVersionName', width: '10%', align: 'center' },
        {
          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='adapterplan.list.addVersion(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowindex + "\", \"add\")'>新增版本</a>";
            html += "<a class=\"m-btn-edit\"  onclick='adapterplan.list.add(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowdata.description + "\", \"modify\")'></a> ";
            html += "<a class=\"m-btn-delete\" onclick='adapterplan.list.deleted(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowindex + "\")'></a>";
          } else if(rowdata.type=="1"){//版本数据
            if(rowdata.status!=0){//已发布版本
              html += "<a href='#' style=\"width: 80px;\" onclick='adapterplan.list.config(\"" + rowdata.version + "\",\"" + rowdata.pid + "\",\"" + rowindex + "\",\"" + rowdata.status + "\")'>查看配置</a>";
              html += "<a href='#' class=\"m-btn-view\" onclick='adapterplan.list.addVersion(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowindex + "\", \"\")'></a>";
            }else{//未发布版本
              html += "<a href='#' style=\"width: 40px;\" onclick='adapterplan.list.customize(\"" + rowindex + "\",\"" + rowdata.version + "\",\"" + rowdata.pid + "\")'>定制</a>";
              html += "<a href='#' style=\"width: 40px;\" onclick='adapterplan.list.config(\"" + rowdata.version + "\",\"" + rowdata.pid + "\",\"" + rowindex + "\",\"" + rowdata.status + "\")'>配置</a>" ;
              html += "<a href='#' style=\"width: 40px;\" onclick='adapterplan.list.releaseVersion(\"" + rowdata.id + "\", \"" + rowdata.publisher + "\")'>发布</a>";
              html += "<a class=\"m-btn-edit\" onclick='adapterplan.list.addVersion(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowindex + "\", \"modify\")'></a> ";
              html += "<a class=\"m-btn-delete\" onclick='adapterplan.list.deleted(\"" + rowdata.id + "\",\"" + rowdata.name + "\",\"" + rowindex + "\")'></a>";
            }
          }
          return html;
        }
        }
      ];

      $("#inp_search").ligerSearch({onClick: function (value) {
        adapterplan.list.getTypeList();
      }});

      this.getTypeList();
      this.event();
    },

    getTypeList: function () {
      var u = adapterplan.list;
      var type = $("#type").val();
      var name = $("#inp_search").val();
      $.ajax({
        url: u._url + "/adapterCenter/searchScheme",
        type: "get",
        dataType: "json",
        data:{condition: JSON.stringify({"type":type,"name":name})},
        success: function (data) {

          if(data.successFlg){
            var resultArr =  data.detailModelList || [];
            adapterplan.list.setUserList(resultArr);
          }else{
            $.ligerDialog.error(data.message);
          }

        }
      })
    },
    setUserList: function (data) {
      var u = adapterplan.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({
          usePager: false,
          columns: u.columns,
          data: gridData,
          rownumbers: false,
          root: 'Rows',
          tree: {
            // columnId: 'deptName',
            columnName: 'name',
            idField: 'id',
            parentIDField: 'pid'
          }
        });
      }
      else {
        u.grid.reload(gridData);
      }
      u.grid.expandAll();//展开全部expandAll()； 收缩全部collapseAll()
    //  window.grid = u.grid;
    },
    showDialog: function (_tital, _url, _height, _width, callback) {

      adapterplan.list.dialog_adapter_detail = $.ligerDialog.open({
        title: _tital,
        url: _url,
        height: _height,
        width: _width,
        onClosed: callback
      });
    },
    add: function (id,name,description, type) {
      var _tital = type=="modify"?"修改适配方案":"新增适配方案";
      
      var _url = type=="modify"?adapterplan.list._url + "/adapterPlan/adapterUpdate?type=modify&id="+id:adapterplan.list._url + "/adapterPlan/adapterUpdate";
      var callback = function () {
        adapterplan.getTypeList();
      };
      adapterplan.list.showDialog(_tital, _url, 580, 500, callback);
    },
    addVersion: function (id,name,rowindex,type) {
      var _tital;
      var schemeNm = "";
      var schemeId = "";
      var versionId = "";
      var versionNm = "";
      var _url;
      adapterplan.list.grid.select(rowindex);//选中行
      if(type=="add"){
        _tital = "新增版本";
        schemeId = id;
        schemeNm = name;
      }else{
        if(type=="modify"){
          _tital = "修改版本";
        }else{
          _tital = "版本信息";
        }
        versionId = id;
        versionNm = name;
        var row = adapterplan.list.grid.getParent(adapterplan.list.grid.getSelectedRow());
        if(row!=null) {//是父节点
          schemeNm = row.name;
          schemeId = row.id;
        }
      }
      _url = adapterplan.list._url + "/adapterPlan/versionUpdate?versionId="+ versionId+"&type="+type+"&versionNm="+versionNm+"&schemeId="+schemeId+"&schemeNm="+schemeNm;
      var callback = function () {
        adapterplan.list.getTypeList();
      };
      adapterplan.list.showDialog(_tital, _url, 500, 500, callback);
    },
    releaseVersion: function (id, publisher) {
      var _text;
      id = id.replace("version","");
      
      var _url = adapterplan.list._url + "/adapterCenter/publishVersion";
        _text = "确定发布当前版本？";
        $.ligerDialog.confirm(_text, function (confirm) {
              if (confirm) {
                $.ajax({
                  url: _url,
                  dataType: "json",
                  data:{versionId: Number(id), publisher:""},
                  success: function (data) {

                    if(data.successFlg){
                      $.ligerDialog.success(data.message);
                      adapterplan.list.getTypeList();
                    }else{
                      $.ligerDialog.error(data.message);
                    }

                  },
                  error: function (jqXHR, textStatus, errorThrown) {
                    $.ligerDialog.error("发布失败！")
                  }
                })
              }
        });
    },
    copyVersion: function (id) {

      var _tital ="复制版本";
      var _url = adapterplan.list._url + "/adapterplan/copyversion?id=" + id;
      var callback = function () {
        adapterplan.list.getTypeList();
      };
      adapterplan.list.showDialog(_tital, _url, 400, 500, callback);
    },
    customize:function(rowindex,version,schemeId){
      var _tital = "适配定制";
      var row = adapterplan.list.grid.getParent(adapterplan.list.grid.getRow(rowindex));
      var stdVersion = row.stdVersion;
      var adapterVersion = version;
      var  _url = adapterplan.list._url + "/adapterPlan/adapterCustomize?stdVersion="+stdVersion+"&adapterVersion="+adapterVersion+"&schemeId="+schemeId;
      var callback = function () {
        adapterplan.list.getTypeList();
      };
      adapterplan.list.showDialog(_tital, _url, 550, 680, callback);
    },
    config: function (version, schemeId, rowindex,status) {
      
      var row = adapterplan.list.grid.getParent(adapterplan.list.grid.getRow(rowindex));
      var stdVersion = row.stdVersion;
      var adapterStdVersion = row.adapterStdVersion;

      adapterplan.list.grid.select(rowindex);//选中行
      var row = adapterplan.list.grid.getParent(adapterplan.list.grid.getSelectedRow());
      var name = "";
      if(row!=null){//是父节点
        name = encodeURIComponent(row.name+"_"+adapterplan.list.grid.getSelectedRow().name);
      }

      indexPage.openChildPage("",'${contextRoot}/adapterPlan/configuration?version='+version+"&status="+status+"&schemeId="+schemeId+"&stdVersion="+stdVersion+"&adapterStdVersion="+adapterStdVersion+"&name="+name);
    },
    deleted: function (id,name,rowindex) {
      if ($.Util.isStrEmpty(id)) {
        $.ligerDialog.error("请先选择需要删除的数据!");
        return;
      }
      adapterplan.list.grid.select(rowindex);//选中行
      var _text;
      var row = adapterplan.list.grid.getParent(adapterplan.list.grid.getSelectedRow());
      if(row==null){//是父节点
        _text = "确定删除适配方案<br/>"+name+"？";
        adapterplan.list.doDeletedScheme(id, _text);
      }else{//是子节点
        var parentName = $.parseJSON(JSON.stringify(row)).name;//获取父节点的标准名称
        _text = "确定删除适配版本<br/>"+parentName+name+"？";
        adapterplan.list.doDeletedVersion(id, _text);
      }
    },
    doDeletedScheme: function (schemeId, _text) {//删除标准
      $.ligerDialog.confirm(_text, function (confirm) {
        if (confirm) {
          schemeId = schemeId.replace("scheme","");
          $.ajax({
            url: adapterplan.list._url + "/adapterCenter/deleteScheme",
            type: "get",
            dataType: "json",
            data: {schemeId: Number(schemeId)},
            success: function (data) {
              if(data.successFlg){
                $.ligerDialog.success(data.message);
                adapterplan.list.getTypeList();
              }else{
                $.ligerDialog.error(data.message);
              }

            },
            error: function (jqXHR, textStatus, errorThrown) {
              $.ligerDialog.error("删除失败！");
            }
          })
        }
      });
    },
    doDeletedVersion: function (versionId, _text) {//删除标准版本
      $.ligerDialog.confirm(_text, function (confirm) {
        if (confirm) {
          versionId = versionId.replace("version","");
          $.ajax({
            url: adapterplan.list._url + "/adapterCenter/deleteVersion",
            type: "get",
            dataType: "json",
            data: {versionId: Number(versionId)},
            success: function (data) {
              
              if(data.successFlg){
                $.ligerDialog.success(data.message);
                adapterplan.list.getTypeList();
              }else{
                $.ligerDialog.error(data.message);
              }

            },
            error: function (jqXHR, textStatus, errorThrown) {
              $.ligerDialog.error("删除失败!");
            }
          })
        }
      });
    },
    event: function () {
      $('#btn_add_adapter').click(function () {
        adapterplan.list.add("","","","");
      });

      $("#inp_search").keyup(function (e) {
        if (e.keyCode == 13) {
          adapterplan.list.getTypeList();
        }
      });

    }
  };

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


</script>
