<template>
  <div class="app-container">
    <div class="header">
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="false"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="评价时间" prop="createTime">
          <el-date-picker
            v-model="timeRange"
            type="daterange"
            placeholder="请选择评价时间"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            style="width: 400px"
            @change="handleTime"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="酒款筛选" prop="beerName">
          <el-select
            v-model="queryParams.beerId"
            placeholder="请选择酒款"
            style="width: 410px"
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="item in beerList"
              :key="item.id"
              :label="item.beerName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="评分区间" prop="overallRating">
          <div
            style="display: flex; justify-content: space-between; width: 410px"
          >
            <el-input
              :min="0"
              :max="5"
              v-model="queryParams.params.lowScore"
              style="width: 190px"
              oninput="value=value.replace(/[^0-9]/g,'')"
            ></el-input>
            <span> ~ </span>
            <el-input
              :min="0"
              :max="5"
              v-model="queryParams.params.highScore"
              style="width: 190px"
              oninput="value=value.replace(/[^0-9]/g,'')"
            ></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div
        style="
          display: flex;
          flex-direction: column;
          justify-content: flex-end;
          align-items: flex-start;
          height: 150px;
          flex: 1;
          margin-left: 10px;
        "
      >
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </div>

      <div class="right">
        <div class="statistic">
          <div class="title">累积酒评数量</div>
          <div class="value">{{ total }}</div>
          <svg-icon icon-class="beerReview" class="icon"></svg-icon>
        </div>
        <div class="top">
          <div class="title">评分TOP排行</div>
          <table>
            <thead>
              <tr class="row-title">
                <th style="width: 60px; text-align: left">TOP</th>
                <th style="width: 120px; text-align: left">酒款名称</th>
                <th style="width: 100px; text-align: center">评分</th>
                <th style="width: 100px; text-align: center">评分数量</th>
              </tr>
            </thead>
            <tbody>
              <tr v-for="(item, index) in topReviewBeers" :key="index">
                <td style="text-align: left">{{ index + 1 }}</td>
                <td
                  style="
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    overflow: hidden;
                  "
                >
                  {{ item.beerName }}
                </td>
                <td style="text-align: center">{{ item.beerOverallRating }}</td>
                <td style="text-align: center">{{ item.beerReviewsCount }}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['brewery:review:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['brewery:review:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['brewery:review:remove']"
          >删除</el-button>
        </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['brewery:review:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table
      v-loading="loading"
      :data="reviewList"
      class="zd-table"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="品牌方id" align="center" prop="breweryId" />
      <el-table-column label="酒款ID" align="center" prop="beerId" /> -->
      <el-table-column
        label="酒款名称"
        align="left"
        prop="beerName"
        min-width="200"
      >
        <template #default="scope">
          <div>{{ scope.row.beerName }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="用户id" align="center" prop="userId" /> -->
      <el-table-column
        label="评价内容"
        align="left"
        prop="reviewContent"
        show-overflow-tooltip
        min-width="300"
      ></el-table-column>
      <el-table-column label="图片" align="center" prop="reviewImg" width="100">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 60px"
            :src="scope.row.reviewImg"
            :zoom-rate="1.2"
            :max-scale="7"
            :min-scale="0.2"
            :preview-src-list="[scope.row.reviewImg]"
            :initial-index="4"
            fit="cover"
            title="点击查看大图"
            preview-teleported
          />
        </template>
      </el-table-column>
      <el-table-column
        label="综合评分"
        align="center"
        prop="overallRating"
        width="100"
      >
        <template #default="scope">
          {{ Number(scope.row.overallRating).toFixed(1) }}
        </template>
      </el-table-column>
      <el-table-column
        label="口味评分"
        align="center"
        prop="tasteRating"
        width="100"
      >
        <template #default="scope">
          {{ Number(scope.row.tasteRating).toFixed(1) }}
        </template>
      </el-table-column>
      <el-table-column
        label="香味评分"
        align="center"
        prop="aromaRating"
        width="100"
      >
        <template #default="scope">
          {{ Number(scope.row.aromaRating).toFixed(1) }}
        </template>
      </el-table-column>
      <el-table-column
        label="颜色评分"
        align="center"
        prop="colorRating"
        width="100"
      >
        <template #default="scope">
          {{ Number(scope.row.colorRating).toFixed(1) }}
        </template>
      </el-table-column>
      <el-table-column
        label="评价时间"
        align="center"
        prop="createTime"
        width="160"
      />
      <!-- <el-table-column
        label="门店ID"
        align="center"
        prop=""
        width="160"
      /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-hasPermi="['brewery:review:edit']"
            v-if="scope.row.isTop == 0"
            @click="handleSetTop(scope.row)"
            >置顶</el-button
          >
          <el-button
            link
            type="primary"
            v-hasPermi="['brewery:review:edit']"
            v-if="scope.row.isTop == 1"
            @click="handlecancelTop(scope.row)"
            >取消置顶</el-button
          >
          <!-- <el-button link type="primary" v-hasPermi="['brewery:review:edit']"
            >点赞</el-button
          > -->
          <!-- <el-button link type="primary" v-hasPermi="['brewery:review:edit']"
            >回复</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改酒评对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="reviewRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌方id" prop="breweryId">
          <el-input v-model="form.breweryId" placeholder="请输入品牌方id" />
        </el-form-item>
        <el-form-item label="啤酒id" prop="beerId">
          <el-input v-model="form.beerId" placeholder="请输入啤酒id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="评论内容">
          <editor v-model="form.reviewContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="口味评分" prop="tasteRating">
          <el-input v-model="form.tasteRating" placeholder="请输入口味评分" />
        </el-form-item>
        <el-form-item label="香味评分" prop="aromaRating">
          <el-input v-model="form.aromaRating" placeholder="请输入香味评分" />
        </el-form-item>
        <el-form-item label="颜色评分" prop="colorRating">
          <el-input v-model="form.colorRating" placeholder="请输入颜色评分" />
        </el-form-item>
        <el-form-item label="综合评分" prop="overallRating">
          <el-input v-model="form.overallRating" placeholder="请输入综合评分" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Review">
import {
  listReview,
  getReview,
  delReview,
  addReview,
  updateReview,
  topReview10BeersList,
  setTop,
  cancelTop,
} from "@/api/brewery/review";
import { allBeers } from "@/api/brewery/beer";
import { onMounted } from "vue";
const { proxy } = getCurrentInstance();

const reviewList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    breweryId: null,
    beerId: null,
    beerName: null,
    userId: null,
    reviewContent: null,
    tasteRating: null,
    aromaRating: null,
    colorRating: null,
    overallRating: null,
    params: {
      lowScore: null,
      highScore: null,
    },
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询酒评列表 */
function getList() {
  loading.value = true;
  listReview(queryParams.value).then((response) => {
    reviewList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    breweryId: null,
    beerId: null,
    beerName: null,
    userId: null,
    reviewContent: null,
    tasteRating: null,
    aromaRating: null,
    colorRating: null,
    overallRating: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("reviewRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加酒评";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getReview(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改酒评";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["reviewRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateReview(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addReview(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除酒评编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delReview(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "brewery/review/export",
    {
      ...queryParams.value,
    },
    `review_${new Date().getTime()}.xlsx`
  );
}

const topList = ref([]);
const beerList = ref([]);
function getAllBeers() {
  allBeers().then((response) => {
    beerList.value = response.data;
  });
}
// 评分前10酒款
const topReviewBeers = ref([]);
function getTopReviewBeers() {
  topReview10BeersList().then((res) => {
    topReviewBeers.value = res.data.slice(0, 3);
  });
}
// 时间筛选
const timeRange = ref([]);
function handleTime() {
  queryParams.value.params.beginTime = timeRange.value[0];
  queryParams.value.params.endTime = timeRange.value[1];
  getList();
}
// 置顶
function handleSetTop(row) {
  setTop(row.id).then(() => {
    getList();
    proxy.$modal.msgSuccess("置顶成功");
  });
}
// 取消置顶
function handlecancelTop(row) {
  cancelTop(row.id).then(() => {
    getList();
    proxy.$modal.msgSuccess("取消置顶成功");
  });
}
onMounted(() => {
  getAllBeers();
  getList();
  getTopReviewBeers();
});
</script>
<style lang="scss" scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  .right {
    display: flex;
    align-items: center;
  }
  .statistic {
    min-width: 300px;
    min-height: 137px;
    padding: 16px;
    border-radius: 14px;
    opacity: 1;
    background: #ffffff;
    box-shadow: 6px 6px 54px 0px rgba(0, 0, 0, 0.05);
    position: relative;
    .title {
      font-family: Nunito Sans;
      font-size: 16px;
      font-weight: 600;
      color: #202224;
      margin-bottom: 12px;
    }
    .value {
      font-family: Nunito Sans;
      font-size: 28px;
      font-weight: bold;
      color: #202224;
    }
    .icon {
      position: absolute;
      top: 16px;
      right: 16px;
      width: 60px;
      height: 60px;
    }
  }
  .top {
    min-width: 400px;
    min-height: 137px;
    padding: 16px;
    border-radius: 14px;
    opacity: 1;
    background: #ffffff;
    box-shadow: 6px 6px 54px 0px rgba(0, 0, 0, 0.05);
    margin-left: 30px;
    .title {
      font-family: Nunito Sans;
      font-size: 16px;
      font-weight: 600;
      color: #202224;
    }
    .row-title {
      font-family: Nunito Sans;
      font-size: 14px;
      font-weight: 800;
      line-height: normal;
      letter-spacing: 0px;
      color: #202224;
    }
  }
}
:deep(.el-popper) {
  max-width: 400px;
}
</style>
