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

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

const router = useRouter();

const statusArr = ref([
  { name: "潜在用户", value: 1 },
  { name: "ficc", value: 3 },
  { name: "现有用户", value: 2 },
]);

const tableData = ref([]);
const total = ref(0);
const page_no = ref(sessionStorage.getItem("applyUserListBack") ? JSON.parse(sessionStorage.getItem("applyUserListBack")).page_no : 1);
const pageSize = ref(10);

/* 获取表格数据 */
const getTableData = () => {
  raiInterface
    .applyList({
      PageSize: pageSize.value,
      CurrentIndex: page_no.value,
      KeyWord: search_txt.value,
      CustomType: custom_type.value,
    })
    .then((res) => {
      if (res.Ret === 200) {
        tableData.value = res.Data.List || [];
        total.value = res.Data.Paging.Totals;
      }
    });
};

const custom_type = ref("");
const search_txt = ref("");
const showViewer = ref(false);
const reviewList = ref([]);
const isShowDia = ref(false); //处理弹窗
const dealObj = ref({});

/* 页码改变 */
const handleCurrentChange = (page) => {
  page_no.value = page;
  getTableData();
};
/* 预览名片 */
const previewImg = (item) => {
  showViewer.value = true;
  reviewList.value = item.BusinessCardUrl.split("#");
};
/* 关闭预览 */
const closeViewer = () => {
  reviewList.value = [];
  showViewer.value = false;
};
const dealHandle = (item) => {
  dealObj.value = item;
  isShowDia.value = true;
};
/* 处理申请 */
const dealApplyHandle = () => {
  raiInterface
    .delApply({
      ApplyRecordId: dealObj.value.ApplyRecordId,
    })
    .then((res) => {
      if (res.Ret === 200) {
        // this.$message.success(res.Msg);
        dealObj.value.Status = 1;
        isShowDia.value = false;
      }
    });
};
// 申请内容
const applicationContent = (item) => {
  window.open(item.HttpUrl, "_blank");
};

watch(
  [() => search_txt.value, () => custom_type.value],
  () => {
    page_no.value = 1;
    getTableData();
  },
  {
    deep: true,
  }
);

onMounted(() => {
  if (sessionStorage.getItem("applyUserListBack")) {
    let backData = JSON.parse(sessionStorage.getItem("applyUserListBack"));
    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("applyUserListBack", JSON.stringify(backData));
  next();
});
</script>

<template>
  <div class="applyUserList_container">
    <div class="applyUserList_top">
      <el-select v-model="custom_type" placeholder="请选择状态" style="width: 200px; margin-right: 16px" clearable>
        <el-option v-for="item in statusArr" :key="item.name" :label="item.name" :value="item.value"> </el-option>
      </el-select>
      <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="applyUserList_cont">
      <el-table ref="userTable" :data="tableData" v-loading="isShowloadding" element-loading-text="数据加载中..." border>
        <el-table-column prop="RealName" label="姓名" align="center">
          <template #default="scope">
            <span>{{ scope.row.RealName }}</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="Email" label="邮箱" align="center" min-width="120px">
          <template #default="scope">
            <span>{{ scope.row.Email }}</span></template
          >
        </el-table-column>
        <el-table-column prop="CompanyName" label="客户名称" align="center">
          <template #default="scope">
            <span
              style="color: #409eff; cursor: pointer"
              v-if="scope.row.CompanyIdPay !== 1"
              @click="
                router.push({
                  path: '/RaiDetail',
                  query: {
                    id: scope.row.CompanyIdPay,
                  },
                })
              "
              >{{ scope.row.CompanyName }}</span
            >
            <span v-else>{{ scope.row.CompanyName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="SellerName" label="所属销售" align="center" width="125">
          <template #default="scope">
            <span>{{ scope.row.SellerName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="CreateTime" label="申请时间" width="160" align="center">
          <template #default="scope">
            <span>{{ scope.row.CreateTime }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="CompanyIdTypeName" label="用户状态" min-width="130" align="center">
          <template #default="scope">
            <span>{{ scope.row.CompanyIdTypeName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="ApplicationSource" label="申请来源" width="115" align="center">
          <template #default="scope">
            <span>{{ scope.row.ApplicationSource }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="Title" label="申请内容" min-width="130" align="center">
          <template #default="scope">
            <span class="editsty" @click="applicationContent(scope.row)">{{ scope.row.Title }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" min-width="110">
          <template #default="scope">
            <span class="editsty" style="margin-right: 10px" v-if="scope.row.BusinessCardUrl" @click="previewImg(scope.row)">查看名片</span>
            <span class="editsty" style="margin-right: 10px" v-if="scope.row.Status === 0" @click="dealHandle(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" />
    <!-- 弹窗 -->
    <intervewDia :title="'deal'" :isShowDia="isShowDia" @cancel="isShowDia = false" @saveOver="dealApplyHandle" />
  </div>
</template>

<style lang="scss" scoped>
.applyUserList_container {
  .applyUserList_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);
  }
  .applyUserList_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>
