/**
 * jiw
 * 2018.10.24
 * 系统参数配置
 */
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: "#RegistryConfigNew",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%",
    height: "100%"
  });
  // 跳转方法
  function changePanel(name) {
    var slideList = ["#RegistryConfigNew_main", "#RegistryConfigNew_edit", "#RegistryConfigNew_view"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $("#RegistryConfigNew_edit_return").on("click", function () {
    changePanel("#RegistryConfigNew_main");
  });
  $("#RegistryConfigNew_view_return").on("click", function () {
    changePanel("#RegistryConfigNew_main");
  });

  var edit_from;

  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "seqno", name: "序号", type: "ro", width: "130", hidden: true },
      { id: "branchCode", name: "物流中心/仓ID", type: "ro", width: 120 },
      { id: "branchName", name: "物流中心/仓名称", type: "ro", width: 120 },
      { id: "parameterCode", name: "参数编号", type: "ro", width: 120 },
      { id: "parameterName", name: "参数名称", type: "ro", width: 120 },
      { id: "fatherParameterCode", name: "分组编号", type: "ro", width: 120 },
      { id: "fatherParameterName", name: "分组名称", type: "ro", width: 120 },
      { id: "parameterValue", name: "参数值", type: "ro", width: 120 },
      { id: "startJobTime", name: "开始作业时间", type: "ro", width: 120 },
      { id: "endJobTime", name: "结束作业时间", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 },
      {
        id: "op", name: "操作", type: "button", align: "center", width: 150, render: function () {
          return "详情^RegistryConfigNew_view,修改^RegistryConfigNew_edit,删除^RegistryConfigNew_delete";
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "input", name: "search", label: "查询条件:", className: "layui-col-md3", placeholder: "请输入参数编号/参数名称" },
          {
            type: "block", className: "layui-col-md6 form_btn_left",
            list: [
              { type: "button", name: "master_search", className: "btn-normal", value: "查询" },
              { type: "newcolumn" },
              { type: "button", name: "master_reset", value: "重置" }
            ]
          },
          {
            type: "block", className: "layui-col-md3 right",
            list: [
              { type: "button", name: "master_add", className: "btn-normal", value: "<i class='layui-icon layui-icon-add-1'></i>新增" }
            ]
          }
        ]
      }
    ],
    fixedLast: true,
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/query",
    type: "post",
    params: "{}",
    page: true,
    contentType: "application/json",
    totalCount: "totalCount"
  };
  var master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("RegistryConfigNew_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        break;
      case "master_add":
        $("#RegistryConfigNew_edit_title").html("新增");
        changePanel("#RegistryConfigNew_edit");
        editFormInit();
        break;
    }
  });
  window.RegistryConfigNew_view = function (rid, rdata) {
    changePanel("#RegistryConfigNew_view");
    new PlgPanel({
      renderer: "RegistryConfigNew_view_form",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 4,
        valueBj: true,
        cols: [
          [
            { value: rdata.branchCode, label: "物流中心/仓ID" },
            { value: rdata.branchName, label: "物流中心/仓名称" },
            { value: rdata.parameterCode, label: "参数编号" }
          ],
          [
            { value: rdata.parameterName, label: "参数名称" },
            { value: rdata.fatherParameterCode, label: "分组编号" },
            { value: rdata.fatherParameterName, label: "分组名称" }
          ],
          [
            { value: rdata.parameterValue, label: "参数值" },
            { value: rdata.startJobTime, label: "开始作业时间" },
            { value: rdata.endJobTime, label: "结束作业时间" }
          ],
          [
            { value: rdata.memo, label: "备注" }
          ]
        ]
      }
    });
  };
  window.RegistryConfigNew_edit = function (rid, rdata) {
    $("#RegistryConfigNew_edit_title").html("修改");
    changePanel("#RegistryConfigNew_edit");
    editFormInit(rdata);
  };
  window.RegistryConfigNew_delete = function (rid) {
    layer.open({
      title: "删除",
      content: "是否删除?",
      btn: ["确认", "取消"],
      yes: function () {
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/delete", type: "post", contentType: "application/json", data: JSON.stringify([{ seqno: rid }]),
          success: function (res) {
            PlgDialog.close(pload);
            if (res.success) {
              layer.closeAll();
              PlgDialog.msg(res.message);
              master_grid.reload();
            }
            else {
              setTimeout(() => { PlgDialog.alert(_t(res.message == null ? "接口调用失败" : res.message)); }, 300);
            }
          }, error: function () {
            PlgDialog.close(pload);
            return false;
          }
        });
      }
    });
  };

  // 注册表单
  function editFormInit(params) {
    $("#RegistryConfigNew_edit_form").html("");
    let isDisabled = !!params;
    var edit_form_opts = {
      renderer: "RegistryConfigNew_edit_form",
      items: [
        { type: "settings", labelAlign: "right", labelWidth: 90 },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md4", name: "parameterCode", label: "参数编号:", required: true, disabled: isDisabled },
            { type: "input", className: "layui-col-md4", name: "parameterName", label: "参数名称:", required: true },
            { type: "input", className: "layui-col-md4", name: "fatherParameterCode", label: "分组编号:", disabled: isDisabled }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md4", name: "fatherParameterName", label: "分组名称:" },
            { type: "input", className: "layui-col-md4", name: "parameterValue", label: "参数值:", required: true },
            { type: "calendar", className: "layui-col-md4", name: "startJobTime", label: "开始作业时间:", readonly: true }
          ]
        },
        {
          type: "row",
          list: [
            { type: "calendar", className: "layui-col-md4", name: "endJobTime", label: "结束作业时间:", readonly: true },
            { type: "input", className: "layui-col-md8", name: "memo", label: "备注:" }
          ]
        }
      ]
    };
    edit_from = new PlgForm(edit_form_opts);
    params && edit_from.setFormData(params);
  }

  // 保存
  $("#RegistryConfigNew_edit_saveBtn").on("click", function () {
    let vdate = edit_from.validate();
    if (!vdate) return false;

    let formData = edit_from.getFormData();
    formData.startJobTime = formData.startJobTime ? Prolog.formatDate(formData.startJobTime) : "";
    formData.endJobTime = formData.endJobTime ? Prolog.formatDate(formData.endJobTime) : "";

    let url = formData.seqno ? UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/update" : UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/save";
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url, type: "post", contentType: "application/json", data: JSON.stringify(formData),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          PlgDialog.msg(res.message);
          changePanel("#RegistryConfigNew_main");
          master_grid.reload();
          return true;
        }
                return false;
      }, error: function () { PlgDialog.close(pload); return false; }
    });
  });
};