<script setup>
import { onMounted, ref, getCurrentInstance, onBeforeMount} from 'vue'
import Download from '@/components/icon/download.vue'
import Warning from '@/components/icon/warning.vue'
import Search from '@/components/icon/search.vue'
import { get, post } from '../../utils/request'
import { userStore } from '@/store/userInfo'


const { proxy } = getCurrentInstance()
const currentPage = ref(1)
const size = ref(5)
const queryString = ref('')
const teamId = ref('')
const role = ref('')
const tableData = ref([])
const teamList = ref([])
const total = ref(0)
const dialogVisible = ref(false)
const cancelEmail = ref('')
const memberId = ref('')
let click = true
const memberRole = ref('')

onMounted(() => {
  let pageText = document.getElementsByClassName('el-pagination__jump')[0]
  if (pageText) {
    pageText.childNodes[0].nodeValue = 'Jump to Page'
  }
  memberRole.value = userStore().user.role
  pagingQuery()
  getTeamList()
})
//打开对话框
const handleClick = (val) => {
  if( memberRole.value?.indexOf("1") !== -1){
    dialogVisible.value = true
    cancelEmail.value = val.email
    memberId.value = val.id
  }
}
//获取分页数据
const pagingQuery = () => {
  //初始化表格
  tableData.value = []
  get(
    '/pdf-tech/vppRTeamMemberRole/pageForAdmin/?page=' +
      currentPage.value +
      '&' +
      'pageSize=' +
      size.value +
      '&' +
      'teamId=' +
      teamId.value +
      '&' +
      'role=' +
      role.value +
      '&' +
      'queryString=' +
      queryString.value 
  ).then((res) => {
    if(res.data.code === 200){
      const data = res.data.result.list
      for (let i = 0; i < data.length; i++) {
        if (data[i].memberRole ?.indexOf("1") !== -1) {
          data[i].memberRole = 'Super admin'
        } else if (data[i].memberRole ?.indexOf("2") !== -1) {
          data[i].memberRole = 'Team admin'
        } else {
          data[i].memberRole = 'Team member'
        }
      }
      tableData.value = data
      total.value = res.data.result.total
    }
  })
}
//筛选
const searchInfo = () => {
  currentPage.value = 1
  pagingQuery()
}
// 获取团队管理列表
const getTeamList = (val) => {
  // get('/pdf-tech/vppTeam/getManageTeamList', {
  //   teamId: '',
  //   keyword: ''
  // }).then((res) => {
  //   if (res.data.code === 200) {
  //     teamList.value = res.data.result.list
  //   }
  // })
  //获取团队列表
  get('/pdf-tech/vppTeam/listWithAdmin').then((res) => {
    if (res.data.code === 200) {
      teamList.value = res.data.result
    }
  })
}
//
const deleteAdmin = (val) => {
  if(!click){return}
  click = false
  setTimeout(() => {
    click = true
  }, 2000)
  dialogVisible.value = false
  var urlencoded = new URLSearchParams()
  urlencoded.append("memberId", memberId.value)
  post('/pdf-tech/vppTeam/deleteAdmin',urlencoded).then((res) => {
    if(res.data.code === 200){
      proxy.$message({
          message: 'Delete Successfully',
          type: "success"
      })
      pagingQuery()
    }
  })
}
//切换每页条数
const handleSizeChange = (val) => {
  size.value = val
  currentPage.value = 1
  pagingQuery()
}
//切换分页
const handleCurrentChange = (val) => {
  currentPage.value = val
  pagingQuery()
}
</script>

