/**
   * guoj
   * 2020.07.28
   * 对账单查询
   */
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 grid;
  let grid2;
  var dataSelete = {};
  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_SHIP_BILLTYPE" }], function (data) {
    dataSelete = data.data;
  });
  //配送中心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: "accountbillId", name: "id", type: "ro", width: "80", hidden: true },
      { id: "payableName", name: "配送中心", type: "ro"},
      { id: "accountbillNo", name: "对账单号", type: "ro" },
      { id: "receivableName", name: "承运商", type: "ro" },
      {
        id: "billsubject", name: "开票主体", type: "ro"
      },
      { id: "billnum", name: "总单数", type: "ro" },
      { id: "packageqty", name: "总件数", type: "ro" },
      { id: "whight", name: "总重量", type: "ro" },
      {
        id: "volume", name: "总体积", type: "ro"
      },
      { id: "feeamt", name: "总金额", type: "ro" },
      { id: "memo", name: "备注", type: "ro" }
    ],
    multiselect: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "startTime", className: "layui-col-md3", label: "开始时间:" },
          { type: "calendar", name: "endTime", 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: "carrierno", label: "承运商:", className: "layui-col-md3" },
          { type: "input", name: "billsubject", label: "开票主体:", className: "layui-col-md3" },
          {
            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: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md6 right",
            list: [
              { type: "button", name: "car_export", className: "btn-normal", value: "导 出" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeAccountBill/bmsFeeAccountBillQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ startTime: ship_FormatDate(new Date()) + " 00:00:00", endTime: FormatDate(new Date()) + " 23:59:59", status: "5" }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("accountStatement_zhu");
  grid.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms", startTime: ship_FormatDate(new Date()), endTime: FormatDate(new Date()) });
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = grid.getToolBarForm().getFormData();
      if (jsonData.startTime == null) {
        jsonData.startTime = ship_FormatDate(new Date()) + " 00:00:00";
      }
      else {
        jsonData.startTime = Prolog.formatDate(jsonData.startTime) + " 00:00:00";
      }
      if (jsonData.endTime == null) {
        jsonData.endTime = FormatDate(new Date()) + " 23:59:59";
      }
      else {
        jsonData.endTime = Prolog.formatDate(jsonData.endTime) + " 23:59:59";
      }
      jsonData.status = "5";
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
      grid2.getGrid().clearAll();
    }
    //重置
    if (name == "car_Reset") {
      grid.getToolBarForm().getDForm().setText("buyername1", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ startTime: ship_FormatDate(new Date()), endTime: FormatDate(new Date()) });
    }
    //导出
    if(name == "car_export"){
      unfilledWorktime_export();
    }
  });

  //主界面导出事件
  function unfilledWorktime_export() {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    if (grid.getRowsNum() <= 0) {
        PlgDialog.close(pload);
        PlgDialog.msg("没有要导出的数据");
        return;
    }
    let url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeAccountBill/bmsFeeAccountExport";
    let name = "对账单查询" + Prolog.formatDate(new Date(), "%YY%MM%DD") + ".xls";
    var jsonData = grid.getToolBarForm().getFormData();
    if (jsonData.startTime == null) {
      jsonData.startTime = ship_FormatDate(new Date()) + " 00:00:00";
    }
    else {
      jsonData.startTime = Prolog.formatDate(jsonData.startTime) + " 00:00:00";
    }
    if (jsonData.endTime == null) {
      jsonData.endTime = FormatDate(new Date()) + " 23:59:59";
    }
    else {
      jsonData.endTime = Prolog.formatDate(jsonData.endTime) + " 23:59:59";
    }
    jsonData.status = "5";
    Prolog.exportFile(url, name, "application/json", "post", jsonData);
    PlgDialog.close(pload);

}
  //分页参数
  var opts2 = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "shipbillno", name: "id", type: "ro", width: "80", hidden: true },
      { id: "branchname", name: "配送中心", width: "80",type: "ro" },
      { id: "shipbillno", name: "运单号", width: "80",type: "ro" },
      { id: "billtype", name: "运单类型", width: "80",type: "ro" ,render: (v)=>{
        return getSelectValue(dataSelete.TMS_SHIP_BILLTYPE,v);
      }},
      { id: "billsubject", name: "开票主体", width: "80",type: "ro"},
      { id: "linename", name: "线路名称", width: "80",type: "ro" },
      { id: "billdate", name: "单据日期", width: "130",type: "ro" },
      { id: "refbillno", name: "业务单号",width: "80", type: "ro" },
      { id: "refbilltype", name: "业务类型",width: "80", type: "ro" },
      { id: "carriername", name: "承运商",width: "80", type: "ro" },
      { id: "arriverName", name: "客户名称",width: "150", type: "ro" },
      { id: "arriveAddress", name: "客户地址",width: "150", type: "ro" },
      { id: "rsumcases", name: "件数",width: "80", type: "ro" },
      { id: "rsumgoodsweight", name: "重量",width: "80", type: "ro" },
      { id: "rsumgoodsvol", name: "体积",width: "80", type: "ro" },
      { id: "signqty", name: "签收件数",width: "80", type: "ro" },
      { id: "receiptqty", name: "确认件数",width: "80", type: "ro" },
      { id: "feeamt", name: "金额",width: "80", type: "ro" },
      { id: "memo", name: "备注",width: "80", type: "ro" }
    ],
    multiselect: false,
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeAccountBill/bmsFeeAccountItemQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: "{}",//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid2 = new PlgGrid(opts2);
  grid2.renderTo("accountStatement_xi");
  //单击事件
  grid.on("onRowSelect", function (id) {
    let rdata = grid.getRowData(id);
    grid2.setParams(JSON.stringify({ "accountbillId": id, accountbillNo: rdata.accountbillNo }));
    grid2.reload();
  });

};