/**
 * zzx
 * 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 master_grid, edit_form;
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: "#abnormalAudit",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", // 设置容器宽度
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#abnormalAudit_master", "#abnormalAudit_edit"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $(document).on("click", "#abnormalAudit_edit_return", function () {
    changePanel("#abnormalAudit_master");
  });

  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";
  // 获取下拉框
  var dataSelect = {
    TMS_ABNORMAL_TYPE: [],
    TMS_VEHICLE_TYPE: [],
    TIME_SECTION: []
  };
  getSelectGJ([
    { "objectcode": "TMS_ABNORMAL_TYPE" }], function (data) {
      dataSelect = data.data;
      dataSelect.TMS_ABNORMAL_TYPE.unshift({ "value": "", "text": "" });
    });

  // 配送中心table
  var branchTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "配送中心编号", type: "ro" },
      { id: "companyname", name: "配送中心名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 200, labelWidth: 100 },
      {
        type: "block", id: "bk", blockOffset: 0, list: [
          { type: "input", name: "companycode", label: "配送中心编号:", width: 120 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }
    ],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "S" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "seqno", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: 120 },
      { id: "seqno", name: "异常编号", type: "ro", width: 120 },
      {
        id: "abnormalType", name: "异常类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_ABNORMAL_TYPE, v);
        }
      },
      { id: "relationBillno", name: "关联单号", type: "ro", width: 120 },
      { id: "relationVoucherno", name: "关联车牌", type: "ro", width: 120 },
      { id: "abnormalDescription", name: "异常原因", type: "ro", width: 180 },
      { id: "createperson", name: "异常上报人", type: "ro", width: 120 },
      { id: "createtime", name: "异常上报时间", type: "ro", width: 120 },
      { id: "abnormalLocation", name: "异常上报地点", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 },
      {
        id: "op", name: "操作", type: "button", align: "left", fixed: "right", width: "100", render: function () {
          return "查看图片^abnormalAudit_imageview";
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 70, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", className: "layui-col-md3", name: "startTime", label: "开始时间:", value: startTime },
          { type: "calendar", className: "layui-col-md3", name: "endTime", label: "结束时间:", value: endTime },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择配送中心",
            readonly: false,
            table: branchTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchidList",
            label: "配送中心:",
            listener: function (Grid) {
              setMethod(Grid, branchTable, function () { });
            }
          },
          { type: "input", className: "layui-col-md3", name: "relationBillno", label: "关联单号:" },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "abnormalType", label: "异常类型:", options: dataSelect.TMS_ABNORMAL_TYPE },
          { type: "input", className: "layui-col-md3", name: "relationVoucherno", label: "关联车牌:" },
          {
            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: "重 置" }

            ]
          },
          {
            type: "block", className: "layui-col-md3 right",
            list: [
              { type: "button", className: "btn-normal", name: "master_review", value: "审 核" }
            ]
          }
        ]
      }
    ],
    fixedLast: true, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/abnormalAudit/abnormalAuditQuery", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ startTime, endTime }),
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("abnormalAudit_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.startTime = request.startTime ? Prolog.formatDate(request.startTime) + " 00:00:00" : "";
        request.endTime = request.endTime ? Prolog.formatDate(request.endTime) + " 23:59:59" : "";
        if (request.startTime > request.endTime) {
          layer.msg("开始时间不能大于结束时间");
          return false;
        }
        request.branchid = request.branchidList ? JSON.parse(request.branchidList)[0] : "";
        dealNullQueryCondition(request);
        delete request.branchidList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setFormData({ startTime, endTime });
        master_grid.getToolBarForm().setText("branchidList", "");
        break;
      case "master_review":
        var rid = master_grid.getSelectedRowId();
        if (!rid) {
          layer.msg("请选择数据");
          return false;
        }
        var param = {
          bizId: rid,
          bizType: 0,
          enterpriseid: userinfo.p_enterpriseid,
          indexCol1: "02"
        };
        var rdata = master_grid.getRowData(rid);
        rdata.abnormalTypeName = getSelectValue(dataSelect.TMS_ABNORMAL_TYPE, rdata.abnormalType);
        querySysPicture(param).then(res => {
          edit_form.setFormData(rdata);
          $("#abnormalAudit_imageList").html("");
          res.forEach(item => {
            let html = "";
            html += "<div style='display: inline-block;margin-right: 20px;'>";
            html += "   <img src='" + item.fileUrl + "' class='abnormalReport_upload_image' style='height: 100px;'>";
            html += "</div>";
            let htmljs = $(html);
            // 点击展示
            htmljs.find(".abnormalReport_upload_image").on("click", function () {
              getImgDataRecon(res, item.fileUrl);
            });
            $("#abnormalAudit_imageList").append(htmljs);
          });
          changePanel("#abnormalAudit_edit");
        });
        break;
    }
  });

  var edit_form_opts = {
    renderer: "abnormalAudit_edit_form",
    items: [
      { type: "settings", labelAlign: "left", labelWidth: 90 },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3 no-border", name: "seqno", label: "异常编号:", readonly: true },
          { type: "input", className: "layui-col-md3 no-border", name: "branchname", label: "配送中心:", readonly: true },
          { type: "input", className: "layui-col-md3 no-border", name: "abnormalTypeName", label: "异常类型:", readonly: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3 no-border", name: "relationBillno", label: "关联单号:", readonly: true },
          { type: "input", className: "layui-col-md3 no-border", name: "relationVoucherno", label: "关联车牌:", readonly: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md9 no-border", name: "abnormalDescription", label: "异常描述:", readonly: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "label", className: "layui-col-md9 type_label", name: "", label: "异常图片:<div class=\"img_view\" id=\"abnormalAudit_imageList\">", labelWidth: 800 }
        ]
      },
      {
        type: "row",
        list: [
          { type: "label", className: "layui-col-md1 type_label", name: "auditOpinion", label: "审核结论:" },
          { type: "radio", className: "layui-col-md1", name: "auditOpinion", value: "1", label: "受理", labelWidth: 40, width: 50, checked: true },
          { type: "radio", className: "layui-col-md1", name: "auditOpinion", value: "0", label: "不受理", labelWidth: 40, width: 50 }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md9", name: "auditDescription", label: "审核描述:", width: 600, rows: 3 }
        ]
      }
    ]
  };
  edit_form = new PlgForm(edit_form_opts);

  // 查看图片
  window.abnormalAudit_imageview = function (rid) {
    let request = {
      bizId: rid,
      bizType: 0,
      enterpriseid: userinfo.p_enterpriseid,
      indexCol1: "02"
    };
    querySysPicture(request).then(res => {
      imageView(res);
    });
  };

  // 图片放大
  function getImgDataRecon(res, src) {
    for (let i = 0; i < res.length; i++) {
      let rd = res[i];
      if (rd.fileUrl == src) {
        imageView(res, i);
        return;
      }
    }
  }

  // 保存
  $("#abnormalAudit_edit_save").on("click", function () {
    let { abnormalDescription, abnormalType, auditDescription, auditOpinion, branchid, branchname, relationBillno, relationVoucherno, seqno } = edit_form.getFormData();
    let request = {
      abnormalDescription,
      abnormalType,
      auditDescription,
      auditOpinion,
      branchid,
      branchname,
      relationBillno,
      relationVoucherno,
      seqno
    };
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/abnormalAudit/abnormalAuditConfirm", type: "post", contentType: "application/json", data: JSON.stringify(request),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          layer.closeAll();
          PlgDialog.msg(res.message);
          changePanel("#abnormalAudit_master");
          master_grid.reload();
          return true;
        }
                return false;

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