/**
 * zzx
 * 2021.09.03
 * 加油站管理
    */
    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 master_grid, edit_form;

  // 跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#gasStationManage",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", // 设置容器宽度
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#gasStationManage_main", "#gasStationManage_edit"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $("#gasStationManage_edit_return").on("click", function () {
    changePanel("#gasStationManage_main");
  });

  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";

  // 配送中心table
  var branchTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "配送中心编号", type: "ro" },
      { id: "companyname", name: "配送中心名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 200 },
      {
        type: "block", id: "bk", blockOffset: 0, list: [
          { type: "input", name: "companycode", label: "配送中心编号:", width: 150 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }
    ],

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "S" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "gsId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "gsCode", name: "加油站编号", type: "ro", width: 120 },
      { id: "gsName", name: "加油站名称", type: "ro", width: 120 },
      { id: "zjm", name: "助记码", type: "ro", width: 120 },
      { id: "gsShortname", name: "简称", type: "ro", width: 120 },
      { id: "contactman", name: "联系人", type: "ro", width: 120 },
      { id: "contacttel1", name: "联系电话", type: "ro", width: 120 },
      { id: "address", name: "地址", type: "ro", width: 120 },
      { id: "createtime", name: "录入时间", type: "ro", width: 120 },
      { id: "createperson", name: "录入人", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 },
      // {
      //   id: "containerType", name: "容器类型", type: "ro", render: function (v) {
      //     return getSelectValue(dataSelect.CONTAINER_TYPE, v);
      //   }
      // },
      {
        id: "op", name: "操作", type: "button", align: "left", width: 150, fixed: "right", render: function () {
          return "作废^gasStationManage_nullify,修改^gasStationManage_edit,复制^gasStationManage_copy,详情^gasStationManage_view";
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 110, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", className: "layui-col-md4", name: "startDate", label: "录入时间查询开始:", value: startTime, readonly: true },
          { type: "calendar", className: "layui-col-md4", name: "endDate", label: "录入时间查询结束:", value: endTime, readonly: true },
          { type: "input", className: "layui-col-md4", name: "zjm", label: "助记码:" },
          { type: "input", className: "layui-col-md4", name: "gsCode", label: "加油站编号:" },
          { type: "input", className: "layui-col-md4", name: "gsName", label: "加油站名称:" },
          {
            type: "block", className: "layui-col-md8 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "master_search", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "master_reset", value: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md4 right",
            list: [
              { type: "button", className: "btn-normal", name: "master_add", value: "<i class='layui-icon layui-icon-add-1'></i>新 增" }
            ]
          }
        ]
      }
    ],
    fixedLast: true, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerGasStation/query", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("gasStationManage_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.startDate = request.startDate ? Prolog.formatDate(request.startDate) + " 00:00:00" : "";
        request.endDate = request.endDate ? Prolog.formatDate(request.endDate) + " 23:59:59" : "";
        if (request.startDate > request.endDate) {
            layer.msg("开始时间不能大于结束时间");
            return false;
        }
        dealNullQueryCondition(request);
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setFormData({ startDate: startTime, endDate: endTime });
        break;
      case "master_add":
        changePanel("#gasStationManage_edit");
        $("#gasStationManage_edit_form").html("");
        editFormCreated();
        $("#gasStationManage_edit_title").html("新增");
        break;
    }
  });
  // 作废
  window.gasStationManage_nullify = function (rid) {
    Cancel([{ "gsId": rid }]);
  };
  // 作废方法
  function Cancel(idList) {
    layer.open({
      title: "删除",
      content: "是否作废维修厂？",
      btn: ["确认", "取消"],
      yes: function () {
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerGasStation/cancel", type: "post", contentType: "application/json", data: JSON.stringify(idList),
          success: function (data) {
            PlgDialog.close(pload);
            if (data.success) {
              layer.closeAll();
              PlgDialog.msg(data.message);
              master_grid.reload();
              return true;
            }
            return false;

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

  // 修改
  window.gasStationManage_edit = function (rid, rdata) {
    changePanel("#gasStationManage_edit");
    $("#gasStationManage_edit_form").html("");
    editFormCreated(rdata);
    $("#gasStationManage_edit_title").html("修改");
  };

  // 复制
  window.gasStationManage_copy = function (rid, rdata) {
    let copyData = JSON.parse(JSON.stringify(rdata));
    copyData.gsId = "";
    // 13420 TMSv1.1.1【基础数据】【加油站管理】复制界面“加油站编号”未带出
    // copyData.gsCode = "";
    changePanel("#gasStationManage_edit");
    $("#gasStationManage_edit_form").html("");
    editFormCreated(copyData);
    $("#gasStationManage_edit_title").html("新增");
  };

  // 创建form
  function editFormCreated(rdata) {
    edit_form = new PlgForm({
      renderer: "gasStationManage_edit_form",
      items: [
        { type: "settings", labelAlign: "left", labelWidth: 90 },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md4", name: "gsCode", label: "加油站编号:", maxLength: 40, required: true },
            { type: "input", className: "layui-col-md4", name: "gsName", label: "加油站名称:", maxLength: 60, required: true },
            { type: "input", className: "layui-col-md4", name: "zjm", label: "助记码:", maxLength: 40 }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md4", name: "gsShortname", label: "简称:", maxLength: 40 },
            { type: "input", className: "layui-col-md4", name: "contactman", label: "联系人:", maxLength: 30 },
            { type: "input", className: "layui-col-md4", name: "contacttel1", label: "联系电话:", maxLength: 30 }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md4", name: "bankName", label: "开户银行:", maxLength: 60 },
            { type: "input", className: "layui-col-md4", name: "bankAccount", label: "银行账户:", maxLength: 60 },
            { type: "input", className: "layui-col-md4", name: "invoiceTitle", label: "发票抬头:", maxLength: 60 }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md8", name: "address", label: "地址:", maxLength: 100, required: true },
            {
              type: "selectTable",
              className: "layui-col-md4",
              placeholder: "请选择配送中心",
              readonly: false,
              table: branchTable,
              tableWidth: 600,
              tableHeight: 500,
              fieldText: "companyname",
              fieldValue: "companyid",
              name: "branchidList",
              label: "配送中心:<span class='dhxform_item_required'>*</span>",
              listener: {
                "init": function (Grid) {
                  // 注册按钮事件
                  Grid.attachToolBarEvent("onButtonClick", function (name) {
                    switch (name) {
                      case "querycarr":
                        var request = Grid.getToolBarForm().getFormData();
                        Grid.setParams(JSON.stringify(request));
                        Grid.reload();
                        break;
                      case "car_Reset":
                        Grid.getToolBarForm().clear();
                        break;
                    }
                  });
                },
                "afterSelect": function (isSelected, selectId, sgrid) {
                  let sdata = sgrid.getRowData(selectId);
                  edit_form.setFormData({
                    branchid: isSelected ? sdata.companyid : "",
                    branchcode: isSelected ? sdata.companycode : "",
                    branchname: isSelected ? sdata.companyname : ""
                  });
                }
              }
            },
            { type: "hidden", name: "branchid" },
            { type: "hidden", name: "branchcode" },
            { type: "hidden", name: "branchname" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md8", name: "memo", label: "备注:", maxLength: 100 }
          ]
        }
      ]
    });

    edit_form.enableItem("gsCode");
    if (rdata) {
      rdata.branchidList = rdata.branchid ? JSON.stringify([rdata.branchid]) : "[]";
      edit_form.setText("branchidList", rdata.branchname || "");
      edit_form.setFormData(rdata);
      if (rdata.gsId) {
        edit_form.disableItem("gsCode");
      }
    }
  }

  // 详情
  window.gasStationManage_view = function (rid, rdata) {
    changePanel("#gasStationManage_edit");
    $("#gasStationManage_edit_form").html("");
    $("#gasStationManage_edit_title").html("详情");
    new PlgPanel({
      renderer: "gasStationManage_edit_form",
      skin: 2,
      header: {
        isShow: false
      },
      defaultBody: {
        layoutCol: 4,
        valueBj: true,
        cols: [
          [
            { label: "加油站编号", value: rdata.gsCode },
            { label: "加油站名称", value: rdata.gsName },
            { label: "助记码", value: rdata.zjm }
          ],
          [
            { label: "简称", value: rdata.gsShortname },
            { label: "联系人", value: rdata.contactman },
            { label: "联系电话", value: rdata.contacttel1 }
          ],
          [
            { label: "开户银行", value: rdata.bankName },
            { label: "银行账户", value: rdata.bankAccount },
            { label: "发票抬头", value: rdata.invoiceTitle }
          ],
          [
            { label: "地址", value: rdata.address, layoutCol: 8 },
            { label: "配送中心", value: rdata.branchname }
          ],
          [
            { label: "备注", value: rdata.memo, layoutCol: 12 }
          ]
        ]
      }
    });
  };

  // 保存
  $("#gasStationManage_edit_save").on("click", function () {
    let feik = edit_form.validate();
    if (!feik) return;
    let formData = edit_form.getFormData();
    if (!formData.branchid) {
      layer.msg("请选择配送中心");
      return false;
    }
    delete formData.branchidList;

    let url = $("#gasStationManage_edit_title").html() === "新增" ? UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerGasStation/add" : UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/partnerGasStation/update";
    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("#gasStationManage_main");
          master_grid.reload();
          return true;
        }
                return false;
      }, error: function () { PlgDialog.close(pload); return false; }
    });
  });
};