var htmlOptEdit = '<button class="btn btn-outline-primary btn-sm" style="width:80px" id="btnRowEdit">编辑</button>'
var htmlOptDel = '' //'<button class="btn btn-outline-danger btn-sm" style="width:80px; margin-left:8px;" id="btnRowDel">删除</button>'

function renderIsOpen(data, type, row) {
  return data == 1 ? "启用" : "禁用"
}

var secpolicyTypeDef = {
  1: "系统事件",
  2: "光伏设备",
  3: "储能设备",
  4: "充电设备",
  5: "负荷设备",
  6: "其它"
}
function renderSecpolicy(data, type, row) {
  var text = secpolicyTypeDef[data]
  return text != undefined ? text : ''
}
var seclevelDef = { 1: "L1", 2: "L2", 3: "L3", 4: "L4" }
function renderSeclevel(data, type, row) {
  var text = seclevelDef[data]
  return text != undefined ? text : ''
}

var tableDef = {
  secpolicy: {
    table: null,
    columns: [
      { title: 'ID', width: '80px' },
      { title: '事件名称', width: '150px' },
      { title: '事件类型', width: '120px', render: renderSecpolicy },
      { title: '事件编号', width: '100px', },
      { title: '安全级别', width: '100px', render: renderSeclevel },
      { title: '描述' },
      { title: '操作参数' },
      { title: '是否启用', width: '120px', render: renderIsOpen },
      { title: '创建时间', width: '180px' },
      {
        title: '操作',
        width: '200px',
        render: function (data, type, row) { return htmlOptEdit + htmlOptDel },
      },
    ],
    header: ['sec_policy_id', 'name', 'type', 'code', 'level', 'describe', 'action', 'is_open', 'create_time'],
    query: G.cppNative.querySecPolicyList,
    insert: G.cppNative.insertSecPolicy,
    update: G.cppNative.updateSecPolicy,
    poptitle: "安全策略",
    popkeys: ['', 'name', 'type', 'code', 'level', 'describe', 'action', 'is_open', ''],
  },

  secrecord: {
    table: null,
    columns: [
      { title: 'ID', width: '80px' },
      { title: '告警时间', width: '180px' },
      { title: '事件类型' },
      { title: '事件编号' },
      { title: '安全级别' },
      { title: '告警内容' },
      { title: '设备ID' },
      { title: '操作人员', width: '180px' },
      { title: '操作时间', width: '180px' },
      { title: '处理方式', width: '180px' },
      { title: '状态', width: '80px' },
      {
        title: '操作',
        width: '200px',
        render: function (data, type, row) { return '' },
      },
    ],
    header: ['sec_policy_id', 'name', 'type', 'code', 'level', 'describe', 'action', 'is_open', 'create_time'],
    query: G.cppNative.querySecRecordList,
    insert: G.cppNative.insertSecRecord,
    update: G.cppNative.updateSecRecord,
    poptitle: "安全策略",
    popkeys: ['', 'name', 'type', 'code', 'level', 'describe', 'action', 'is_open', ''],
  }
}


// 初始化页面和表格的基础数据
async function initSubpage(id) {
  G.clickSubpageBtn(id)

  var tableInfo = tableDef[id]

  // 表格已经初始化，重新加载数据
  if (tableInfo.table) {
    tableInfo.table.ajax.reload()
    return
  }

  G.initTable(id, tableInfo)
  G.initForm(id, popConfirm)

  // 绑定行编辑
  tableInfo.table.on('click', '#btnRowEdit', function () {
    var row = tableInfo.table.row($(this).closest('tr'))
    showPop(id, row.data())
  })
}

function showPop(id, rowData) {
  var tableInfo = tableDef[id]
  tableInfo.rowData = (rowData && rowData.length > 0) ? rowData : null
  var isEdit = (tableInfo.rowData) ? true : false

  G.showElement(id + 'Pop', true)

  var tableInfo = tableDef[id]
  // 设置弹框的标题
  $('#' + id + 'PopTitle').text((isEdit ? '编辑' : '新增') + tableInfo.poptitle)
  // 设置弹框的参数信息
  G.popSetParams(id, tableInfo.popkeys, tableInfo.rowData, callbackPopSetParams)
}

function callbackPopGetParams(id, key) {
}

// 用户弹窗确认
function popConfirm(id) {
  var tableInfo = tableDef[id]

  // 获取弹框的参数信息
  var params = G.popGetParams(id, tableInfo.popkeys, tableInfo.rowData, callbackPopGetParams)
  if (tableInfo.rowData) {
    var dataId = tableInfo.rowData[0]
    G.cppNative.log(id + " edit: params=" + JSON.stringify(params))

    tableInfo.update(dataId, params).then(ret => {
      var msg = '编辑' + tableInfo.poptitle + (ret == 0 ? '操作成功!' : '操作失败!')

      if (ret == 0 && id == "role" && params.permission.length > 0) {
        G.cppNative.updateRolePermission(dataId, params.permission).then(ret => {
          G.showElement(id + 'Pop', false)
          showPrompt(msg, ret)
          tableInfo.table.ajax.reload()
        })
      } else {
        G.showElement(id + 'Pop', false)
        showPrompt(msg, ret)
        tableInfo.table.ajax.reload()
      }
    })
  } else {
    // tableInfo.insert(params).then(ret => {
    //   var msg = '新增' + tableInfo.poptitle + (ret == 0 ? '操作成功!' : '操作失败!')
    //   G.showElement(id + 'Pop', false)
    //   showPrompt(msg, ret)
    //   tableInfo.table.ajax.reload()
    // })
  }
}

$(document).ready(function () {
  initSubpage('secpolicy')
})
