/**
 * zzx
 * 2022.01.17
 * 导入日志
 */
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 () => {
  var master_grid, view_grid;

  // 跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#journalImport",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", // 设置容器宽度
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#journalImport_main", "#journalImport_view"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $("#journalImport_view_return").on("click", function () {
    changePanel("#journalImport_main");
  });

  // 获取下拉框
  var dataSelect;
  getSelectGJ([
    { "objectcode": "OPERATE_STATUS" },
    { "objectcode": "IMPORT_STATUS" }
  ], function (res) {
    dataSelect = res.data;
  });

  // 查询省
  var provinceEnums = [];
  utilSyncAjaxGJ(UPCLOUD_RDC_TMS_MAINDATA_SERVICE + "/zone/get-by-pid", "post", JSON.stringify({ "fatherid": "01" }), function (data) {
    provinceEnums = data.data;
    for (let x in provinceEnums) {

      provinceEnums[x].text = provinceEnums[x].keyvalue;
      provinceEnums[x].value = provinceEnums[x].zoneid;
    }
    provinceEnums.unshift({ "value": "", "text": "" });
  }, "application/json");

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "id", name: "id", type: "ro", width: "130", hidden: true },
      { id: "importObjName", name: "导入对象", type: "ro", width: 120 },
      { id: "importUserName", name: "导入人", type: "ro", width: 120 },
      { id: "importUserId", name: "帐号", type: "ro", width: 120 },
      { id: "phoneNumber", name: "手机号", type: "ro", width: 120 },
      { id: "importTime", name: "导入时间", type: "ro", width: 120 },
      { id: "importBatchNumber", name: "导入批次", type: "ro", width: 120 },
      {
        id: "status", name: "导入状态", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.IMPORT_STATUS, v);
        }
      },
      {
        id: "operateStatus", name: "操作状态", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.OPERATE_STATUS, v);
        }
      },
      { id: "successNum", name: "成功条数", type: "ro", width: 120 },
      { id: "failNum", name: "失败条数", type: "ro", width: 120 },
      {
        id: "failReason", name: "失败原因", type: "ro", width: 300, render: function (v) {
          // if (!v) {
          //   return " "
          // }
          // else if (v.includes("http")) {
          //   return v + "^journalImport_download"
          // }
          // else {
          //   return v + "^null"
          // }
          if (v && v.includes('http')) {
            return '<a href="' + v + '" download>' + v + '</a>';
          }
          return v
        }
      },

      {
        id: "op", name: "操作", type: "button", align: "left", width: 120, fixed: "right", render: function (v, rd) {
          let result = "查看^journalImport_view"
          if (rd.status != 2) {
            result += ",撤销^journalImport_del"
          }
          return result;
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "importObjName", label: "查询条件:", placeholder: "请输入导入对象" },
          {
            type: "block", className: "layui-col-md3 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "master_search", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "master_reset", value: "重 置" }
            ]
          }
        ]
      }
    ],
    fixedLast: true,
    multiselect: false,
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE + "/import_log/page",
    type: "post",
    params: "{}",
    page: true,
    contentType: "application/json",
    totalCount: "totalCount"
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("journalImport_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        break;
    }
  });
  // 单个撤销
  window.journalImport_del = function (rid) {
    auditNodeDelete(rid);
  };
  // 撤销方法
  function auditNodeDelete(rid) {
    layer.open({
      title: "撤销",
      content: "是否撤销？",
      btn: ["确认", "取消"],
      yes: function () {
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE + "/import_log/revoke/" + rid, type: "get",
          success: function (res) {
            PlgDialog.close(pload);
            if (res.success) {
              layer.closeAll();
              PlgDialog.msg(res.message);
              master_grid.reload();
              return true;
            }
            return false;

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

  // 修改
  window.journalImport_view = function (rid, rdata) {
    changePanel("#journalImport_view");
    listColumns(rdata);
  };

  // 获取表格列
  function listColumns(params) {
    $("#journalImport_view_grid").html("");
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE + "/sysimport/templet/listColumns/" + params.importObjNo, type: "get",
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          let columns = res.data.map(item => {
            return { id: item.value, name: item.text, type: "ro", width: 120 };
          });
          columns.unshift({ id: "INDEX", name: "id", type: "ro", width: 120, hidden: true });
          let view_grid_opts = {
            skin: "card",
            columns,
            beforeLoadData: function (res) {
              res.data.list.forEach((item, index) => {
                item.INDEX = index + "";
              });
              return res;
            },
            fixedLast: false,
            multiselect: false,
            url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE + "/import_log/check",
            type: "post",
            params: JSON.stringify({ importBatchNumber: params.importBatchNumber, importObjNo: params.importObjNo }),
            page: true,
            contentType: "application/json",
            totalCount: "totalCount"
          };
          view_grid = new PlgGrid(view_grid_opts);
          view_grid.renderTo("journalImport_view_grid");
          view_grid.loadData();
          return true;
        }
        return false;

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

  window.journalImport_download = function (rid, rdata) {
    if (rdata.failReason && rdata.failReason.includes('http')) {
      var $form = $('<form method="GET"></form>');
      $form.attr('action', rdata.failReason);
      $form.appendTo($('body'));
      $form.submit();
    }
  }
};