/**
 * zzx
 * 2020.07.29
 * 收货确认
 */
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 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 startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01";
  var endTime = Prolog.formatDate(new Date());
  // 获取下拉框
  var dataSelect = {
    TMS_URGENTIDENTIFY: [],
    TMS_REF_BILLTYPE: [],
    TMS_SHIP_BILLTYPE: []
  };
  getSelectGJ([
    { "objectcode": "TMS_URGENTIDENTIFY" },
    { "objectcode": "TMS_REF_BILLTYPE" },
    { "objectcode": "TMS_SHIP_BILLTYPE" }], function (data) {
      dataSelect = data.data;
      dataSelect.TMS_URGENTIDENTIFY.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: "shipbillno", name: "运单号", type: "ro", width: 120 },
      { id: "consignorname", name: "货主", type: "ro", width: 120 },
      {
        id: "billtype", name: "运单类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      { id: "billdate", name: "单据日期", type: "ro", width: 120 },
      {
        id: "urgentIdentify", name: "是否急单", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_URGENTIDENTIFY, v);
        }
      },
      { id: "refbillno", name: "业务单号", type: "ro", width: 120 },
      {
        id: "refbilltype", name: "业务类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_REF_BILLTYPE, v);
        }
      },
      { id: "customerName", name: "承运商", type: "ro", width: 120 },
      { id: "shiptoCustomerName", name: "客户名称", type: "ro", width: 120 },
      { id: "shiptoAddress", name: "客户地址", type: "ro", width: 180 },
      { id: "rsumcases", name: "件数", type: "ro", width: 120 },
      { id: "rsumgoodsweight", name: "重量(KG)", type: "ro", width: 120 },
      { id: "rsumgoodsvol", name: "体积(m³)", type: "ro", width: 120 },
      { id: "signdate", name: "签收时间", type: "ro", width: 120 },
      { id: "signcases", 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 "查看图片^receiptConfirmation_imageview";
        }
      }
    ],
    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: "shipbillno", label: "运单号:" },
          { type: "input", className: "layui-col-md3", name: "refbillno", label: "业务单号:" },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "urgentIdentify", label: "是否急单:", options: dataSelect.TMS_URGENTIDENTIFY },
          {
            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_confirm", value: "收货确认" }
            ]
          }
        ]
      }
    ],
    fixedLast: true, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: true, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/receiveConfirmQuery", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ sdate: startTime, edate: endTime }),
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("receiptConfirmation");
  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) : "";
        request.edate = request.edate ? Prolog.formatDate(request.edate) : "";
        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({ sdate: startTime, edate: endTime });
        master_grid.getToolBarForm().setText("branchidList", "");
        break;
      case "master_confirm":
        var rids = master_grid.getCheckedIds();
        if (!rids) {
          layer.msg("请选择数据");
          return false;
        }
        receiveConfirm(rids);
        break;
    }
  });

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

  // 收货确认
  function receiveConfirm(rids) {
    let volist = rids.split(",").map(item => {
      let element = master_grid.getRowData(item);
      return {
        shipbillid: element.shipbillid,
        shipbillno: element.shipbillno,
        seqno: element.seqno,
        appointVehicle: element.appointVehicle,
        branchid: element.branchid,
        branchname: element.branchname,
        rsumcases: element.rsumcases
      };
    });
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/receiveConfirm", type: "post", contentType: "application/json", data: JSON.stringify({ volist }),
      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; }
    });
  }
};