/**
* guoj
* 2020.10.13
* 容器回收
*/
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 ddfrom;
  //录入员table
  var usersTable = {
    columns: [
      { id: "p_id", name: "id", type: "ro", width: "130", hidden: true },
      { id: "p_workernumber", name: "工号", type: "ro" },
      { id: "p_nickname", name: "人员名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 80 },
      {
        type: "block",
        id: "bk",
        blockOffset: 0,
        list: [
          { type: "input", name: "search", label: "用户名称:", width: 190 },
          { type: "newcolumn", offset: 30 },
          { type: "button", name: "querycarr", value: "查询" }, { type: "newcolumn" },
          { type: "button", name: "car_Reset", className: " normal", value: "重置" }, { type: "newcolumn" }
        ]
      }
    ],
    url: "/users/users/user/popup", //数据接口
    type: "post", //数据提交方式，默认为get
    pagebar: { type: "simple" },
    params: {}, //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //客户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,
    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 conterTable = {
    columns: [
      { id: "containerId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "containerCode", name: "编号", type: "ro" },
      { id: "containerName", name: "名称", type: "ro" }
    ],
    beforeLoadData: function (res) {
      res.data.list.forEach(item => {
        item.containerId = item.containerId + "";
      });
      return res;
    },
    multiselect: false,
    pagebar: { type: "simple" },
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseTransportContainer/queryTBaseTransportContainer", //数据接口
    type: "post", //数据提交方式，默认为get
    contentType: "application/json",
    params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //配送中心table
  var objectTable = {
    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/queryCompanyBranch", //数据接口
    type: "post", //数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ companytypes: ["S"] }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  let dataSelect;
  //获取下拉框
  getSelectGJ([{ "objectcode": "CONTAINER_RECORD_STATUS" }, { "objectcode": "CONTAINER_TYPE" }, { "objectcode": "GET_CUS_TYPE" }], function (data) {
    dataSelect = data.data;
  });
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "lineid", name: "id", type: "ro", width: "100", hidden: true },
      { id: "branchName", name: "配送中心", type: "ro", width: 100 },
      { id: "containerCode", name: "容器编号", type: "ro", width: 130 },
      { id: "containerName", name: "容器名称", type: "ro", width: 130 },
      {
        id: "containerType", name: "容器类型", type: "ro", width: 100, render: (v) => {
          return getSelectValue(dataSelect.CONTAINER_TYPE, v);
        }
      },
      { id: "bindSmartDeviceGps", name: "关联gps设备", type: "ro", width: 100 },
      { id: "bindSmartDeviceCold", name: "关联冷链设备", type: "ro", width: 100 },
      { id: "containerno", name: "包裹号", type: "ro", width: 100 },
      {
        id: "shiptoCustomerType", name: "客户类型", type: "ro", width: 100, render: (v) => {
          return getSelectValue(dataSelect.GET_CUS_TYPE, v);
        }
      },
      { id: "shiptoCustomerCode", name: "客户编号", type: "ro", width: 100 },
      { id: "shiptoCustomerName", name: "客户名称", type: "ro", width: 100 },
      {
        id: "status", name: "状态", type: "ro", width: 100, render: (v) => {
          return getSelectValue(dataSelect.CONTAINER_RECORD_STATUS, v);
        }
      },
      { id: "bindingtime", name: "绑定时间", type: "ro", width: 130 },
      { id: "bindingperson", name: "绑定人", type: "ro", width: 110 },
      { id: "outtime", name: "出库时间", type: "ro", width: 130 },
      { id: "outperson", name: "出库登记人", type: "ro", width: 110 },
      { id: "signtime", name: "签收时间", type: "ro", width: 130 },
      { id: "signperson", name: "签收人", type: "ro", width: 110 },
      { id: "memo", name: "备注", type: "ro", width: 110 }
    ],
    multiselect: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "sdate", label: "开始时间:", className: "layui-col-md3", value: startTime },
          { type: "calendar", name: "edate", label: "结束时间:", className: "layui-col-md3", value: endTime },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择配送中心",
            readonly: false,
            table: objectTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchId",
            label: "配送中心:",
            listener: function () {
            }
          },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择容器编号",
            readonly: false,
            table: conterTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "containerName",
            fieldValue: "containerId",
            name: "containerId",
            label: "容器编号:",
            listener: function () {
            }
          },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择客户",
            readonly: false,
            table: userTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "shiptoCustomerCode",
            label: "客户:",
            listener: function () {
            }
          },
          {
            type: "block", className: "layui-col-md3 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "querywork", value: "查询" },
              { type: "newcolumn" },
              { type: "button", name: "work_Reset", className: "normal", value: "重置" }
            ]
          },
          {
            type: "block", className: "layui-col-md6 right",
            list: [
              { type: "button", className: "btn-normal", name: "addBtn", offsetLeft: 10, value: "容器回库" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/containerBind/queryContainerRecycle",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ sdate: startTime, edate: endTime }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  var grid = new PlgGrid(opts);
  grid.renderTo("containerRecycling_guojzhuans2");
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querywork") {
      var fdata = grid.getToolBarForm().getFormData();
      fdata.branchId = fdata.branchId == "" || fdata.branchId == null ? "" : JSON.parse(fdata.branchId)[0];
      fdata.containerId = fdata.containerId == "" || fdata.containerId == null ? "" : JSON.parse(fdata.containerId)[0];
      fdata.shiptoCustomerCode = fdata.shiptoCustomerCode == "" || fdata.shiptoCustomerCode == null ? "" : JSON.parse(fdata.shiptoCustomerCode)[0];
      fdata.sdate = FormatDateTime(fdata.sdate);
      fdata.edate = Prolog.formatDate(fdata.edate) + " 23:59:59";
      if (fdata.sdate > fdata.edate) {
        layer.msg("开始时间不能大于结束时间");
        return false;
      }
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    //容器回收
    if (name == "addBtn") {
      var data = grid.getCheckedIds();
      if (data == null || data == "") {
        data = grid.getSelectedRowId();
      }
      if (data == null) {
        PlgDialog.msg("请选择行!");
        return;
      }
      PlgDialog.open({
        title: "容器回库弹出框",
        shadeClose: true, //点击遮罩关闭
        content: "<div style='padding:23px' id='containerRecyclingFromData'></div>",
        area: ["450px", "230px"],
        type: "2",
        btn: ["回库确认", "关闭"], //只是为了演示
        yes: function () {
          let fdata = ddfrom.getFormData();
          if (fdata.recycleperson == "" || fdata.recycleperson == null) {
            layer.msg("容器回收人不能为空");
            return;
          }
          if (fdata.recycletime == "" || fdata.recycletime == null) {
            layer.msg("回收时间不能为空");
            return;
          }
          fdata.recycleperson = JSON.parse(fdata.recycleperson)[0];
          fdata.lineid = data;
          fdata.recycletime = FormatDateTime(fdata.recycletime);
          utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/containerBind/containerRecycle", "post", JSON.stringify(fdata), () => {
            layer.closeAll();
            grid.reload();
            layer.msg("成功");
          }, "application/json");
        },
        btn2: function () {
          layer.closeAll();
        }
      });

      ddfrom = new PlgForm({
        renderer: "containerRecyclingFromData",
        items: [
          { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
          {
            width: 260,
            type: "selectTable",
            placeholder: "请选择容器回收人",
            readonly: false,
            table: usersTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "p_nickname",
            fieldValue: "p_id",
            name: "recycleperson",
            label: "容器回收人:",
            listener: function (Grid) {
              setMethod(Grid, usersTable, function () { });
            }
          },
          { type: "newcolumn" },
          { type: "calendar", name: "recycletime", label: "回收时间:", width: 260 }]
      });
    }
    //重置
    if (name == "work_Reset") {
      grid.getToolBarForm().getDForm().setText("branchId", "");
      grid.getToolBarForm().getDForm().setText("shiptoCustomerCode", "");
      grid.getToolBarForm().getDForm().setText("containerId", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ sdate: startTime, edate: endTime });
    }
  });
};
