<script>
import { defineComponent } from "vue";
export default defineComponent({
  beforeRouteEnter(to, from, next) {
    if (from.path != "/customDetail") {
      sessionStorage.removeItem("adviceListBack");
    }
    next();
  },
});
</script>

<script setup>
import { ref, onMounted, watch } from "vue";
import { raiInterface } from "@/api/api.js";
import mPage from '@/components/mPage.vue'
import { onBeforeRouteLeave, useRouter } from "vue-router";
import { Search } from "@element-plus/icons-vue";

const router = useRouter();

const tableData = ref([]);
const total = ref(0);
const page_no = ref(sessionStorage.getItem("adviceListBack") ? JSON.parse(sessionStorage.getItem("adviceListBack")).page_no : 1);
const pageSize = ref(10);
const search_txt = ref("");
const showViewer = ref(false);
const reviewList = ref([]);

watch(
  () => search_txt.value,
  () => {
    page_no.value = 1;
    getTableData();
  }
);


/* 获取表格数据 */
const getTableData = () => {
  raiInterface
    .adviceList({
      PageSize: pageSize.value,
      CurrentIndex: page_no.value,
      KeyWord: search_txt.value,
    })
    .then((res) => {
      if (res.Ret === 200) {
        tableData.value = res.Data.List || [];
        total.value = res.Data.Paging.Totals;
      }
    });
};
/* 页码改变 */
const handleCurrentChange = (page) => {
  page_no.value = page;
  getTableData();
};
/* 预览名片 */
const previewImg = (item) => {
  showViewer.value = true;
  reviewList.value = item.AdviceImgUrl.split("#");
};

/* 关闭预览 */
const closeViewer = () => {
  reviewList.value = [];
  showViewer.value = false;
};
onMounted(() => {
  if (sessionStorage.getItem("adviceListBack")) {
    let backData = JSON.parse(sessionStorage.getItem("adviceListBack"));
    search_txt.value = backData.keyword;
    page_no.value = backData.page_no;
  }
  getTableData();
});

onBeforeRouteLeave((to, form, next) => {
  let backData = {
    page_no: page_no.value,
    keyword: search_txt.value,
  };
  sessionStorage.setItem("adviceListBack", JSON.stringify(backData));
  next();
});
</script>

<template>
  <div class="adviceList_container">
    <div class="adviceList_top">
      <el-input placeholder="姓名/手机号" v-model="search_txt" clearable style="max-width: 500px">
        <template #prefix>
          <el-icon class="el-input__icon"><search /></el-icon>
        </template>
      </el-input>
    </div>
    <div class="adviceList_cont">
      <el-table ref="userTable" :data="tableData" v-loading="isShowloadding" element-loading-text="数据加载中..." border>
        <el-table-column prop="UserRealName" label="姓名" align="center">
          <template #default="scope">
            <span>{{ scope.row.UserRealName }}</span></template
          >
        </el-table-column>
        <el-table-column prop="Mobile" label="手机号" align="center">
          <template #default="scope">
            <span>{{ scope.row.Mobile }}</span></template
          >
        </el-table-column>
        <el-table-column prop="CompanyName" label="客户名称" align="center">
          <template #default="scope">
            <span
              style="color: #409eff; cursor: pointer"
              @click="
                router.push({
                  path: '/RaiDetail',
                  query: {
                    id: scope.row.CompanyId,
                  },
                })
              "
              >{{ scope.row.CompanyName }}</span
            ></template
          >
        </el-table-column>
        <el-table-column prop="SalesRealName" label="所属销售" align="center">
          <template #default="scope">
            <span>{{ scope.row.SalesRealName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="Advice" label="优化建议" align="center" min-width="300px">
          <template #default="scope">
            <span>{{ scope.row.Advice }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="CreateTime" label="创建时间" min-width="130px" align="center">
          <template #default="scope">
            <span>{{ scope.row.CreateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="110">
          <template #default="scope">
            <span class="editsty" v-if="scope.row.AdviceImgUrl" @click="previewImg(scope.row)">查看图片</span>
          </template>
        </el-table-column>
        <template #empty>
          <div style="line-height: 44px; margin: 60px 0; color: #999">
            <img src="~@/assets/img/cus_m/nodata.png" alt="" style="display: block; width: 160px; height: 128px; margin: auto" />
            <span>暂无数据</span>
          </div>
        </template>
      </el-table>
      <el-col :span="24" class="toolbar">
        <m-page :total="total" :page_no="page_no" @handleCurrentChange="handleCurrentChange" />
      </el-col>
    </div>
    <!-- 图片预览 -->
    <el-image-viewer v-if="showViewer" @close="closeViewer" :url-list="reviewList" />
  </div>
</template>

<style lang="scss" scoped>
.adviceList_container {
  .adviceList_top {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    border: 1px solid #ececec;
    padding: 20px 30px;
    background: #fff;
    border-radius: 4px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.05);
  }
  .adviceList_cont {
    min-height: calc(100vh - 320px);
    padding: 20px 30px 80px;
    background: #fff;
    margin-top: 20px;
    position: relative;
    border: 1px solid #ececec;
    border-radius: 4px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.05);
  }
}
</style>
