<template>
  <div class="app-container">
    <vip-level-table
      ref="table"
      :hide-columns="['mchName']"
      :list-api="mchListVipLevel"
      @selection-change="handleSelectionChange"
    >
      <template #table-operator>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="!isEmpty(ids)"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
      </template>
      <template v-slot:row-operator="scope">
        <el-button
          size="mini"
          type="text"
          icon="el-icon-edit"
          @click="handleUpdate(scope.row)"
        >修改</el-button>
        <el-button
          size="mini"
          type="text"
          icon="el-icon-delete"
          @click="handleDelete(scope.row)"
        >删除</el-button>
      </template>
    </vip-level-table>

    <!-- 添加或修改会员等级对话框 -->
    <vip-level-edit-dialog
      :show.sync="open"
      :id="row.id"
      @success="getList"
      :get-api="mchGetVipLevel"
      :add-api="mchAddVipLevel"
      :update-api="mchUpdateVipLevel"
      :hide-columns="['mchId']"
    />
  </div>
</template>

<script>
import { listVipLevel, getVipLevel, delVipLevel, addVipLevel, updateVipLevel } from "@/api/ss/vipLevel";
import { $showColumns } from '@/utils/mixins';
import UserLink from '@/components/Business/SmUser/UserLink.vue'
import VipLevelEditDialog from '@/views/ss/vipLevel/components/VipLevelEditDialog.vue'
import VipLevelTable from '@/views/ss/vipLevel/components/VipLevelTable.vue'
import { isEmpty } from '@/utils'
import {
  mchAddVipLevel,
  mchDeleteVipLevel,
  mchGetVipLevel,
  mchListVipLevel,
  mchUpdateVipLevel
} from '@/api/mch/vipLevel'


export default {
  name: "MchVipLevel",
  components: { VipLevelTable, VipLevelEditDialog, UserLink },
  mixins: [$showColumns],
  data() {
    return {
      row: {},
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
    };
  },
  methods: {
    mchListVipLevel,
    mchUpdateVipLevel,
    mchAddVipLevel,
    mchGetVipLevel,
    isEmpty,
    getList() {
      this.$refs.table.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.row = {};
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.row = row;
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除会员等级编号为"' + ids + '"的数据项？').then(function() {
        return mchDeleteVipLevel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ss/vipLevel/export', {
        ...this.queryParams
      }, `vipLevel_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
