/**
 * 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: "#NumberManagerNew",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%",
    height: "100%"
  });
  // 跳转方法
  function changePanel(name) {
    var slideList = ["#NumberManagerNew_main", "#NumberManagerNew_edit", "#NumberManagerNew_view"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $("#NumberManagerNew_edit_return").on("click", function () {
    changePanel("#NumberManagerNew_main");
  });
  $("#NumberManagerNew_view_return").on("click", function () {
    changePanel("#NumberManagerNew_main");
  });

  // 获取下拉框数据
  var dataSelect;
  getSelectGJ([
    { "objectcode": "CODEENGINE_CODETYPE" },
    { "objectcode": "CODEENGINE_CODEDATATYPE" },
    { "objectcode": "YES_OR_NO" }
  ], function (data) {
    dataSelect = data.data;
    dataSelect.CODEENGINE_CODETYPE.unshift({ "value": "", "text": "" });
    dataSelect.CODEENGINE_CODEDATATYPE.unshift({ "value": "", "text": "" });
  });

  var edit_from;

  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "codeid", name: "号码代号", type: "ro", width: "130", hidden: true },
      { id: "code", name: "识别代号", type: "ro", width: 120 },
      { id: "name", name: "名称", type: "ro", width: 120 },
      {
        id: "codetype", name: "号码类型分类", type: "ro", width: 120, render: function (v) {
          return Prolog.getNameByID(dataSelect.CODEENGINE_CODETYPE, v);
        }
      },
      {
        id: "codedatatype", name: "值类型", type: "ro", width: 120, render: function (v) {
          return Prolog.getNameByID(dataSelect.CODEENGINE_CODEDATATYPE, v);
        }
      },
      { id: "codeflag", name: "标识符", type: "ro", width: 120 },
      { id: "length", name: "长度", type: "ro", width: 120 },
      { id: "gap", name: "间隔", type: "ro", width: 120 },
      { id: "maxbillno", name: "最大单据号", type: "ro", width: 120 },
      { id: "billrule", name: "号码规则", type: "ro", width: 120 },
      { id: "issegsource", name: "是否段资源定义", type: "ro", width: 120 },
      {
        id: "issegsource", name: "是否段资源定义", type: "ro", width: 120, render: function (v) {
          return Prolog.getNameByID(dataSelect.YES_OR_NO, v);
        }
      },
      { id: "segcodesql", name: "段代码取法", type: "ro", width: 120 },
      { id: "groupid", name: "父识别号", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 },
      {
        id: "op", name: "操作", type: "button", align: "center", width: 150, render: function () {
          return "详情^NumberManagerNew_view,修改^NumberManagerNew_edit,删除^NumberManagerNew_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+"/codes/query",
    type: "post",
    params: "{}",
    page: true,
    contentType: "application/json",
    totalCount: "totalCount"
  };
  var master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("NumberManagerNew_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":
        $("#NumberManagerNew_edit_title").html("新增");
        changePanel("#NumberManagerNew_edit");
        editFormInit();
        break;
    }
  });
  window.NumberManagerNew_view = function (rid, rdata) {
    changePanel("#NumberManagerNew_view");
    new PlgPanel({
      renderer: "NumberManagerNew_view_form",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 4,
        valueBj: true,
        cols: [
          [
            { value: rdata.code, label: "识别代号" },
            { value: rdata.name, label: "名称" },
            { value: Prolog.getNameByID(dataSelect.CODEENGINE_CODETYPE, rdata.codetype), label: "号码类型分类" }
          ],
          [
            { value: Prolog.getNameByID(dataSelect.CODEENGINE_CODEDATATYPE, rdata.codedatatype), label: "值类型" },
            { value: rdata.codeflag, label: "标识符" },
            { value: rdata.length, label: "长度" }
          ],
          [
            { value: rdata.gap, label: "间隔" },
            { value: rdata.maxbillno, label: "最大单据号" },
            { value: rdata.billrule, label: "号码规则" }
          ],
          [
            { value: Prolog.getNameByID(dataSelect.YES_OR_NO, rdata.issegsource), label: "是否段资源定义" },
            { value: rdata.segcodesql, label: "段代码取法" },
            { value: rdata.groupid, label: "父识别号" }
          ],
          [
            { value: rdata.memo, label: "备注" }
          ]
        ]
      }
    });
  };
  window.NumberManagerNew_edit = function (rid, rdata) {
    $("#NumberManagerNew_edit_title").html("修改");
    changePanel("#NumberManagerNew_edit");
    editFormInit(rdata);
  };
  window.NumberManagerNew_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+"/codes/delete", type: "post", contentType: "application/json", data: JSON.stringify([{ codeid: 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) {
    $("#NumberManagerNew_edit_form").html("");
    var edit_form_opts = {
      renderer: "NumberManagerNew_edit_form",
      items: [
        { type: "settings", labelAlign: "right", labelWidth: 90 },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md4", name: "code", label: "识别代号:", required: true },
            { type: "input", className: "layui-col-md4", name: "name", label: "名称:", required: true },
            { type: "combo", className: "layui-col-md4", name: "codetype", label: "号码类型分类:", options: dataSelect.CODEENGINE_CODETYPE, readonly: true, required: true }
          ]
        },
        {
          type: "row",
          list: [
            { type: "combo", className: "layui-col-md4", name: "codedatatype", label: "值类型:", options: dataSelect.CODEENGINE_CODEDATATYPE, readonly: true, required: true },
            { type: "input", className: "layui-col-md4", name: "codeflag", label: "标识符:" },
            { type: "input", className: "layui-col-md4", name: "length", label: "长度:", placeholder: "只能输入正整数", required: true, validate: "NotEmpty,ValidInteger" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md4", name: "gap", label: "间隔:", placeholder: "只能输入正整数", required: true, validate: "NotEmpty,ValidInteger" },
            { type: "input", className: "layui-col-md4", name: "maxbillno", label: "最大单据号:" },
            { type: "input", className: "layui-col-md4", name: "billrule", label: "号码规则:" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "combo", className: "layui-col-md4", name: "issegsource", label: "是否段资源定义:", options: dataSelect.YES_OR_NO, readonly: true },
            { type: "input", className: "layui-col-md4", name: "segcodesql", label: "段代码取法:" },
            { type: "input", className: "layui-col-md4", name: "groupid", label: "父识别号:" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md8", name: "memo", label: "备注:" }
          ]
        }
      ]
    };
    edit_from = new PlgForm(edit_form_opts);
    params && edit_from.setFormData(params);
  }

  // 保存
  $("#NumberManagerNew_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.codeid ? UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/codes/update" : UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/codes/create";
    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("#NumberManagerNew_main");
          master_grid.reload();
          return true;
        }
                return false;
      }, error: function () { PlgDialog.close(pload); return false; }
    });
  });
};