/**
 * zzx
 * 2020.10.26
 * 未装登记审核
 */
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;
  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";

  // 配送中心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
  };
  // 客户table
  var customerTable = {
    // 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: 70 },
      {
        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/queryCompanyCustomer",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "C" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  // 货主table
  var consignorTable = {
    // 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/queryCompanyConsignor",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "H" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  // 获取下拉框
  var dataSelect = {
    BILL_TYPE: [],
    URG_FLG: [],
    AUDIT_FLOW_STATUS: []
  };
  getSelectGJ([
    { "objectcode": "BILL_TYPE" },
    { "objectcode": "AUDIT_FLOW_STATUS" },
    { "objectcode": "URG_FLG" }
  ], function (data) {
    data.data.AUDIT_FLOW_STATUS.unshift({ value: "", text: "" });
    dataSelect = data.data;
  });

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "lineid", name: "运单id", type: "ro", width: "130", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: 120 },
      { id: "shipbillno", name: "运单编号", type: "ro", width: 120 },
      { id: "consignorname", name: "货主名称", type: "ro", width: 120 },
      { id: "loadbillno", name: "装车单号", type: "ro", width: 120 },
      {
        id: "billtype", name: "单据类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.BILL_TYPE, v);
        }
      },
      { id: "linenane", name: "线路名称", type: "ro", width: 120 },
      { id: "billdate", name: "单据时间", type: "ro", width: 120 },
      {
        id: "urgentIdentify", name: "是否急单", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.URG_FLG, v);
        }
      },
      { id: "refbillno", name: "业务单据号", type: "ro", width: 120 },
      { id: "refbilltype", name: "业务单据类型", type: "ro", width: 120 },
      { id: "shiptoCustomerName", name: "客户名称", type: "ro", width: 120 },
      { id: "shiptoAddress", name: "客户地址", type: "ro", width: 120 },
      { id: "rsumcases", name: "件数", type: "ro", width: 120 },
      { id: "rsumgoodsweight", name: "重量", type: "ro", width: 120 },
      { id: "rsumgoodsvol", name: "体积", type: "ro", width: 120 },
      { id: "abnormalReason", name: "异常原因", type: "ro", width: 120 },
      { id: "auditOpinion", name: "审核意见", type: "ro", width: 120 },
      {
        id: "status", name: "审核状态", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.AUDIT_FLOW_STATUS, v);
        }
      },
      { id: "memo", name: "备注", type: "ro", width: 120 }
    ],
    toolbar: [
      { type: "settings", labelWidth: 70, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", className: "layui-col-md3", name: "sdate", label: "开始时间:", value: startTime },
          { type: "calendar", className: "layui-col-md3", name: "edate", 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: "loadbillno", label: "装车单号:" },
          { type: "input", className: "layui-col-md3", name: "shipbillno", label: "运单号:" },
          { type: "input", className: "layui-col-md3", name: "refbillno", label: "业务单号:" },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择客户",
            readonly: false,
            table: customerTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "shiptoCustomerIdList",
            label: "客户:",
            listener: function (Grid) {
              setMethod(Grid, customerTable, function () { });
            }
          },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择货主",
            readonly: false,
            table: consignorTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "consignorList",
            label: "货主:",
            listener: function (Grid) {
              setMethod(Grid, consignorTable, function () { });
            }
          },
          {
            type: "block", className: "layui-col-md6 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-md6 right",
            list: [
              { type: "button", className: "btn-normal", name: "master_review", value: "未装审核" }
            ]
          }
        ]
      }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: true, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/shipbillAbnormal/shipbillAbnormalQuery", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ sdate: startTime, edate: endTime, abnormalType: "01" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("uninstalledRegistrationReview");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.sdate = request.sdate ? Prolog.formatDate(request.sdate) + " 00:00:00" : "";
        request.edate = request.edate ? Prolog.formatDate(request.edate) + " 23:59:59" : "";
        if (request.sdate > request.edate) {
          layer.msg("开始时间不能大于结束时间");
          return false;
        }
        request.branchid = request.branchidList ? JSON.parse(request.branchidList)[0] : "";
        request.shiptoCustomerId = request.shiptoCustomerIdList ? JSON.parse(request.shiptoCustomerIdList)[0] : "";
        request.consignor = request.consignorList ? JSON.parse(request.consignorList)[0] : "";
        request.abnormalType = "01";
        dealNullQueryCondition(request);
        delete request.branchidList;
        delete request.shiptoCustomerIdList;
        delete request.consignorList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setText("branchidList", "");
        master_grid.getToolBarForm().setText("shiptoCustomerIdList", "");
        master_grid.getToolBarForm().setText("consignorList", "");
        master_grid.getToolBarForm().setFormData({ sdate: startTime, edate: endTime });
        break;
      case "master_review":
        var ids = master_grid.getCheckedIds();
        if (!ids) {
          layer.msg("请选择数据");
          return false;
        }
        var idList = ids.split(",").map(item => {
          return {
            lineid: item
          };
        });
        reviewDialog(idList);
        break;
    }
  });

  // 未装审核弹窗
  function reviewDialog(idList) {
    let dialogForm;
    PlgDialog.open({
      title: "未装审核",
      shadeClose: true,
      type: 1, // 设置为1，手动控制dialog的开关
      area: ["450px", "250px"],
      btn: ["确定", "取消"],
      content: "<div id =\"uninstalledRegistrationReview_dialog\"></div>", //iframe的url
      yes: function (index) {
        let vdate = dialogForm.validate();
        if (!vdate) return false;
        let { auditOpinion } = dialogForm.getFormData();
        let request = {
          list: idList,
          auditOpinion
        };
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/shipbillAbnormal/shipbillAbnormalAudit", type: "post", contentType: "application/json", data: JSON.stringify(request),
          success: function (res) {
            PlgDialog.close(pload);
            if (res.success) {
              PlgDialog.close(index);
              PlgDialog.msg(res.message);
              master_grid.reload();
              return true;
            }
                        return false;
          },
          error: function () { PlgDialog.close(pload); return false; }
        });
      }
    });
    let dialogForm_opts = {
      renderer: "uninstalledRegistrationReview_dialog",
      items: [
        { type: "settings", position: "label-left", labelWidth: 60 },
        {
          type: "block", width: "*",
          list: [
            { type: "input", name: "auditOpinion", label: "审核意见:", width: 280, rows: 3, required: true }
          ]
        }
      ]
    };
    dialogForm = new PlgForm(dialogForm_opts);
  }
};