/**
 * zzx
 * 2020.03.31
 * BOM主数据
 */
 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 carousel = layui.carousel;
  carousel.render({
    elem: "#BOMmaindata",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  // 跳转方法
  function changePanel(name) {
    var slideList = ["#BOMmaindata_master", "#BOMmaindata_edit", "#BOMmaindata_view"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $(document).on("click", "#BOMmaindata_edit_return", function () {
    changePanel("#BOMmaindata_master");
  });
  $(document).on("click", "#BOMmaindata_view_return", function () {
    changePanel("#BOMmaindata_master");
  });

  // 编辑表id
  var edit_id = 0;
  // 默认时间
  var startTime = Prolog.formatDate(new Date());
  var endTime = "9999-12-31";
  var attributeEnums = [
    { value: "", text: "" },
    { value: "1", text: "自制" },
    { value: "2", text: "外协" }
  ];
  var roundingEnums = [
    { value: "0", text: "不取整" },
    { value: "1", text: "向上取整" }
  ];
  var putmodeEnums = [
    { value: "1", text: "推式" },
    { value: "2", text: "倒扣" },
    { value: "3", text: "不发料" }
  ];

  // 主表grid
  var BOMmaindata_master_opts = {
    skin: "card",
    columns: [
      { id: "bomid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "itemcode", name: "母件物料编码", type: "ro" },
      { id: "itemname", name: "母件物料名称", type: "ro" },
      { id: "bomversion", name: "BOM版本", type: "ro" },
      { id: "uomCode", name: "生产单位", type: "ro" },
      { id: "", name: "标准用量", type: "ro" },
      { id: "productivity", name: "成品率", type: "ro" },
      { id: "begindate", name: "生效日期", type: "ro" },
      { id: "enddate", name: "失效日期", type: "ro" },
      {
        id: "op", name: "操作", type: "button", align: "center", width: 250, render: function () {
          return "详情^BOMmaindata_view,修改^BOMmaindata_edit,删除^BOMmaindata_del,启用^BOMmaindata_switch";
        }
      }
    ],
    toolbar: [
      { type: "input", name: "billno", label: "母件物料:", width: 170 },
      { type: "newcolumn" },
      { type: "button", name: "master_search", value: "查询" },
      { type: "newcolumn" },
      { type: "button", name: "master_reset", value: "重置" },
      { type: "newcolumn" },
      { type: "button", name: "master_add", className: "plg-toolbar-right", value: "<i class='layui-icon layui-icon-add-1'></i>新增" }
    ],
    fixedLast: true,
    imagePath: "/prologui/dist/images/PlgGrid",
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/iteminfo/list", //数据接口
    type: "get", //数据提交方式，默认为get
    params: { itemid: "%", likeItemcode: "%", pageNum: 1, pageSize: 10 }, //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    // contentType: "application/json",
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  var BOMmaindata_master_grid = new PlgGrid(BOMmaindata_master_opts);
  BOMmaindata_master_grid.renderTo("BOMmaindata_master");
  BOMmaindata_master_grid.loadData();
  // 主表按钮点击事件
  BOMmaindata_master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = BOMmaindata_master_grid.getToolBarForm().getFormData();
        BOMmaindata_master_grid.setParams(JSON.stringify(request));
        BOMmaindata_master_grid.reload();
        break;
      case "master_reset":
        BOMmaindata_master_grid.getToolBarForm().clear();
        break;
      case "master_add":
        $("#BOMmaindata_edit_title").html("新增");
        changePanel("#BOMmaindata_edit");
        break;
    }
  });

  // 编辑页grid
  var BOMmaindata_edit_opts = {
    skin: "card",
    columns: [
      { id: "bomdetailid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "seqno", name: "序号", type: "ro", width: 100 },
      { id: "itemcode", name: "物料编码", type: "ro", width: 100 },
      { id: "itemname", name: "物料名称", type: "ro", width: 100 },
      { id: "capacity", name: "规格", type: "ro", width: 100 },
      { id: "uomCode", name: "计量单位", type: "ed", width: 100 },
      { id: "quantity", name: "固定用量", type: "ed", width: 100 },
      { id: "changequantity", name: "变动用量", type: "ed", width: 100 },
      { id: "changequantityd", name: "母件底数", type: "ed", width: 100 },
      { id: "rounding", name: "取整", type: "coro", width: 100 },
      { id: "wasterate", name: "固定损耗", type: "ed", width: 100 },
      { id: "changerate", name: "变动损耗", type: "ed", width: 100 },
      { id: "putmode", name: "发料方式", type: "coro", width: 100 },
      { id: "leadday", name: "前置期", type: "ed", width: 100 },
      { id: "begindate", name: "生效日期", type: "dhxCalendar", width: 100 },
      { id: "enddate", name: "失效日期", type: "dhxCalendar", width: 100 }
    ],
    toolbar: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
      {
        type: "block", id: "bk", blockOffset: 0,
        list: [
          { type: "input", name: "itemcode", label: "母件物料编码:", width: 170, required: true },
          { type: "newcolumn" },
          { type: "input", name: "itemname", label: "母件物料名称:", width: 170, readonly: true, required: true },
          { type: "newcolumn" },
          { type: "input", name: "bomversion", label: "BOM版本:", width: 170, required: true },
          { type: "newcolumn" },
          { type: "calendar", name: "begindate", label: "生效日期:", width: 170, value: startTime, required: true },
          { type: "newcolumn" },
          { type: "calendar", name: "enddate", label: "失效日期:", width: 170, value: endTime, required: true }
        ]
      },
      {
        type: "block", id: "bk", blockOffset: 0,
        list: [
          { type: "combo", readonly: true, name: "attribute", label: "BOM类型:", width: 170, options: attributeEnums, required: true },
          { type: "newcolumn" },
          { type: "input", name: "uomCode", label: "生产单位:", width: 170, required: true },
          { type: "newcolumn" },
          { type: "input", name: "quantity", label: "标准产量:", width: 170, required: true },
          { type: "newcolumn" },
          { type: "input", name: "productivity", label: "成品率:", width: 170, required: true },
          { type: "newcolumn" },
          { type: "input", name: "prdline", label: "产线:", width: 170, required: true }
        ]
      },
      {
        type: "block", id: "bk", blockOffset: 0,
        list: [
          { type: "button", name: "edit_add", value: "新增" },
          { type: "newcolumn" },
          { type: "button", name: "edit_reset", value: "重置" }
        ]
      }
    ],
    pagebar: {
      buttons: [
        { text: "<i class='layui-icon layui-icon-delete'></i>删除", className: "plg-btn-normal", name: "edit_del" }
      ]
    },
    multiselect: true,
    imagePath: "/prologui/dist/images/PlgGrid",
    url: "/omsbusiness/instockOrder/searchDetailList/v1.0", //数据接口
    type: "post", //数据提交方式，默认为get
    params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  var BOMmaindata_edit_grid = new PlgGrid(BOMmaindata_edit_opts);
  BOMmaindata_edit_grid.renderTo("BOMmaindata_edit_grid");
  // 可编辑列改变颜色
  BOMmaindata_edit_grid.getGrid().setColLabel(4, "<span style='color:#096dd9;'>物料编码</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(7, "<span style='color:#096dd9;'>计量单位</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(8, "<span style='color:#096dd9;'>固定用量</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(9, "<span style='color:#096dd9;'>变动用量</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(10, "<span style='color:#096dd9;'>母件底数</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(11, "<span style='color:#096dd9;'>取整</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(12, "<span style='color:#096dd9;'>固定损耗</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(13, "<span style='color:#096dd9;'>变动损耗</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(14, "<span style='color:#096dd9;'>发料方式</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(15, "<span style='color:#096dd9;'>前置期</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(16, "<span style='color:#096dd9;'>生效日期</span>");
  BOMmaindata_edit_grid.getGrid().setColLabel(17, "<span style='color:#096dd9;'>失效日期</span>");
  BOMmaindata_edit_grid.getGrid().setDateFormat("%Y-%m-%d");
  BOMmaindata_edit_grid.getGrid().enableEditEvents(true, false, true);
  // 下拉赋值
  let combo_1 = BOMmaindata_edit_grid.getGrid().getCombo(11);
  roundingEnums.forEach(v => {
    combo_1.put(v.value, v.text);
  });
  let combo_2 = BOMmaindata_edit_grid.getGrid().getCombo(14);
  putmodeEnums.forEach(v => {
    combo_2.put(v.value, v.text);
  });
  // 变价表按钮点击事件
  BOMmaindata_edit_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "edit_add":
        var { begindate, enddate } = BOMmaindata_edit_grid.getToolBarForm().getFormData();
        var data = {
          seqno: edit_id,
          itemcode: "",
          itemname: "",
          capacity: "",
          uomCode: "",
          quantity: "0",
          changequantity: "0",
          changequantityd: "1",
          rounding: "0",
          wasterate: "0",
          changerate: "0",
          putmode: "1",
          leadday: "0",
          begindate: FormatDate(begindate),
          enddate: FormatDate(enddate)
        };
        BOMmaindata_edit_grid.addRow(++edit_id, data);
        break;
      case "edit_reset":
        BOMmaindata_edit_grid.getToolBarForm().clear();
        BOMmaindata_edit_grid.clearAll();
        break;
    }
  });
  // 编辑离开校验
  BOMmaindata_edit_grid.attachEvent("onEditCell", function (stage, rId, cInd, nValue, oValue) {
    let rd = BOMmaindata_edit_grid.getRowData(rId);
    // let positiveIntegerOrZero = /^\d+$/; // 非负整数
    let positveOrZero = /^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/; // 非负数
    let positiveInteger = /^[1-9]\d*$/; // 正整数
    let integer = /^-?\d+$/; // 整数
    if (cInd === 8 && stage === 2) { // 固定用量
      if (!positveOrZero.test(nValue)) {
        layer.msg("请输入正数");
        nValue = oValue;
        return;
      }
      if (nValue == 0 && rd.changequantity == 0) {
        layer.msg("固定用量、变动用量不能同时为0");
        nValue = oValue;
        return;
      }
      BOMmaindata_edit_grid.setCellValue(rId, 8, Number(nValue));
    }
    if (cInd === 9 && stage === 2) { // 变动用量
      if (!positveOrZero.test(nValue)) {
        layer.msg("请输入正数");
        nValue = oValue;
        return;
      }
      if (nValue == 0 && rd.quantity == 0) {
        layer.msg("固定用量、变动用量不能同时为0");
        nValue = oValue;
        return;
      }
      BOMmaindata_edit_grid.setCellValue(rId, 9, Number(nValue));
    }
    if (cInd === 10 && stage === 2) { // 母件底数
      if (!positiveInteger.test(Number(nValue))) {
        layer.msg("请输入正整数");
        nValue = oValue;
        return;
      }
      BOMmaindata_edit_grid.setCellValue(rId, 10, Number(nValue));
    }
    if (cInd === 12 && stage === 2) { // 固定损耗
      if (Number(nValue) < 0) {
        layer.msg("请输入正数");
        nValue = oValue;
        return;
      }
      BOMmaindata_edit_grid.setCellValue(rId, 12, Number(nValue));
    }
    if (cInd === 13 && stage === 2) { // 变动损耗
      if (Number(nValue) < 0) {
        layer.msg("请输入正数");
        nValue = oValue;
        return;
      }
      BOMmaindata_edit_grid.setCellValue(rId, 13, Number(nValue));
    }
    if (cInd === 15 && stage === 2) { // 前置期
      if (!integer.test(Number(nValue))) {
        layer.msg("请输入整数");
        nValue = oValue;
        return;
      }
      BOMmaindata_edit_grid.setCellValue(rId, 15, Number(nValue));
    }
    return true;
  });

  // 详情页
  var BOMmaindata_view_opts = {
    skin: "card",
    columns: [
      { id: "bomdetailid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "seqno", name: "序号", type: "ro", width: 100 },
      { id: "itemcode", name: "物料编码", type: "ro", width: 100 },
      { id: "itemname", name: "物料名称", type: "ro", width: 100 },
      { id: "capacity", name: "规格", type: "ro", width: 100 },
      { id: "uomCode", name: "计量单位", type: "ro", width: 100 },
      { id: "quantity", name: "固定用量", type: "ro", width: 100 },
      { id: "changequantity", name: "变动用量", type: "ro", width: 100 },
      { id: "changequantityd", name: "母件底数", type: "ro", width: 100 },
      { id: "rounding", name: "取整", type: "ro", width: 100 },
      { id: "wasterate", name: "固定损耗", type: "ro", width: 100 },
      { id: "changerate", name: "变动损耗", type: "ro", width: 100 },
      { id: "putmode", name: "发料方式", type: "ro", width: 100 },
      { id: "leadday", name: "前置期", type: "ro", width: 100 },
      { id: "begindate", name: "生效日期", type: "ro", width: 100 },
      { id: "enddate", name: "失效日期", type: "ro", width: 100 }
    ],
    imagePath: "/prologui/dist/images/PlgGrid",
    url: "/omsbusiness/instockOrder/searchDetailList/v1.0", //数据接口
    type: "post", //数据提交方式，默认为get
    params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  var BOMmaindata_view_grid = new PlgGrid(BOMmaindata_view_opts);
  BOMmaindata_view_grid.renderTo("BOMmaindata_view_grid");

  // 详情
  window.BOMmaindata_view = function (rid, rdata) {
    new PlgPanel({
      renderer: "BOMmaindata_view_form",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [
            { value: "", label: "母件物料编码" },
            { value: "", label: "母件物料名称" },
            { value: "", label: "BOM版本" },
            { value: "", label: "生效日期" },
            { value: "", label: "失效日期" },
            { value: "", label: "BOM类型" },
            { value: "", label: "生产单位" },
            { value: "", label: "标准产量" },
            { value: "", label: "成品率" },
            { value: "", label: "产线" }
          ]
        ]
      }
    });
    changePanel("#BOMmaindata_view");
  };
  // 修改
  window.BOMmaindata_edit = function (rid, rdata) {
    $("#BOMmaindata_edit_title").html("修改");
    changePanel("#BOMmaindata_edit");
  };
  // 删除
  window.BOMmaindata_del = function (rid, rdata) { };
  // 启用/停用
  window.BOMmaindata_switch = function (rid, rdata) { };
};