<template>
  <div class="flex flex-col items-center">
    <div class="w-full">
      <h1 class="leading-40px">Manage Admin</h1>
      <div
        class="
          mt-36px
          mb-16px
          leading-20px
          text-16px
          font-bold
          flex
          justify-between
        "
      >
        <span>Content</span>
        <div class="flex" v-if="memberRole?.indexOf('1') !== -1">
          <router-link
            :to="{ path: '/manage-admin/add-admin', query: {} }"
            class="
              bg-[#1460F3]
              h-28px
              w-111px
              flex
              justify-center
              items-center
              font-500
              text-14px
              ml-12px
              rounded-4px
              text-[#fff]
              hover:opacity-80
            "
          >
            Add Admin
          </router-link>
        </div>
      </div>
      <div class="flex bg-[#fff] pt-32px px-24px rounded-t-8px w-full">
        <select class="w-140px" v-model="teamId" :class="{ '!text-[#232A40]': teamId !== '' }">
          <option value="" selected>Team</option>
          <option v-for="item in teamList" :key="item.value" :value="item.id">
            {{ item.name }}
          </option>
        </select>
        <select class="w-140px ml-16px" v-model="role" :class="{ '!text-[#232A40]': role !== '' }">
          <option value="" selected>Role</option>
          <option value="1">Super admin</option>
          <option value="2">Team admin</option>
        </select>
        <div class="relative">
          <el-input
            v-model="queryString"
            size="mini"
            class="!w-316px ml-16px input-with-select"
            placeholder="Search Name/Email"
          >
          </el-input>
          <button type="button" @click="searchInfo()" class="absolute top-8px right-8px"><Search /></button>
        </div>
        <button
          type="button"
          @click="searchInfo()"
          class="
            w-70px
            h-28px
            border-1px border-[#1460F3]
            rounded-4px
            ml-16px
            text-[#1460F3]
            hover:opacity-80
          "
        >
          Confirm
        </button>
      </div>
      <el-table :data="tableData" class="px-24px rounded-b-8px w-full">
        <el-table-column prop="fullName" label="Name"> </el-table-column>
        <el-table-column prop="email" label="Email"> </el-table-column>
        <el-table-column prop="teamNames" label="Team"> </el-table-column>
        <el-table-column prop="memberRole" label="Role"> </el-table-column>
        <el-table-column prop="createdAt" label="Added date"> </el-table-column>
        <el-table-column prop="operate" label="Action">
          <template slot-scope="scope">
            <div class="flex flex-col" v-if="scope.row.memberRole !== 'Super admin' && memberRole?.indexOf('1') !== -1">
              <router-link
                :to="{ path: '/manage-admin/edit-admin', query: { Email: scope.row.email, Team: scope.row.teamName, Id: scope.row.id } }"
                class="
                  w-60px
                  h-20px
                  flex
                  justify-center
                  items-center
                  rounded-4px
                  border-1px border-[#1460F3]
                  text-[#1460F3]
                  leading-12px
                "
              >
                Edit
              </router-link>
              <button
                @click="handleClick(scope.row)"
                type="text"
                class="
                  w-60px
                  h-20px
                  mt-8px
                  rounded-4px
                  border-1px border-[#1460F3]
                  text-[#1460F3]
                  leading-12px
                "
              >
                Delete
              </button>
            </div>
            <el-dialog title="" :visible.sync="dialogVisible" width="376px" top="30vh" center :show-close="false">
              <Warning />
              <p class="mt-16px">Are you sure you want to delete team admin?</p>
              <p>
                Team Admin Email: <span class="text-16px font-bold">{{
                  cancelEmail
                }}</span>
              </p>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">Cancel</el-button>
                <el-button type="primary" @click="deleteAdmin(scope.row)">
                  Delete
                </el-button>
              </span>
            </el-dialog>
          </template>
        </el-table-column>
        <p slot="empty">No Data Available</p>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[5, 10, 20]"
        :page-size="size"
        :background="true"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        class="px-24px !rounded-0 rounded-b-8px mt-20px flex justify-end"
      >
      </el-pagination>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.el-table::v-deep .el-table__cell{
  padding-right: 20px !important;
  padding-left: 20px !important;
}

.el-table__header-wrapper::v-deep .el-table__header {
  display: flex;
  width: 1100px !important;
}

.el-table__header {
  display: flex;
  width: 1100px !important;
}

.el-table::v-deep thead {
  color: #000 !important;
}
::v-deep .input-with-select .el-input__inner {
  padding: 0 20px 0 8px;
  border-color: #d9d9d9;
  font-size: 14px;
  color: #232A40;
}
::v-deep .input-with-select .el-input__inner::placeholder {
  font-size: 14px;
  font-weight: 400;
  color: #808185;
}
.el-table::v-deep .cell{
  word-break: break-word;
}
</style>

