/**
   * guoj
   * 2020.06.23
   * 提货仓库修改
      */
    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 dataselect = [];
  let gridB;

  //获取下拉框
  getSelectGJ([{ "objectcode": "APPOINT_BILL_STATUS" },{ "objectcode": "TIME_SECTION" },
  { "objectcode": "TMS_SHIP_BILLTYPE" }], function (data) {
    dataselect = data.data;
    dataselect.TMS_SHIP_BILLTYPE.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
};
  //预约方table
  var company_Table = {
      columns: [
          { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
          { id: "companycode", name: "承运商编号", type: "ro" },
          { id: "companyname", name: "承运商名称", type: "ro" }
      ],
      multiselect: false,

      url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyCarrier",
      type: "post", //数据提交方式，默认为get
      contentType: "application/json",
      pagebar: { type: "simple" },
      params: JSON.stringify({"companytype": "Y"}), //查询提交参数，分页参数默认为pageNum,pageSize
      page: true, //是否启用分页
      totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "shipbillno", name: "id", type: "ro", width: "80", hidden: true },
      { id: "status", name: "单据状态", type: "ro", width: "100" ,render: (v)=>{
        return getSelectValue(dataselect.APPOINT_BILL_STATUS,v);
      }},
      { id: "branchname", name: "发货仓库", type: "ro", width: "80" },
      { id: "billdate", name: "预约时间", type: "ro", width: "80" },
      { id: "appointBillNo", name: "预约单号", type: "ro", width: "130" },
      { id: "appointVehicle", name: "车辆", type: "ro", width: "80" },
      { id: "appointDriver", name: "司机", type: "ro", width: "80" },
      { id: "shipbillno", name: "运单号", type: "ro", width: "80" },
      { id: "billtype", name: "单据类型", type: "ro", width: "80" ,render: (v)=>{
        return getSelectValue(dataselect.TMS_SHIP_BILLTYPE,v);
      }},
      { id: "customerName", name: "承运商", type: "ro",width: "80" },
      { id: "sumcases", name: "总件数", type: "ro", width: "80" },
      { id: "sumgoodsweight", name: "总重量", type: "ro", width: "80" },
      { id: "sumgoodsvol", name: "总体积", type: "ro",width: "80" },
      { id: "appointDate", name: "预约提货日期", type: "ro", width: "130" },
      { id: "appointTime", name: "预约提货时间", type: "ro", width: "130",render: (v)=>{
        return getSelectValue(dataselect.TIME_SECTION,v);
      } },
      { id: "memo", name: "备注", type: "ro",width: "80" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "sdate",className: "layui-col-md3", label: "开始时间:" },
          { type: "calendar", name: "edate",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: "提货点:" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择承运商",
            readonly: false,
            table: company_Table,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companycode",
            name: "customercode1",
            label: "承运商:",
            listener: function(consignorGrid) {
                setMethod(consignorGrid, company_Table, function(data) {
                    var fromdata = grid.getToolBarForm().getFormData();
                    data = fromdata.customercode1.length == 2 ? "" : data;
                    var datad = {};
                    datad.customerId = data.companyid;
                    grid.getToolBarForm().setFormData(datad);
                });
            }
          },
          { type: "hidden", name: "customerId", label: "承运商:"},
          { type: "input", name: "appointVehicle",className: "layui-col-md3", label: "车牌号:" },
          { type: "input", name: "appointDriver", className: "layui-col-md3",label: "司机:" },
          { type: "combo", name: "billType", className: "layui-col-md3", label: "单据类型:" ,options: dataselect.TMS_SHIP_BILLTYPE},
          { type: "input", name: "shipbillno",className: "layui-col-md3", label: "运单号:" },
          {
            type: "block", className: "layui-col-md6 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-md6 right",
            list: [
              { type: "button", className: "btn-addBtn btn-normal", name: "warehouseUpdate_print", value: "提货地点修改" }
            ]
          }
        ]
      }],

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bookingManage/branchUpdateQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ sdate: ship_FormatDate(new Date()) + " 00:00:00", edate: FormatDate(new Date()) + " 23:59:59" }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
   grid = new PlgGrid(opts);
  grid.renderTo("warehouseUpdate_zhu");
  grid.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
  grid.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()),edate: FormatDate(new Date())});
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = grid.getToolBarForm().getFormData();
      if (jsonData.sdate == null) {
        jsonData.sdate = ship_FormatDate(new Date()) + " 00:00:00";
      }
      else {
        jsonData.sdate = Prolog.formatDate(jsonData.sdate) + " 00:00:00";
      }
      if (jsonData.edate == null) {
        jsonData.edate = FormatDate(new Date()) + " 23:59:59";
      }
      else {
        jsonData.edate = Prolog.formatDate(jsonData.edate) + " 23:59:59";
      }
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //重置
    if (name == "car_Reset") {
      grid.getToolBarForm().getDForm().setText("buyername1", "");
      grid.getToolBarForm().getDForm().setText("customercode1", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()),edate: FormatDate(new Date())});
    }
    //提货地点修改
    if (name == "warehouseUpdate_print") {
      let orderSingle_from1 ;
      var id = grid.getSelectedRowData();
      if(id == "" || id == null){
        layer.msg("请选择一行数据");
        return;
      }
      layer.open({
        title: "<h1>提货地点修改</h1>",
        shadeClose: true,
        area: ["1000px", "530px"],
        btn: ["提交","返回"],
        content: " <div id=\"warehouseUpdate_app2\"></div><div style='height:80%;' id=\"warehouseUpdate_app3\"></div>", //iframe的url
        yes: function (index) {
          setTimeout(() => {
            //如果设定了yes回调，需进行手工关闭
            let dataitem = orderSingle_from1.getFormData();

            var length = gridB.getGrid().getRowsNum();
            let DataList = [];
            for (var i = 0; i < length; i++) {
                let rd = gridB.getRowData(gridB.getRowId(i));
                rd.appointBillNo = dataitem.appointBillNo;
                rd.sumloadpcs = dataitem.sumloadpcs ;
                rd.shipbillno = dataitem.shipbillno;
                rd.branchid = dataitem.branchid;
                rd.branchname = dataitem.branchname;
                rd.memo = dataitem.memo;
                rd.driverId = dataitem.driver1st;
                rd.oldPcs = dataitem.sumpcs == null ? 0 : dataitem.sumpcs;
                DataList.push(rd);
            }
            utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bookingManage/branchUpdate", "post", JSON.stringify(DataList), (data)=>{
              if(data.success){
                layer.msg("成功");
                layer.close(index);
                grid.reload();
              }
            }, "application/json");
          }, 100);
        }
      });

      let orderSingle_div1 = {
        renderer: "warehouseUpdate_app2",
        items: [
            { type: "settings", PoSingle: "label-left", labelWidth: 80 },
            { type: "input", name: "shipbillno", label: "运单号:", width: 365 },
            { type: "newcolumn" },
            { type: "input", name: "sumloadpcs", label: "已装数量:", width: 365 },
            { type: "newcolumn" },
            {
                width: 365,
                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;
                        datad.branchname = data.companyname;
                        orderSingle_from1.setFormData(datad);
                    });
                }
            },
            { type: "hidden", name: "branchid", label: "第二配送中心:", width: 365 },
            { type: "hidden", name: "branchname", label: "第二配送中心:", width: 365 },
            { type: "newcolumn" },
            { type: "input", name: "sumcases", label: "总箱数:", width: 365 },
            { type: "newcolumn" },
            { type: "input", name: "memo", label: "备注:", width: 365 }
        ]
      };
      orderSingle_from1 = new PlgForm(orderSingle_div1);
      orderSingle_from1.setFormData(id);
      orderSingle_from1.disableItem("shipbillno");
      orderSingle_from1.disableItem("sumloadpcs");
      orderSingle_from1.getDForm().setText("buyername1", id.branchname);

      //到场车辆明细信息
      var optsB = {
        //terrace显示全部，card分页显示
        skin: "card",
        //renderer:"grid",
        columns: [
          { id: "seqno", name: "id", type: "ro", width: "130", hidden: true },
          { id: "shipbillno", name: "运单号", type: "ro", width: "100" },
          { id: "seqno", name: "行号", type: "ro", width: "110" },
          { id: "itemno", name: "商品编号", type: "ro", width: "110" },
          { id: "itemname", name: "商品名称", type: "ro", width: "80" },
          { id: "pcs", name: "要货量", type: "ro", width: "80" },
          { id: "loadpcs", name: "<span style='color:#096dd9;'>已装量</span>", type: "ed" }
        ],
        multiselect: false,

        url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bookingManage/tmsShipbillDQuery",//数据接口
        type: "post",//数据提交方式，默认为get
        contentType: "application/json",
        params: JSON.stringify({shipbillno: id.shipbillno}), //查询提交参数，分页参数默认为pageNum,pageSize
        page: false, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
      };
      gridB = new PlgGrid(optsB);
      gridB.renderTo("warehouseUpdate_app3");
      gridB.getGrid().enableEditEvents(true, false, true);
      gridB.loadData();
      //离开事件
      gridB.attachEvent("onEditCell", function (stage, rId, cInd, nValue) {
          if (stage == 2) {
            gridB.setCellValue(rId, cInd, nValue);
            var length = gridB.getGrid().getRowsNum();
            let sunqty = 0;
            for (var i = 0; i < length; i++) {
                let rd = gridB.getRowData(gridB.getRowId(i));
                rd.loadpcs = rd.loadpcs == "" || rd.loadpcs == null ? 0 : rd.loadpcs;
                sunqty += rd.loadpcs * 1;
            }
            orderSingle_from1.setFormData({sumloadpcs: sunqty});
          }
          return true;
      });
    }
  });
};