/*
* zzx
* 2020.06.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 userinfo = {
  p_id: '',
  p_username: '',
  p_workernumber: '',
  p_nickname: '',
  p_phone: null,
  p_email: null,
  p_companyid: '',
  p_enterpriseid: '',
  p_organizationid: '',
  p_companyname: '',
  p_orgname: '',
  p_enterprisename: '',
  p_usertype: '',
  p_lastlogindate: '',
  p_lastmodipassdate: null,
  p_validdate: '',
  p_import_batchno: null,
  p_data_source: null,
  forceModifyPasswdFlag: false,
  vehicle: null,
}
  var branchCode = userinfo.p_companyid;
  var branchName = userinfo.p_companyname;

  // 下拉框数据
  var startJobTimeEnums = [
    { value: "", text: "" },
    { value: 0, text: "00" },
    { value: 1, text: "01" },
    { value: 2, text: "02" },
    { value: 3, text: "03" },
    { value: 4, text: "04" },
    { value: 5, text: "05" },
    { value: 6, text: "06" },
    { value: 7, text: "07" },
    { value: 8, text: "08" },
    { value: 9, text: "09" },
    { value: 10, text: "10" },
    { value: 11, text: "11" },
    { value: 12, text: "12" },
    { value: 13, text: "13" },
    { value: 14, text: "14" },
    { value: 15, text: "15" },
    { value: 16, text: "16" },
    { value: 17, text: "17" },
    { value: 18, text: "18" },
    { value: 19, text: "19" },
    { value: 20, text: "20" },
    { value: 21, text: "21" },
    { value: 22, text: "22" },
    { value: 23, text: "23" }
  ];
  var dataList = [
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency01",
      parameterName: "00-02H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency02",
      parameterName: "02-04H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency03",
      parameterName: "04-06H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency04",
      parameterName: "06-08H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency05",
      parameterName: "08-10H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency06",
      parameterName: "10-12H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency07",
      parameterName: "12-14H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency08",
      parameterName: "14-16H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency09",
      parameterName: "16-18H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency10",
      parameterName: "18-20H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency11",
      parameterName: "20-22H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "loadingEfficiency12",
      parameterName: "22-24H",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "startJobTime",
      parameterName: "开始作业时间",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    },
    {
      branchCode,
      branchName,
      parameterCode: "endJobTime",
      parameterName: "结束作业时间",
      fatherParameterCode: "loadingEfficiency",
      fatherParameterName: "装卸参数",
      parameterValue: ""
    }
  ];
  var dataObj = {};

  var loadingAndUnloading_form_opts = {
    renderer: "loadingAndUnloading_form",
    items: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "branchName", label: "当前仓库:", readonly: true, required: true },
          { type: "hidden", className: "layui-col-md3", name: "branchCode", label: "当前仓库ID:", readonly: true, required: true },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "startJobTime", label: "作业时间始:", options: startJobTimeEnums, required: true },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "endJobTime", label: "作业时间止:", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency01", label: "0-2H:", required: true },
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency02", label: "2-4H:", required: true },
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency03", label: "4-6H:", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency04", label: "6-8H:", required: true },
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency05", label: "8-10H:", required: true },
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency06", label: "10-12H:", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency07", label: "12-14H:", required: true },
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency08", label: "14-16H:", required: true },
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency09", label: "16-18H:", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency10", label: "18-20H:", required: true },
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency11", label: "20-22H:", required: true },
          { type: "input", className: "layui-col-md3", name: "loadingEfficiency12", label: "22-24H:", required: true }
        ]
      }
    ]
  };
  var loadingAndUnloading_form = new PlgForm(loadingAndUnloading_form_opts);
  loadingAndUnloading_form.setFormData({ branchCode, branchName });

  // 作业时间联动
  loadingAndUnloading_form.attachComboEvent("startJobTime", "onChange", function (id) {
    loadingAndUnloading_form.getCombo("endJobTime").clearAll();
    loadingAndUnloading_form.getCombo("endJobTime").setComboText("");
    if (id) {
      let list = [];
      startJobTimeEnums.forEach(item => {
        if (item.value - id > 0) {
          list.push([item.value, item.text]);
        }
      });
      loadingAndUnloading_form.getCombo("endJobTime").addOption(list);
    }
  });

  // 按钮事件
  $(".loadingAndUnloading_btn").click(function () {
    let name = $(this).attr("name");
    if (name == "loadingAndUnloading_btn_save") {
      let vdata = loadingAndUnloading_form.validate();
      if (!vdata) return false;
      let formData = loadingAndUnloading_form.getFormData();
      for (const key in formData) {
        if (key.includes("loadingEfficiency") || key === "startJobTime" || key === "endJobTime") {
          dataObj[key].parameterValue = formData[key];
        }
      }
      let list = [];
      for (const key in dataObj) {
        const element = dataObj[key];
        list.push(element);
      }
      save(list);
    }
    else if (name == "loadingAndUnloading_btn_reset") {
      loadingAndUnloading_form.clear();
      loadingAndUnloading_form.setFormData({ branchCode, branchName });
    }
  });

  // 查询
  function query() {
    let request = {
      branchCode,
      fatherParameterCode: "loadingEfficiency"
    };
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/baseParameter/query/v1.0", type: "post", contentType: "application/json", data: JSON.stringify(request),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          let list = res.data.length > 0 ? res.data : dataList;
          let obj = {};
          list.forEach(item => {
            dataObj[item.parameterCode] = item;
            obj[item.parameterCode] = item.parameterValue;
          });
          loadingAndUnloading_form.setFormData(obj);
          return true;
        }
                return false;

      }, error: function () { PlgDialog.close(pload); return false; }
    });
  }
  query();
  // 保存
  function save(list) {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/baseParameter/save/v1.0", type: "post", contentType: "application/json", data: JSON.stringify(list),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          PlgDialog.msg(res.message);
          query();
          return true;
        }
                return false;

      }, error: function () { PlgDialog.close(pload); return false; }
    });
  }
};