/**
 * guoj
 * 2020.10.19
 * 托运交接
 */
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 sdate = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var edate = Prolog.formatDate(new Date()) + " 23:59:59";
  let dataSelect;

  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_REF_BILLTYPE" }, { "objectcode": "TMS_SHIP_BILLTYPE" }], function (data) {
    data.data.TMS_REF_BILLTYPE.unshift({ "text": "", "value": "" });
    dataSelect = data.data;
  });
  let ddfrom;
  //跳转页面
  var carousel = layui.carousel;
  var car = carousel.render({
    elem: "#deliveryOfConsignment_div",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  //跳转方法
  var tt = "deliveryOfConsignment_zhu_div";

  function changePanel() {
    car.slide(tt);
    tt = tt == "deliveryOfConsignment_xi1" ? "deliveryOfConsignment_zhu_div" : "deliveryOfConsignment_xi1";
  }
  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
  };
  // 中转站table
  var transferTable = {
    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
  };
  //客户table
  var companyTable = {
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "编号", type: "ro" },
      { id: "companyname", name: "名称", type: "ro" }
    ],
    multiselect: false,
    pagebar: { type: "simple" },
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyCustomer", //数据接口
    type: "post", //数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ companytypes: ["C"] }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //货主table
  var connerTable = {
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "编号", type: "ro" },
      { id: "companyname", name: "名称", type: "ro" }
    ],
    multiselect: false,
    pagebar: { type: "simple" },
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyConsignor", //数据接口
    type: "post", //数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ companytypes: ["H"] }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //承运商table
  var companynerTable = {
    columns: [
      { id: "carrierId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "carrierCode", name: "单位编号", type: "ro" },
      { id: "carrierName", name: "单位名称", type: "ro" }
    ],
    multiselect: false,
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tmsBaseCarrier/queryTmsBaseCarrier", //数据接口
    type: "post", //数据提交方式，默认为get
    pagebar: { type: "simple" },
    contentType: "application/json",
    params: JSON.stringify({ pageNum: 1, pageSize: 500 }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "shipbillid", name: "id", type: "ro", width: "80", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: "100" },
      { id: "shipbillno", name: "运单号", type: "ro", width: "100" },
      {
        id: "consignorname", name: "货主", type: "ro", width: "80"
      },
      {
        id: "billtype", name: "运单类型", type: "ro", width: "100", render: (v) => {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      {
        id: "linenane", name: "线路名称", type: "ro", width: "80"
      },
      { id: "billdate", name: "单据日期", type: "ro", width: "120" },
      {
        id: "urgentIdentify", name: "是否急单", type: "ro", width: "120", render: (v) => {
          return v == "01" ? "是" : "否";
        }
      },
      { id: "refbillno", name: "业务单号", type: "ro", width: "120" },
      {
        id: "refbilltype", name: "业务类型", type: "ro", width: "130", render: (v) => {
          return getSelectValue(dataSelect.TMS_REF_BILLTYPE, v);
        }
      },
      { id: "shiptoCustomerName", name: "客户名称", type: "ro", width: "80" },
      { id: "shiptoAddress", name: "客户地址", type: "ro", width: "80" },
      { id: "rsumcases", name: "件数", type: "ro", width: "80" },
      { id: "rsumgoodsweight", name: "重量", type: "ro", width: "80" },
      { id: "rsumgoodsvol", name: "体积", type: "ro", width: "80" },
      { id: "memo", name: "备注", type: "ro", width: "80" }
    ],
    multiselect: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "sdate", className: "layui-col-md3", label: "开始时间:", value: sdate },
          { type: "calendar", name: "edate", className: "layui-col-md3", label: "结束时间:", value: edate },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择配送中心",
            readonly: false,
            table: userTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchid",
            label: "配送中心:"
          },
          { type: "input", name: "shipbillno", className: "layui-col-md3", label: "运单号:" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择货主",
            readonly: false,
            table: connerTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "consignor",
            label: "货主:"
          },
          { type: "input", name: "refbillno", className: "layui-col-md3", label: "业务单号:" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择客户",
            readonly: false,
            table: companyTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "shiptoCustomerId",
            label: "客户:"
          },
          {
            type: "block", className: "layui-col-md2 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-md1 right",
            list: [
              { type: "button", className: "btn-addBtn btn-normal", name: "arrivalConfirmation_print", value: "托运交接" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/shipHandoverQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ sdate: sdate, edate: edate }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("deliveryOfConsignment_zhu");
  grid.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = grid.getToolBarForm().getFormData();
      jsonData.sdate = jsonData.sdate ? Prolog.formatDate(jsonData.sdate) + " 00:00:00" : "";
      jsonData.edate = jsonData.edate ? Prolog.formatDate(jsonData.edate) + " 23:59:59" : "";
      if (jsonData.sdate > jsonData.edate) {
        layer.msg("开始时间不能大于结束时间");
        return false;
      }
      jsonData.branchid = jsonData.branchid ? JSON.parse(jsonData.branchid)[0] : "";
      jsonData.consignor = jsonData.consignor ? JSON.parse(jsonData.consignor)[0] : "";
      jsonData.shiptoCustomerId = jsonData.shiptoCustomerId ? JSON.parse(jsonData.shiptoCustomerId)[0] : "";
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //重置
    if (name == "car_Reset") {
      grid.getToolBarForm().getDForm().setText("branchid", "");
      grid.getToolBarForm().getDForm().setText("consignor", "");
      grid.getToolBarForm().getDForm().setText("shiptoCustomerId", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ sdate: sdate, edate: edate });
    }
    //托运交接
    if (name == "arrivalConfirmation_print") {
      let dataList = getDeleteAll(grid);
      if (dataList == null || dataList == "") {
        return;
      }
      let sumPcs = 0;
      for (var i = 0; i < dataList.length; i++) {
        let rd = grid.getRowData(dataList[i]);
        sumPcs += rd.rsumcases * 1;
        dataList[i] = rd;
      }
      ddfrom = new PlgForm({
        renderer: "deliveryOfConsignment_xi",
        items: [
          { type: "settings", labelWidth: 90, labelAlign: "right" },
          {
            type: "row",
            list: [
              { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
              { type: "hidden", name: "stationid", label: "id:", className: "layui-col-md3" },
              { type: "input", name: "signqty1", label: "订单数:", className: "layui-col-md6", disabled: true },
              { type: "input", name: "signqty2", label: "总件数:", className: "layui-col-md6", disabled: true },
              { type: "combo", name: "type", label: "托运类型:", className: "layui-col-md3", options: [{ text: "干线", value: "01" }, { text: "支线", value: "02" }] },
              {
                className: "layui-col-md3",
                type: "selectTable",
                placeholder: "请选择中转站",
                readonly: false,
                table: transferTable,
                tableWidth: 500,
                tableHeight: 400,
                fieldText: "companyname",
                fieldValue: "companyid",
                name: "transferList",
                label: "中转站:<span class='dhxform_item_required'>*</span>",
                listener: {
                  "afterSelect": function (isSelected, selectId, sgrid) {
                    let sdata = sgrid.getRowData(selectId);
                    ddfrom.setFormData({
                      transferid: isSelected ? sdata.companyid : "",
                      transfername: isSelected ? sdata.companyname : ""
                    });
                  }
                }
              },
              { type: "hidden", name: "transferid" },
              { type: "hidden", name: "transfername" },
              {
                className: "layui-col-md3",
                type: "selectTable",
                placeholder: "请选择承运商",
                readonly: false,
                table: companynerTable,
                tableWidth: 500,
                tableHeight: 400,
                fieldText: "carrierName",
                fieldValue: "carrierId",
                name: "signqty10",
                label: "承运商:<span class='dhxform_item_required'>*</span>",
                listener: {
                  "afterSelect": function (isSelected, selectId, sgrid) {
                    let sdata = sgrid.getRowData(selectId);
                    ddfrom.setFormData({
                      carriername: isSelected ? sdata.carrierName : "",
                      carrierno: isSelected ? sdata.carrierCode : "",
                      carrierid: isSelected ? sdata.carrierId : ""
                    });
                  }
                }
              },
              { type: "hidden", name: "carrierid", label: "承运商:", className: "layui-col-md3" },
              { type: "hidden", name: "carriername", label: "承运商:", className: "layui-col-md3" },
              { type: "hidden", name: "carrierno", label: "承运商:", className: "layui-col-md3" },
              { type: "input", name: "handoverProof", label: "交接凭证:", className: "layui-col-md3", required: true },
              { type: "input", name: "handoverPerson", label: "交接人:", className: "layui-col-md3", required: true },
              { type: "input", name: "remark", label: "交接备注:", className: "layui-col-md12" }
            ]
          }]
      });

      ddfrom.attachComboEvent("type", "onChange", function (value) {
        ddfrom.setFormData({ transferid: "", transfername: "", transferList: "" });
        ddfrom.setText("transferList", "");
        if (value == "01") {
          ddfrom.showItem("transferList");
        }
        else {
          ddfrom.hideItem("transferList");
        }
      });

      ddfrom.setFormData({ signqty1: dataList.length, signqty2: sumPcs });

      changePanel();
    }
  });
  //返回
  document.querySelector("#deliveryOfConsignment_back1").onclick = function () {
    changePanel();
  };
  //确认
  document.querySelector("#deliveryOfConsignment_save").onclick = function () {
    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    let dataList = getDeleteAll(grid);
    for (var i = 0; i < dataList.length; i++) {
      let rd = grid.getRowData(dataList[i]);
      dataList[i] = rd;
    }
    //信息比对
    let url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/shipHandover";
    let datai = ddfrom.getFormData();

    if (datai.type === "01" && !datai.transferid) {
      layer.msg("托运类型为干线时，中转站必填");
      return false;
    }

    datai.list = dataList;
    delete datai.signqty10;
    delete datai.transferList;
    //调用ajax方法
    utilAjaxGJ(url, "post", JSON.stringify(datai), function (data) {
      if (data.success) {
        grid.reload();
        changePanel();
      }
    }, "application/json");
  };
};