/**
 * zzx
 * 2022.01.20
 * 预警配置
 */
import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE, UPCLOUD_RDC_TMS_BUSINESS_SERVICE, UPCLOUD_RDC_TMS_INTERFACE_SERVICE } from '@/config/serviceSettings.js';
import { getSelectGJ, querySysPicture } from '@/utils/prolog.ajax.js';
export default (_this) => {
  var master_grid, selectData;
  var userinfo = {
    p_id: '',
    p_username: '',
    p_workernumber: '',
    p_nickname: '',
    p_phone: null,
    p_email: null,
    p_companyid: '',
    p_enterpriseid: '',
    p_organizationid: '',
    p_companyname: '',
    p_orgname: '',
    p_enterprisename: '',
    p_usertype: '',
    p_lastlogindate: '',
    p_lastmodipassdate: null,
    p_validdate: '',
    p_import_batchno: null,
    p_data_source: null,
    forceModifyPasswdFlag: false,
    vehicle: null,
  }
  // 跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#earlyWarningAlert",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%",
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#earlyWarningAlert_main", "#earlyWarningAlert_view"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  $("#earlyWarningAlert_view_return").on("click", function () {
    changePanel("#earlyWarningAlert_main");
  });

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "messageId", name: "id", type: "ro", width: 120, hidden: true },
      { id: "messageTypeName", name: "消息类型", type: "ro", width: 120 },
      { id: "companyname", name: "客户", type: "ro", width: 120 },
      { id: "refbillno", name: "业务单号", type: "ro", width: 120 },
      { id: "message", name: "消息内容", type: "ro", width: 120 },
      { id: "sendTime", name: "发送时间", type: "ro", width: 120 },
      { id: "sendPeopleName", name: "发送人", type: "ro", width: 120 },
      { id: "sendDepartmentName", name: "发送部门", type: "ro", width: 120 },
      { id: "sendEnterpriseName", name: "发送公司", type: "ro", width: 120 },
      {
        id: "op", name: "操作", type: "button", align: "left", width: 150, fixed: "right", render: function () {
          return "查看^earlyWarningAlert_view,已读^earlyWarningAlert_read,已处理^earlyWarningAlert_handle";
        }
      }
    ],
    fixedLast: true, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: true, // 是否支持多选
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE + "/alertMessage/query", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("earlyWarningAlert_master");
  master_grid.loadData();

  // 已读
  window.earlyWarningAlert_read = function (rid) {
    alertMessage(rid, "read");
  };
  // 已处理
  window.earlyWarningAlert_handle = function (rid) {
    alertMessage(rid, "handle");
  };
  // 删除方法
  function alertMessage(messageId, url, isReturn) {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE + "/alertMessage/" + url, type: "post", contentType: "application/json", data: JSON.stringify([{ messageId }]),
      success: function (data) {
        PlgDialog.close(pload);
        if (data.success) {
          PlgDialog.msg(data.message);
          if (isReturn) changePanel("#earlyWarningAlert_main");
          master_grid.reload();
          // alertMessage_count();
          _this.$store.dispatch('resetRemind') // 更新顶部的数字
          return true;
        }
        setTimeout(() => { PlgDialog.alert(data.message || "接口调用失败"); }, 300);
        return false;

      }, error: function () { PlgDialog.close(pload); return false; }
    });
  }

  // 查看
  window.earlyWarningAlert_view = function (rid, rdata) {
    changePanel("#earlyWarningAlert_view");
    selectData = rdata;
    new PlgPanel({
      renderer: "earlyWarningAlert_view_form",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [
            { label: "消息类型", value: rdata.messageTypeName },
            { label: "客户", value: rdata.companyname },
            { label: "企业单号", value: rdata.refbillno },
            { label: "发送时间", value: rdata.sendTime }
          ],
          [
            { label: "发送人", value: rdata.sendPeopleName },
            { label: "发送部门", value: rdata.sendDepartmentName },
            { label: "发送公司", value: rdata.sendEnterpriseName }
          ],
          [
            { label: "消息描述", value: rdata.message }
          ]
        ]
      }
    });
  };

  $("#earlyWarning_edit_read").on("click", function () {
    alertMessage(selectData.messageId, "read", true);
  });
  $("#earlyWarning_edit_handle").on("click", function () {
    alertMessage(selectData.messageId, "handle", true);
  });
};