<template>
  <div class="table-container">
    
    <!-- <vab-page-header description="功能强大的数据表格组件，支持排序、分页、搜索等功能" :icon="['fas', 'table']" title="表格管理" /> -->
    <el-row>
  <el-col :span="22">
     <el-form ref="form"  :inline="true" :model="queryForm" @submit.native.prevent>
       <el-form-item label="查询时间" >
            <el-date-picker
               v-model="queryForm.time"
                 unlink-panels
                value-format="yyyy-MM-dd"
               type="daterange"
               range-separator="-"
               start-placeholder="开始日期"
               end-placeholder="结束日期"
                :picker-options="pickerOptions"
            ></el-date-picker>
         </el-form-item>
           </el-form>
  </el-col>
     <el-form ref="form" style="maigin-left:50px;"  :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>
</el-row>

    <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="100" label="用户ID" prop="用户ID"  />
         <el-table-column align="center" width="100" label="管家ID" prop="管家ID"  />
           <el-table-column align="center" width="100" label="管家名称" prop="管家名称"  />
         <el-table-column align="center"  width="160" label="昵称" prop="昵称"  />
       <el-table-column align="center" width="100" label="用户头像" prop="用户头像">
         <template slot-scope="scope">
          <img v-if="scope.row.用户头像" :src="scope.row.用户头像" alt="图片" style="width: 30px; height: 30px;">
        </template>
          </el-table-column>
      <el-table-column  align="center" width="100" label="评论ID" prop="评论ID"  />
      <el-table-column  align="center" width="100" label="订单ID" prop="订单ID"  />
       <el-table-column  align="center" width="180" label="订单号" prop="订单号"  />
      <el-table-column  align="center" width="100" label="星级" prop="星级"  />
        <el-table-column align="center"  width="160" label="评论内容" prop="评论内容"  />
        <el-table-column align="center"  width="200" label="评论图片" prop="评论图片"  >
         <template    slot-scope="scope" >
          <div style="display: flex;overflow:auto;">
              <div v-for="(image, index) in scope.row.评论图片" :key="index" >
        <img v-if="image" :src="image" alt="图片" @click="previewImage(image)" style="width: 50px; height: 50px;margin-left:10px;">
      </div>
          </div>
        </template>
          </el-table-column>
        <el-table-column align="center"  width="160" label="评论时间" prop="评论时间"  />
       
      <!-- <el-table-column label="头像" >
        <template #default="{ row }">
          <el-image v-if="imgShow" :preview-src-list="imageList" :src="row.img" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="点击量" prop="pageViews"  sortable /> -->
      <!-- <el-table-column label="状态" >
        <template #default="{ row }">
          <el-tooltip class="item" :content="row.status" effect="dark" placement="top-start">
            <el-tag :type="row.status | statusFilter">
              {{ row.status }}
            </el-tag>
          </el-tooltip>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="时间" prop="datetime"  width="200" /> -->
      <!-- <el-table-column label="操作"  fixed="right" width="60px">
        <template #default="{ row }">
          <el-button v-if="row.订单状态 == '已派单' || row.订单状态 == '已支付'  " type="text" @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 { doDelete, commentList } 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 {
           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: {
      previewImage(url) {
        
      // 方法1: 使用window.open在新标签页中打开图片
      window.open(url, '_blank');
      
      // 方法2: 使用Vue的Modal组件（例如使用Element UI的Dialog）
      // this.$refs.imageModal.showImage(url);
    },
      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()
      },
      handleEdit(row) {
        console.log(row,'---')
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids: ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return 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 commentList(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>
