<template>
  <div class="page-prod-comment app-container">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        label-width="auto"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="spuName"
            label="商品名称:"
          >
            <el-input
              v-model="searchForm.spuName"
              type="text"
              placeholder="商品名称"
            />
          </el-form-item>
          <el-form-item
            prop="nickName"
            label="用户昵称:"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              placeholder="用户昵称"
            />
          </el-form-item>
          <el-form-item label="记录时间:">
            <el-date-picker
              v-model="recDateRange"
              type="datetimerange"
              :range-separator="'至'"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="'开始时间'"
              :end-placeholder="'结束时间'"
              :default-time="defaultTime"
            />
          </el-form-item>
          <el-form-item label="回复时间:">
            <el-date-picker
              v-model="replyDateRange"
              type="datetimerange"
              :range-separator="'至'"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="'开始时间'"
              :end-placeholder="'结束时间'"
              :default-time="defaultTime"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="filter-item"
              @click="onSearch(true)"
            >
              搜索
            </el-button>
            <el-button
              @click="onResetSearch"
            >
              重置
            </el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-content">
      <div class="table-con">
        <el-table
          ref="prodCommTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            align="left"
            prop="spuName"
            label="商品名称"
            width="360"
          >
            <template #default="scope">
              <div class="table-cell-con">
                <div class="table-cell-image">
                  <ImgShow
                    :src="scope.row.spuImg"
                    :img-style="{width:'60px',height:'60px'}"
                  />
                </div>
                <span class="table-cell-text">{{ scope.row.spuName }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="nickName"
            label="用户昵称"
          >
            <template #default="scope">
              <span v-if="scope.row.isAnonymous">匿名用户</span>
              <span
                v-else
                class="table-cell-text"
              >{{ scope.row.nickName || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="score"
            label="评价得分"
          />
          <el-table-column
            align="left"
            prop="isAnonymous"
            label="是否匿名"
          >
            <template #default="scope">
              <div class="tag-text">
                {{ ['否', '是'][scope.row.isAnonymous] }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="createTime"
            label="记录时间"
            width="250"
          >
            <template #default="scope">
              <div>{{ scope.row.createTime || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="replyTime"
            label="回复时间"
            width="250"
          >
            <template #default="scope">
              <div>{{ scope.row.replyTime || '-' }}</div>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="200"
          >
            <template #default="{row}">
              <el-button
                v-permission="['prod:prodComm:update']"
                type="primary"
                link
                @click="onAddOrUpdate(row.spuCommId,true)"
              >
                编辑
              </el-button>
              <el-button
                type="primary"
                link
                @click="onAddOrUpdate(row.spuCommId,false)"
              >
                查看
              </el-button>
              <el-button
                v-permission="['prod:prodComm:delete']"
                type="primary"
                link
                @click="onDelete(row.spuCommId)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="pagination-box">
      <el-pagination
        v-if="dataList.length"
        class="pagination-item"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'
import * as api from '@/api/product/prod-comm'
import { ElMessage, ElMessageBox } from 'element-plus'

const defaultTime = [
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
]
const recDateRange = ref([])
const replyDateRange = ref([])
const dataList = reactive([])
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  spuName: '',
  nickName: ''
})

onMounted(() => {
  onGetDataList()
})

let tempSearchForm = null // 保存上次点击查询的请求条件
const dataListLoading = ref(false)
const onGetDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    tempSearchForm = {
      ...searchForm,
      pageNum: pageParam == null ? page.currentPage : pageParam.currentPage,
      pageSize: pageParam == null ? page.pageSize : pageParam.pageSize
    }
    if (recDateRange.value.length !== 0) {
      tempSearchForm.createStartTime = recDateRange.value[0]
      tempSearchForm.createEndTime = recDateRange.value[1]
    }
    if (replyDateRange.value.length !== 0) {
      tempSearchForm.replyStartTime = replyDateRange.value[0]
      tempSearchForm.replyEndTime = replyDateRange.value[1]
    }
  } else {
    tempSearchForm.pageNum = pageParam == null ? page.currentPage : pageParam.currentPage
    tempSearchForm.pageSize = pageParam == null ? page.pageSize : pageParam.pageSize
  }
  api.page({
    ...tempSearchForm
  }).then(data => {
    // dataList.length = 0
    onHandleArr(dataList, data.list)
    page.total = data.total
  })
}

// 新增 / 修改
const addOrUpdateRef = ref()
const addOrUpdateVisible = ref(false)
const onAddOrUpdate = (id, isEdit) => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id, isEdit)
  })
}

const onDelete = (spuCommId) => {
  ElMessageBox.confirm($t('admin.isDeleOper') + '?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    api.deleteById(spuCommId).then(() => {
      ElMessage({
        message: '操作成功',
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  }).catch(() => { })
}

// 刷新回调用
const refreshChange = () => {
  onGetDataList(page)
}

const onSearch = (newData = false) => {
  onGetDataList(page, newData)
}

// 重置表单
const searchFormRef = ref()
const onResetSearch = () => {
  searchFormRef.value.resetFields()
  replyDateRange.value = []
  recDateRange.value = []
  onGetDataList(page, true)
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  onGetDataList()
}

const onPageChange = (val) => {
  page.currentPage = val
  onGetDataList()
}

// 数组赋值
const onHandleArr = (arr, data) => {
  arr.length = 0
  arr.push(...data)
}
</script>

<style lang="scss" scoped>
.page-prod-comment {
  .search-bar {
    background-color: #F7F8FA;
    .input-row {
      display: block;
    }
  }
  .pagination-box{
    text-align: right;
    padding: 20px 0;
    .pagination-item{
      justify-content: flex-end;
    }
  }
}
</style>
