<template>
  <el-container class="recyclebin-view">
    <el-main v-loading="loading">
      <el-table
        :data="table.data"
        @selection-change="changeSelection"
        @sort-change="fieldFilter"
        class="sa-table"
        stripe
      >
        <template #empty>
          <sa-empty />
        </template>
        <el-table-column type="selection" width="48" align="center"></el-table-column>
        <el-table-column sortable="custom" prop="id" label="ID" min-width="100"></el-table-column>
        <el-table-column label="名称" min-width="100">
          <template #default="scope">
            <div class="sa-table-line-1">{{ scope.row.name || '-' }}</div>
          </template>
        </el-table-column>
        <el-table-column
          sortable="custom"
          prop="delete_time"
          label="删除时间"
          min-width="160"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" min-width="120">
          <template #default="scope">
            <el-button
              v-auth="'shop.admin.payconfig.restore'"
              class="is-link"
              type="primary"
              @click="restoreRow(scope.row.id)"
              >还原</el-button
            >
            <el-popconfirm
              width="fit-content"
              confirm-button-text="确认"
              cancel-button-text="取消"
              title="确认销毁这条记录?"
              @confirm="destroyRow(scope.row.id)"
            >
              <template #reference>
                <el-button v-auth="'shop.admin.payconfig.destroy'" class="is-link" type="danger">
                  销毁
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <sa-view-bar>
      <template #left>
        <sa-batch-handle
          :batchHandleTools="batchHandleTools"
          :selectedLeng="table.selected.length"
          @batchHandle="batchHandle"
        >
        </sa-batch-handle>
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from './payConfig.service';
  import { ElMessageBox } from 'element-plus';

  const loading = ref(true);

  // 表格状态
  const table = reactive({
    data: [],
    order: '',
    sort: '',
    selected: [],
  });

  // 获取数据
  async function getData() {
    loading.value = true;
    const { data } = await api.recyclebin({
      order: table.order,
      sort: table.sort,
    });
    table.data = data;
    loading.value = false;
  }

  // table 字段排序
  function fieldFilter({ prop, order }) {
    table.order = order == 'ascending' ? 'asc' : 'desc';
    table.sort = prop;
    getData();
  }

  // table 批量选择
  function changeSelection(row) {
    table.selected = row;
  }
  // 批量操作
  const batchHandleTools = [
    {
      type: 'restore',
      label: '还原',
      auth: 'shop.admin.payconfig.restore',
      buttonType: 'primary',
    },
    {
      type: 'destroy',
      label: '销毁',
      auth: 'shop.admin.payconfig.destroy',
      buttonType: 'danger',
    },
    {
      type: 'all',
      label: '清空回收站',
      auth: 'shop.admin.payconfig.destroy',
      buttonType: 'danger',
      operType: 'all',
    },
  ];
  async function batchHandle(type, id) {
    let ids = [];
    table.selected.forEach((row) => {
      ids.push(row.id);
    });
    switch (type) {
      case 'all':
        ElMessageBox.confirm('此操作将清空回收站, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          destroyRow('all');
        });
        break;
      case 'destroy':
        ElMessageBox.confirm('此操作将销毁, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          destroyRow(ids.join(','));
        });
        break;
      case 'restore':
        ElMessageBox.confirm('此操作将还原, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          restoreRow(ids.join(','));
        });
        break;
    }
  }
  // 销毁
  async function destroyRow(id) {
    await api.destroy(id);
    getData();
  }
  // 还原
  async function restoreRow(id) {
    await api.restore(id);
    getData();
  }

  onMounted(() => {
    getData();
  });
</script>
