<template>
  <div class="table-container">
<el-form :inline="true" :model="queryForm" class="demo-form-inline">
  
    <!-- <el-form-item label="状态">
    <el-select v-model="queryForm.status" style="width: 300px;" placeholder="请选择状态">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value">
     </el-option>
     </el-select>
  </el-form-item> -->
  <el-form-item label="">
    <el-input style="width: 300px;" v-model="queryForm.status_Inquiry" placeholder="请输入内容查询"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="handleQuery">查询</el-button>
  </el-form-item>
</el-form>
    <vab-query-form>
      <vab-query-form-left-panel >
        <el-button  icon="el-icon-plus" type="primary" @click="handleAdd">添加</el-button>
          <el-button  icon="el-icon-delete" type="primary" @click="handleDelete">导出</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>
  <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
      @sort-change="tableSortChange"
    >
      <el-table-column align="center" label="订单ID" prop="订单ID" width="100" />
      <el-table-column align="center" label="订单号" prop="订单号" min-width="160" show-overflow-tooltip />
      <el-table-column align="center" label="订单状态" prop="订单状态" width="100" />
      <el-table-column align="center" label="订单内容" prop="订单内容" width="100" />
      <el-table-column align="center" label="创建时间" prop="创建时间" width="165" />
      <el-table-column align="center" label="支付时间" prop="支付时间" width="165" />
      <el-table-column align="center" label="实付金额" prop="实付金额" width="100" />
      <el-table-column align="center" label="用户ID" prop="用户ID" width="100" />
      <el-table-column align="center" label="手机号" prop="手机号" width="120" />
      <el-table-column align="center" label="地址" prop="地址" min-width="180" show-overflow-tooltip />
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="{ row }">
          <el-button type="text" @click="handleView(row, ids)">查看</el-button>
          <el-button type="text" v-if="row.订单状态 == '2' " @click="handleEdit(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 { getProductOrders, expormallOrdern } 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]);
    }
    }]
    },
     options: [
       {
          value: '1',
          label: '上架'
        },{
          value: '2',
          label: '下架'
        }],
        value: '',
    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) {
      const id = row['价格ID'] || row['商品ID']
      if (id != null) {
        this.$baseConfirm('确定要删除当前商品吗？', null, async () => {
          const { msg } = await deletePackage({ ids: id })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      }
    },
     handleDelete() {
        expormallOrdern().then(
  response => {
     window.location.href = response.data.url;  
  }
).catch(error => {
  console.error('下载失败', error)
  this.loading = false // 停止加载，隐藏加载状态
})
      },
    getImageUrl(path) {
      if (!path) return ''
      if (path.startsWith('http')) return path
      return `https://static.wagoo.pet/${path}`
    },
    tableSortChange() {
      const imageList = (this.$refs.tableSort && this.$refs.tableSort.tableData) || []
      this.imageList = imageList.map((item) => item['商品图片']).filter(Boolean)
    },
    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() {
    expormallOrdern().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()
    },
    onSubmit(){
      console.log(111)

    },
    handleQuery() {
    this.queryForm.pageNo = 1
    this.fetchData()
    },
    async fetchData() {
      this.listLoading = true
      try {
        const res = await getProductOrders(this.queryForm)
        const data = res.data || []
        const total = res.total != null ? res.total : data.length
        this.list = Array.isArray(data) ? data : []
        this.total = total
      } catch (e) {
        this.list = []
        this.total = 0
      }
      this.timeOutID = setTimeout(() => {
        this.listLoading = false
      }, 300)
    },
    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>
