<template>
  <el-container class="pay-config-page panel-block">
    <el-header>
      <div class="sa-title sa-flex sa-row-between">
        <div class="label sa-flex">支付配置</div>
        <div>
          <el-button class="sa-button-refresh" icon="RefreshRight" @click="getData()"></el-button>
          <el-button v-auth="'shop.admin.payconfig.add'" icon="Plus" type="primary" @click="addRow"
            >新建</el-button
          >
          <el-button
            v-auth="'shop.admin.payconfig.recyclebin'"
            type="danger"
            icon="Delete"
            plain
            @click="openRecyclebin"
            >回收站</el-button
          >
        </div>
      </div>
    </el-header>
    <el-main class="sa-p-0">
      <div class="sa-table-wrap panel-block panel-block--bottom" v-loading="loading">
        <el-table height="100%" class="sa-table" :data="table.data" row-key="id" stripe>
          <template #empty>
            <sa-empty />
          </template>
          <el-table-column label="名称" min-width="280">
            <template #default="scope">
              <span class="sa-table-line-1">
                {{ scope.row.name || '-' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="支付类型" min-width="120">
            <template #default="scope">
              <div class="sa-table-line-1">
                {{ scope.row.type_text || '-' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="状态" min-width="130" align="center">
            <template #default="scope">
              <el-dropdown trigger="click" @command="handleCommand">
                <el-button
                  class="status-btn"
                  :class="scope.row.status == 'normal' ? 'success-btn' : ''"
                >
                  {{ scope.row.status_text }}
                  <el-icon class="el-icon--right"><ArrowDown /></el-icon>
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item
                      :command="{
                        id: scope.row.id,
                        type: 'normal',
                      }"
                    >
                      <span class="status-normal">正常</span>
                    </el-dropdown-item>
                    <el-dropdown-item
                      :command="{
                        id: scope.row.id,
                        type: 'disabled',
                      }"
                      ><span class="status-hidden">禁用</span></el-dropdown-item
                    >
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" min-width="174">
            <template #default="scope">
              {{ scope.row.create_time || '-' }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" min-width="120">
            <template #default="scope">
              <el-button class="is-link" type="primary" @click="editRow(scope.row)">编辑</el-button>
              <el-popconfirm
                width="fit-content"
                confirm-button-text="确认"
                cancel-button-text="取消"
                title="确认删除这条记录?"
                @confirm="deleteApi(scope.row.id)"
              >
                <template #reference>
                  <el-button v-auth="'shop.admin.payconfig.delete'" class="is-link" type="danger">
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-main>
    <sa-view-bar>
      <template #right>
        <sa-pagination :pageData="pageData" @updateFn="getData" />
      </template>
    </sa-view-bar>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from './payConfig.service';
  import { useModal } from '@/sheep/hooks';
  import { usePagination } from '@/sheep/hooks';
  import PayConfigRecyclebin from './recyclebin.vue';
  import PayConfigEdit from './edit.vue';
  const { pageData } = usePagination();

  // 列表
  const table = reactive({
    data: [],
    order: '',
    sort: '',
    selected: [],
  });
  const loading = ref(true);
  // 获取
  async function getData() {
    loading.value = true;
    const { code, data } = await api.list({
      page: pageData.page,
      size: pageData.size,
      order: table.order,
      sort: table.sort,
    });
    if (code == 200) {
      table.data = data.data;
      pageData.page = data.current_page;
      pageData.size = data.per_page;
      pageData.total = data.total;
    }
    loading.value = false;
  }
  function openRecyclebin() {
    useModal(
      PayConfigRecyclebin,
      {
        title: '回收站',
      },
      {
        confirm: () => {
          getData();
        },
        close: () => {
          getData();
        },
      },
    );
  }
  async function handleCommand(e) {
    await api.edit(e.id, {
      status: e.type,
    });
    getData();
  }
  function addRow() {
    useModal(
      PayConfigEdit,
      {
        title: '添加支付方式',
        type: 'add',
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  function editRow(row) {
    useModal(
      PayConfigEdit,
      {
        title: '编辑支付方式',
        type: 'edit',
        id: row.id,
      },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  // 删除api 单独批量可以直接调用
  async function deleteApi(id) {
    await api.delete(id);
    getData();
  }

  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .pay-config-page {
    .el-header {
      height: auto;
      --el-header-padding: 0;
    }
    .el-main {
      background: var(--sa-background-assist);
      .sa-table-wrap {
        height: 100%;
      }
    }
  }
  .status-btn {
    height: 24px;
    font-size: 12px;
    color: #999999;
    background: rgba(153, 153, 153, 0.16);
    width: 56px;
    border-radius: 4px;
    border: none;
  }
  .success-btn {
    color: #52c41a;
    background: rgba(82, 196, 26, 0.16);
  }
  .status-hidden {
    color: #999999;
  }
  .status-normal {
    color: #52c41a;
  }
</style>
