/**
   * guoj
   * 2020.08.08
   * 回单回退
   */
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 () => {
  let dataSelect;
  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_URGENTIDENTIFY" }, { "objectcode": "TMS_REF_BILLTYPE" },
  { "objectcode": "TMS_SHIP_BILLTYPE", "groupcode1": "APPOINT" }], function (data) {
    data.data.TMS_URGENTIDENTIFY.unshift({ "text": "", "value": "" });
    dataSelect = data.data;
  });
  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: "seqno", name: "id", type: "ro", width: "80", hidden: true },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 80, render: function () {
          return "查看图片^receiptBack_details";
        }
      },
      { id: "branchname", name: "配送中心", type: "ro", width: "100" },
      { id: "shipbillno", name: "运单号", type: "ro", width: "100" },
      { id: "consignor", 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: "100" },
      { id: "billdate", name: "单据日期", type: "ro", width: "130" },
      {
        id: "urgentIdentify", name: "是否急单", type: "ro", width: 100, render: (v) => {
          return getSelectValue(dataSelect.TMS_URGENTIDENTIFY, v);
        }
      },
      { id: "refbillno", name: "业务单号", type: "ro", width: "120" },
      {
        id: "refbilltype", name: "业务类型", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.TMS_REF_BILLTYPE, v);
        }
      },
      { id: "customerName", name: "承运商", type: "ro", width: 100 },
      { id: "shiptoCustomerName", name: "客户名称", type: "ro", width: 130 },
      { id: "shipToAddress", name: "客户地址", type: "ro", width: 130 },
      { id: "rsumcases", name: "件数", type: "ro", width: 100 },
      { id: "rsumgoodsweight", name: "重量", type: "ro", width: 100 },
      { id: "rsumgoodsvol", name: "体积", type: "ro", width: 100 },
      { id: "memo", name: "备注", type: "ro", width: 100 }
    ],
    multiselect: true,
    // fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
    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: "配送中心:" },
          { 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-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-normal", name: "returnV", value: "回 退" },
              { type: "newcolumn" },
              { type: "button", name: "btnOk", className: "btn-normal", value: "归 档" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/returnBillQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ sdate: ship_FormatDate(new Date()), edate: FormatDate(new Date()) }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("receiptBack_zhu");
  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());
      }
      else {
        jsonData.sdate = Prolog.formatDate(jsonData.sdate);
      }
      if (jsonData.edate == null) {
        jsonData.edate = FormatDate(new Date());
      }
      else {
        jsonData.edate = Prolog.formatDate(jsonData.edate);
      }
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //回退
    if (name == "returnV") {
      let listData = getDeleteAll(grid);
      if (listData == "" || listData == null) {
        return;
      }
      for (var i = 0; i < listData.length; i++) {
        listData[i] = grid.getRowData(listData[i]);
      }
      //调用ajax方法
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/returnBill", "post", JSON.stringify({ volist: listData }), function (data) {
        if (data.success) {
            grid.reload();
        }
      }, "application/json");
    }
    //归档
    if (name == "btnOk") {
      let listData = getDeleteAll(grid);
      if (listData == "" || listData == null) {
        return;
      }
      for (var y = 0; y < listData.length; y++) {
        listData[y] = grid.getRowData(listData[y]);
      }
      //调用ajax方法
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/file", "post", JSON.stringify({ volist: listData }), function (data) {
        if (data.success) {
            grid.reload();
        }
      }, "application/json");
    }
    //重置
    if (name == "car_Reset") {
      grid.getToolBarForm().getDForm().setText("buyername1", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()), edate: FormatDate(new Date()) });
    }
  });
  //查看图片
  window.receiptBack_details = function (rid) {
    grid.selectRowById(rid);
    let src = "./images/222.png";
    PlgDialog.open({
      title: "查看图片",
      shadeClose: true, //点击遮罩关闭
      content: "<img src='" + src + "' style='width:100%;height:100%;' ></img>",
      area: ["550px", "400px"]
    });
  };
};