<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">{{ $t('cuxiao.home') }}</el-breadcrumb-item>
      <el-breadcrumb-item>{{ $t('review.ddpl') }}</el-breadcrumb-item>
    </el-breadcrumb>

    <el-row :gutter="10" style="margin-top: 20px;margin-bottom: 20px;">
      <el-col :span="1.5">
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            size="mini"-->
<!--            @click="handleAdd"-->
<!--        >{{ $t('review.ck') }}-->
<!--        </el-button>-->
      </el-col>
    </el-row>
    <el-table ref="filterTable" v-loading="loding" :data="tableData" :header-cell-style="{background:'#f0f0f0'}">
<!--        <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column :label="$t('review.ddid')" align="center" prop="ddId" />
        <el-table-column :label="$t('review.pf')" align="center" prop="score" />
        <el-table-column :label="$t('review.plsj')" align="center" prop="cretim" width="180">
        </el-table-column>
        <el-table-column :label="$t('review.plnr')" align="center" prop="content" />
        <el-table-column :label="$t('review.yhid')" align="center" prop="userId" />
        <el-table-column :label="$t('review.sfnm')" align="center" prop="
anonymous">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.anonymous==0">{{$t('review.yes')}}</el-tag>
            <el-tag v-if="scope.row.anonymous==1">{{$t('review.no')}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('review.cz')" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"
            >{{$t('review.ck')}}</el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
            >{{$t('review.sc')}}</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div style="width: 100%;display: flex;justify-content: flex-end;">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.page"
          :page-size="queryParams.size"
          layout="total, prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="text" :visible.sync="open"
               :width="$t('loding.language')=='en'||$t('loding.language')=='vi'?'650px':'600px'" append-to-body>
      <el-form ref="form" :model="form"
               :label-width="$t('loding.language')=='en'||$t('loding.language')=='vi'?'180px':'100px'">
        <el-form-item :label="$t('review.ddid')" prop="ddId">
          <el-input v-model="form.ddId"  />
        </el-form-item>
        <el-form-item :label="$t('review.pf')" prop="score">
          <el-input v-model="form.score" readonly  />
        </el-form-item>
        <el-form-item :label="$t('review.plsj')" prop="cretim">
          <el-date-picker clearable
                          v-model="form.cretim"
                          type="datetime"
                          value="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="$t('review.plnr')">
          <el-input type="textarea" v-model="form.content" rows="10"/>
        </el-form-item>
        <el-form-item v-if="form.images" :label="$t('review.pltp')" prop="images">
          <!-- 使用Flex布局水平排列图片 -->
          <div class="image-container" v-if="form.images">
            <!-- 多张图片显示 -->
            <template v-if="form.images.includes(',')">
              <div v-for="(img, index) in form.images.split(',')"
                   :key="index"
                   class="image-wrapper">
                <img :src="seruer + img.trim()" class="avatar">
              </div>
            </template>
            <!-- 单张图片显示 -->
            <template v-else>
              <div class="image-wrapper">
                <img :src="seruer + form.images" class="avatar">
              </div>
            </template>
          </div>

          <!-- 无图片时显示上传图标 -->
          <div v-else class="image-container">
            <span style="line-height: 100px;">{{ $t('review.none') }}</span>
          </div>
        </el-form-item>
        <el-form-item :label="$t('review.yhid')" prop="userId">
          <el-input v-model="form.userId"   readonly/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t('mendian.cancellation') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReview, getReview, delReview, addReview, updateReview } from "@/api/review";
import googmap from "@/components/googmap.vue";
import Bus from "@/utils/Bus";
import {delshSalesPromotion} from "@/api/promotion"; // 路径按你实际情况
export default {
  name: "Review",
  components: {
    googmap
  },
  data() {
    return {
      seruer: process.env.VUE_APP_BASE_URL,
      loding: false,
      tableData: [],
      total: 0,

      //分页
      queryParams: {
        page: 1,
        size: 10,
      },
      open: false,
      form: {},
      user: {},
      text: '',
      language:this.$t('loding.lass'),
      dqmd:null,
    }
  },
  created() {
    const that = this;
    this.listReview();
    Bus.$on('l18zh',function(res){
      that.language = res;
    })
  },
  watch: {
    language(newVal) {
      // 语言变化时执行逻辑
      this.listReview();
    }
  },
  methods: {
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该数据项？').then(() => {
        delReview(ids).then(res=>{
          if(res.code===200){
            this.listReview();
            this.$message({
              type: 'success',
              message: this.$t('mendian.successful')
            });
          }else{
            this.$message({
              type: 'info',
              message: this.$t('mendian.Deletefailed')
            });
          }
        }).catch(() => {});
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getReview(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.text = this.$t('review.ck');
        // 关键：如果是减免个别商品，并且有 spId，查商品详情
      });
    },
    reset() {
      this.form = {
        id: null,
        ddId: null,
        score: null,
        cretim: null,
        content: null,
        userId: null,
        images:null,
        anonymous: null
      };

    },

    cancel() {
      this.open = false;
      this.reset();
    },
    listReview() {
      var that = this;
      that.loding = true;
      listReview(this.queryParams).then(response => {
        that.loding = false;
        this.total = response.data.total;
        this.tableData = response.data.records;
      });
    },

    //翻页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryParams.size = val;
      this.listReview();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryParams.page = val;
      this.listReview();
    },
    handleAvatarlogo(res, file) {
      // this.upimg = URL.createObjectURL(file.raw);
      this.form.poster = file.response.data;
      // console.log('上传图片',file.response);

    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error(this.$t('quanyi.ItcanonlyJPG'));
      }
      if (!isLt2M) {
        this.$message.error(this.$t('quanyi.Cannotexceed'));
      }
      return isJPG && isLt2M;
    },



  }
}
</script>

<style>
/* 无图片提示样式 */
.no-images {
  color: #999;
  font-style: italic;
  padding: 10px;
  border: 1px dashed #eee;
  border-radius: 4px;
  text-align: center;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}

/* 图片容器 - 水平排列 */
.image-container {
  display: flex;
  flex-wrap: wrap; /* 不换行 */
  overflow-x: auto;  /* 水平滚动（当图片多时） */
  gap: 10px;         /* 图片间距 */
}

/* 图片包裹容器 */
.image-wrapper {
  position: relative;
}
</style>
