/**
   * guoj
   * 2020.07.28
   * 异常记录查询
   */
 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 dataSelete = {};
  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_ABNORMAL_TYPE" }], function (data) {
    dataSelete = data.data;
    dataSelete.TMS_ABNORMAL_TYPE.unshift({text: "",value: ""});
  });
  var grid;
  //配送中心table
  var userTable = {
      columns: [
          { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
          { id: "companycode", name: "配送中心编号", type: "ro" },
          { id: "companyname", name: "配送中心名称", type: "ro" }
      ],
      multiselect: false,
      contentType: "application/json",
        url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch", //数据接口
      type: "post", //数据提交方式，默认为get
      pagebar: { type: "simple" },
      params: JSON.stringify({"companytype": "S"}), //查询提交参数，分页参数默认为pageNum,pageSize
      page: true, //是否启用分页
      totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "seqno", name: "id", type: "ro", width: "80", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: "100" },
      { id: "seqno", name: "异常编号", type: "ro", width: "100" },
      { id: "abnormalType", name: "异常类型", type: "ro", width: "80",render: (v)=>{
        return getSelectValue(dataSelete.TMS_ABNORMAL_TYPE,v);
      } },
      { id: "relationBillno", name: "关联单号", type: "ro", width: "80" },
      { id: "relationVoucherno", name: "关联车牌", type: "ro", width: "100" },
      { id: "abnormalDescription", name: "异常原因", type: "ro", width: "80" },
      { id: "createperson", name: "异常上报人", type: "ro", width: "120" },
      { id: "createtime", name: "异常上报时间", type: "ro", width: "120" },
      { id: "abnormalLocation", name: "异常上报地点", type: "ro", width: "120" },
      { id: "auditDate", name: "审核时间", type: "ro",width: "130" },
      { id: "auditPerson", name: "审核人", type: "ro", width: "80" },
      { id: "auditOpinion", name: "审核结论", type: "ro", width: "80" },
      { id: "auditDescription", name: "审核描述", type: "ro", width: "80" },
      { id: "memo", name: "备注", type: "ro", width: "80" },
      {
          id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 80, render: function () {
              return "查看图片^abnormalRecord_details";
          }
      }
    ],
    multiselect: false,
    fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "startTime", className: "layui-col-md3",label: "开始时间:" },
          { type: "calendar", name: "endTime",className: "layui-col-md3", label: "结束时间:" },
          {
            className: "layui-col-md3",
              type: "selectTable",
              placeholder: "请选择配送中心",
              readonly: false,
              table: userTable,
              tableWidth: 500,
              tableHeight: 400,
              fieldText: "companyname",
              fieldValue: "companycode",
              name: "buyername1",
              label: "配送中心:",
              listener: function (consignorGrid) {
                  setMethod(consignorGrid, userTable, function (data) {
                      var datad = {};
                      datad.branchid = data.companyid;
                      grid.getToolBarForm().setFormData(datad);
                  });
              }
          },
          { type: "hidden", name: "branchid", label: "配送中心:" },
          { type: "input", name: "relationBillno", className: "layui-col-md3",label: "关联单号:" },
          { type: "combo", name: "abnormalType", className: "layui-col-md3",label: "异常类型:",options: dataSelete.TMS_ABNORMAL_TYPE },
          { type: "input", name: "relationVoucherno",className: "layui-col-md3", label: "关联车牌:" },
          {
            type: "block", className: "layui-col-md3 form_btn_left",
            list: [
              { type: "button",className: "btn-normal", name: "querycarr", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "car_Reset", className: "normal", value: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md3 right",
            list: [
              { type: "button", name: "car_export", className: "btn-normal", value: "导 出" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/abnormalRecordsQuery/abnormalRecordsQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({startTime: FormatDate(new Date()) + " 00:00:00" ,endTime: FormatDate(new Date()) + " 23:59:59" }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("abnormalRecord_zhu");
  grid.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = grid.getToolBarForm().getFormData();
      if (jsonData.startTime == null) {
        jsonData.startTime = FormatDate(new Date()) + " 00:00:00";
      }
      else {
        jsonData.startTime = Prolog.formatDate(jsonData.startTime) + " 00:00:00";
      }
      if (jsonData.endTime == null) {
        jsonData.endTime = FormatDate(new Date()) + " 23:59:59";
      }
      else {
        jsonData.endTime = Prolog.formatDate(jsonData.endTime) + " 23:59:59";
      }
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //重置
    if (name == "car_Reset") {
      grid.getToolBarForm().getDForm().setText("buyername1", "");
      grid.getToolBarForm().clear();
    }
    //导出
    if(name == "car_export"){
      unfilledWorktime_export();
    }
  });
  //主界面导出事件
  function unfilledWorktime_export() {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    if (grid.getRowsNum() <= 0) {
        PlgDialog.close(pload);
        PlgDialog.msg("没有要导出的数据");
        return;
    }
    let url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/abnormalRecordsQuery/abnormalRecordsExport";
    let name = "异常记录查询" + Prolog.formatDate(new Date(), "%YY%MM%DD") + ".xls";
    var jsonData = grid.getToolBarForm().getFormData();
    if (jsonData.startTime == null) {
      jsonData.startTime = FormatDate(new Date()) + " 00:00:00";
    }
    else {
      jsonData.startTime = Prolog.formatDate(jsonData.startTime) + " 00:00:00";
    }
    if (jsonData.endTime == null) {
      jsonData.endTime = FormatDate(new Date()) + " 23:59:59";
    }
    else {
      jsonData.endTime = Prolog.formatDate(jsonData.endTime) + " 23:59:59";
    }
    Prolog.exportFile(url, name, "application/json", "post", jsonData);
    PlgDialog.close(pload);

}
  //查看图片
  window.abnormalRecord_details = function (rid) {
      grid.selectRowById(rid);
      var data = grid.getSelectedRowData();
      let request = {
        bizId: data.seqno,
        bizType: 0,
        indexCol1: "02",
        enterpriseid: userGJ.p_enterpriseid
      };
      querySysPicture(request).then((res)=>{
        imageView(res)
      });
  };
};