/**
   * guoj
   * 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 startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";
  let dataSelect;
  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_URGENTIDENTIFY" }, { "objectcode": "SIGN_STATUS" },
  { "objectcode": "TMS_SHIP_BILLTYPE" }], function (data) {
    data.data.TMS_URGENTIDENTIFY.unshift({ "text": "", "value": "" });
    data.data.SIGN_STATUS.unshift({ "text": "", "value": "" });
    dataSelect = data.data;
  });
  let ddfrom;
  //跳转页面
  var carousel = layui.carousel;
  var car = carousel.render({
    elem: "#signAdjustment_div",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  //跳转方法
  var tt = "signAdjustment_zhu_div";

  function changePanel() {
    car.slide(tt);
    tt = tt == "signAdjustment_xi1" ? "signAdjustment_zhu_div" : "signAdjustment_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
  };
  //分页参数
  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: "billtype", name: "运单类型", type: "ro", width: "80", render: (v) => {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      { id: "linename", name: "线路名称", type: "ro", width: "80" },
      { id: "billdate", name: "单据日期", type: "ro", width: "130" },
      {
        id: "urgentIdentify", name: "是否急单", type: "ro", width: "80", render: (v) => {
          return getSelectValue(dataSelect.TMS_URGENTIDENTIFY, v);
        }
      },
      //{ id: "refbillno", name: "业务单号", type: "ro", width: "120" },
      //{ id: "refbilltype", name: "业务类型", type: "ro", width: "120" },
      { id: "carriername", name: "承运商", type: "ro", width: "120" },
      { id: "arriverName", name: "客户名称", type: "ro", width: "130" },
      { id: "arriveAddress", 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: "signqty", name: "签收件数", type: "ro", width: "80" },
      { id: "receiptqty", name: "确认件数", type: "ro", width: "80" },
      { id: "memo", name: "备注", type: "ro", width: "80" },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 80, render: function () {
          return "信息比对^signAdjustment_details";
        }
      }
    ],
    multiselect: false,
    fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "startTime", className: "layui-col-md3", label: "开始时间:", value: startTime },
          { type: "calendar", name: "endTime", className: "layui-col-md3", label: "结束时间:", value: endTime },
          {
            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: "配送中心:" },
          { type: "input", name: "shipbillno", className: "layui-col-md3", label: "运单号:" },
          //{ type: "input", name: "refbillno", className: "layui-col-md3",label: "业务单号:" },
          { type: "combo", name: "urgentIdentify", className: "layui-col-md3", label: "是否急单:", options: dataSelect.TMS_URGENTIDENTIFY },
          { type: "input", name: "lineno", className: "layui-col-md3", label: "运输线路:" },
          {
            type: "block", className: "layui-col-md3 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "querycarr", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "car_Reset", className: "normal", value: "重 置" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/signDataAdjust/signDataAdjustQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ startTime: startTime, endTime: endTime }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("signAdjustment_zhu");
  grid.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = grid.getToolBarForm().getFormData();
      jsonData.startTime = jsonData.startTime ? Prolog.formatDate(jsonData.startTime) + " 00:00:00" : "";
      jsonData.endTime = jsonData.endTime ? Prolog.formatDate(jsonData.endTime) + " 23:59:59" : "";
      if (jsonData.startTime > jsonData.endTime) {
        layer.msg("开始时间不能大于结束时间");
        return false;
      }
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //重置
    if (name == "car_Reset") {
      grid.getToolBarForm().getDForm().setText("buyername1", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ startTime: startTime, endTime: endTime });
    }
  });
  //信息比对
  window.signAdjustment_details = function (rid, rdata) {
    grid.selectRowById(rid);
    new PlgPanel({
      renderer: "signAdjustment_xi",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [{
            label: "配送中心",
            value: rdata.branchname
          },
          {
            label: "运单号",
            value: rdata.shipbillno
          },
          {
            label: "运单类型",
            //value: rdata.billtype,
            value: getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, rdata.billtype)
          },
          {
            label: "总件数",
            value: rdata.rsumcases
          }],
          [
            {
              label: "线路名称",
              value: rdata.linename
            },
            {
              label: "单据日期",
              value: rdata.billdate
            },
            {
              label: "承运商",
              value: rdata.carriername
            }
          ],
          [
            {
              label: "客户",
              value: rdata.arriverName
            },
            {
              label: "客户地址",
              value: rdata.arriveAddress
            }
          ]
        ]
      }
    });
    ddfrom = new PlgForm({
      renderer: "signAdjustment_xi2",
      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: "calendar", name: "signdate", label: "签收时间:", className: "layui-col-md3" },
            { type: "combo", name: "signstatus", label: "签收状态:", className: "layui-col-md3", options: dataSelect.SIGN_STATUS },
            {
              type: "input", name: "signqty", label: "签收件数 :", className: "layui-col-md3"
            },
            { type: "input", name: "rejectreason", label: "签收拒收原因:", className: "layui-col-md3" },
            { type: "calendar", name: "receiptdate", label: "确认时间:", className: "layui-col-md3" },
            {
              type: "combo", name: "receiptstatus", label: "确认状态:", className: "layui-col-md3", options: dataSelect.SIGN_STATUS
            },
            {
              type: "input", name: "receiptqty", label: "确认件数:", className: "layui-col-md3"
            },
            {
              type: "input", name: "receiptrejectreason", label: "确认拒收原因:", className: "layui-col-md3"
            }
          ]
        }]
    });
    ddfrom.setFormData(rdata);
    changePanel();
  };
  //返回
  document.querySelector("#signAdjustment_back1").onclick = function () {
    changePanel();
  };
  //确认
  document.querySelector("#signAdjustment_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    var feik = ddfrom.validate();
    if (!feik) {
      return;
    }
    data.signdate = FormatDateTime(data.signdate);
    data.receiptdate = FormatDateTime(data.receiptdate);
    //调用ajax方法
    utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/signDataAdjust/signDataAdjustConfirm", "post", JSON.stringify(data), function (data) {
      if (data.success) {
        grid.reload();
        ddfrom.clear();
        changePanel();
      }
    }, "application/json");
  };
};