<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel >
        <el-button  icon="el-icon-plus" type="primary" @click="handleAdd">领养宠物录入</el-button>
        <!-- <el-button :disabled="true" icon="el-icon-delete" type="danger" @click="handleDelete">删除</el-button> -->
        <!-- <el-button type="primary" @click="testMessage">baseMessage</el-button>
        <el-button type="primary" @click="testALert">baseAlert</el-button>
        <el-button type="primary" @click="testConfirm">baseConfirm</el-button>
        <el-button type="primary" @click="testNotify">baseNotify</el-button> -->
      </vab-query-form-left-panel>
      <!-- <vab-query-form-right-panel>
        <el-form ref="form" :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model="queryForm.status_Inquiry" placeholder="输入券名称支持模糊查询" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" native-type="submit" type="primary" @click="handleQuery">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel> -->
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
      @sort-change="tableSortChange"
    >
      <!-- <el-table-column  type="selection" width="55" /> -->
      <!-- <el-table-column label="序号"  width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column> -->
       <el-table-column align="center" width="180" label="券包ID" prop="券包ID"  />
      <el-table-column  align="center"  label="券包价格" prop="券包价格"  />
      <el-table-column  align="center" label="券包名称" prop="券包名称"  />
      <!-- <el-table-column align="center" width="200" label="券规则" prop="券规则"  /> -->
        <!-- <el-table-column align="center"  width="120" label="手机号" prop="手机号"  /> -->
        <!-- <el-table-column align="center" width="200" label="抵扣条件" prop="抵扣条件"  />
        <el-table-column align="center"  width="100" label="抵扣金额" prop="抵扣金额"  /> -->
          <!-- <el-table-column align="center"  width="100" label="有效期" prop="有效期"  /> -->
             <!-- <el-table-column align="center"  width="100" label="起始时间" prop="起始时间"  />
            <el-table-column align="center"  width="100" label="结束时间" prop="结束时间"  /> -->
      <el-table-column label="操作"  fixed="right" width="150px">
        <template #default="{ row }">
          <el-button  type="text" @click="handleView(row,ids)">查看</el-button>
          <el-button  type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="hdleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <table-edit ref="edit" />
  </div>
</template>

<script>
  import { deletePackage, couponPackage,exportUserOperateAdmin } from '@/api/table'
  import TableEdit from './components/TableEdit'
  import VabPageHeader from '@/components/VabPageHeader'

  export default {
    name: 'ComprehensiveTable',
    components: {
      TableEdit,
      VabPageHeader,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        ids:true,
          pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
        imgShow: true,
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          title: '',
        },
        timeOutID: null,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    mounted() {},
    methods: {
        hdleDelete(row) {
          console.log(row,'--=')
        if (row.券包ID) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await deletePackage({ ids: row.券包ID })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } 
      },
    
      tableSortChange() {
        const imageList = []
        this.$refs.tableSort.tableData.forEach((item, index) => {
          imageList.push(item.img)
        })
        this.imageList = imageList
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleView(row,ids){
        this.$refs['edit'].showEdit(row,ids)
      },
      handleEdit(row) {
        // console.log(row,'---')
        this.$refs['edit'].showEdit(row)
      },
      handleDelete() {
        exportUserOperateAdmin().then(
  response => {
     window.location.href = response.data.url;  
    // const url = window.URL.createObjectURL(new Blob([response.data])) // 创建下载链接
    // const link = document.createElement('a')
    // link.href = url
    // link.download = '全文导入模板.xlsx' // 设置文件名
    // document.body.appendChild(link)
    // link.click() // 触发下载
    // document.body.removeChild(link) // 下载后移除元素
  }
).catch(error => {
  console.error('下载失败', error)
  this.loading = false // 停止加载，隐藏加载状态
})
      
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data, total } = await couponPackage(this.queryForm)
        this.list = data
        const imageList = []
        data.forEach((item, index) => {
          imageList.push(item.img)
        })
        this.imageList = imageList
        this.total = total
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
      testMessage() {
        this.$baseMessage('test1', 'success')
      },
      testALert() {
        this.$baseAlert('11')
        this.$baseAlert('11', '自定义标题', () => {
          /* 可以写回调; */
        })
        this.$baseAlert('11', null, () => {
          /* 可以写回调; */
        })
      },
      testConfirm() {
        this.$baseConfirm(
          '你确定要执行该操作?',
          null,
          () => {
            /* 可以写回调; */
          },
          () => {
            /* 可以写回调; */
          }
        )
      },
      testNotify() {
        this.$baseNotify('测试消息提示', 'test', 'success', 'bottom-right')
      },
    },
  }
</script>
