
<script setup>
// 引入依赖
import api from '@/api/integral'
// import capi from '@/api/course'
import custom from '@/utils/custom'
import {formatDate} from '@/utils/format'
// import { toSQLLine } from '@/utils/stringFun'
// import WarningBar from '@/components/warningBar/warningBar.vue'
// import coursePool from './components/coursePool.vue'
// import exercisesPool from '@/view/course/components/exercisesPool.vue'
import {ref,onMounted,provide } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
// store
import { useUserStore } from '@/pinia/modules/user'
const userStore = useUserStore()
// 变量
const path = ref(import.meta.env.VITE_BASE_API)
// const typeList = custom.getExercisesTypeList()
const queryParams = ref({
  page:1,
  pageSize:10,
  status:'',
  province:''
})
const tableData = ref([])
const deleteVisible = ref(false)
const total = ref(0)
const dialogFormVisible = ref(false)
const dialogTitle = ref('')
const form =ref({})
const rules = ref({
  name: [{ required: true, message: '请输入课程名称', trigger: 'blur' }]
})
const content = ref(null)
// const options = ref([]) // 答案选项数组
const exam_types = ref([])
const exam_ids = ref([])
import cityList from '@/utils/city.json';
console.log(cityList)
// const cityList = ref([])
// 生命周期
onMounted(() => {
  getIntegralList()
})
const stateList = ref([
  {
    name:'正常',
    value:1
  },
  {
    name:'冻结',
    value:2
  }
])
// 方法
async function getIntegralList() {
  const res = await api.getIntegralList(queryParams.value)
  if(res.code === 0) {
    tableData.value = res.data.records
    total.value = res.data.total
  }
}
function onSubmit() {
  getIntegralList()
}
const onReset = () => {
  queryParams.value = {
    page:1,
    pageSize:10,
    name:'',
    status:'',
    subject:''
  }
}
function openDialog(type) {
  // let params = {}
  switch (type){
    case 'add':
      dialogTitle.value = '新增试卷'
        form.value = {}
      break;
    case 'edit':
      // params.question_id = question_id.value
      dialogTitle.value = '编辑试卷'
      break;
  }
  dialogFormVisible.value = true
  // router.push({name:'addCourse',params})
}
async function onDelete() {
  const ids = exam_ids.value
  const res = await api.delExamination({ exam_ids:ids })
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: res.msg
    })
    deleteVisible.value = false
    getIntegralList()
  }
}
const handleSelectionChange = (val) => {
  exam_ids.value = val.map((item) => {
      return item.exam_id
  })
}
function deleteExamFunc(row) {
  ElMessageBox.confirm('此操作将永久删除该数据, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  .then(async() => {
    const res = await api.delExamination({
          exam_ids:[row.exam_id]
    })
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '删除成功!'
      })
      getIntegralList()
    }
  },() => {
  })
}
function handleCurrentChange(val) {
  queryParams.value.page = val
  getIntegralList()
}
function handleSizeChange(val) {
  queryParams.value.pageSize = val
  getIntegralList()
}
function closeDialog(){
  dialogFormVisible.value = false
  exam_types.value = []
  form.value = {}
}
async function enterDialog() { // 提交
  form.value.teacher_id = 0
  form.value.exam_type = exam_types.value.map((item) => {
    return item = parseInt(item)
  })
  form.value.duration = parseInt(form.value.duration)
  // return
  const params = {
    ...form.value
  }
  params.question_ids = JSON.stringify(form.value.question_ids)
  params.course_ids = JSON.stringify(form.value.course_ids)
  let func_name = 'addExamination'
  if(form.value.exam_id) { // 编辑
    func_name = 'editExamination'
  }
  const res = await api[func_name](params)
  if(res.code === 0) {
    ElMessage({
      type: 'success',
      message: res.msg
    })
    closeDialog()
    getIntegralList()
  }
}
function viewOrderFunc(row) { // 查看订单
  router.push({name:'viewOrder',params:{order_id:row.order_id}})
}
function getStateName(state) {
  let str = ''
  switch (state){
    case 1:
        str = '正常'
      break;
    case 2:
        str = '冻结'
      break;
  }
  return str
}
async function integralOperationFunc(type,row) { // 冻结 解冻
  let func = '', title = ''
  if(type == 1) {
    func = 'freeze'
    title = '是否冻结该用户积分？'
  }
  else {
    func = 'unfreeze'
    title = '是否解冻该用户积分？'
  }
  // const res = await api[func]({id:row.id})
  // if(res === 0) {
  //
  // }

  ElMessageBox.confirm(title, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
  .then(async() => {
    const res = await api[func]({id:row.point_id})
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '操作成功!'
      })
      getIntegralList()
    }
  },() => {
  })
}
</script>
<template>
  <div>
    <!--    搜索框-->
    <div class="gva-search-box">
      <el-form ref="searchForm" :inline="true" :model="queryParams">
        <el-form-item label="状态">
          <el-select v-model="queryParams.status" filterable placeholder="请选择">
            <el-option
              v-for="item in stateList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属区域">
          <el-select v-model="queryParams.province" filterable placeholder="请选择">
            <el-option
              v-for="(item,i) in cityList"
              :key="item.province"
              :label="item.province"
              :value="i"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button size="small" icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--    表格数据-->
    <div class="gva-table-box">
      <!--      数据列表-->
      <el-table border :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column align="center" label="ID"  prop="point_id" sortable="custom" />
        <el-table-column align="center" label="姓名"  prop="nickname" />
        <el-table-column align="center" label="手机号" min-width="150" prop="phone" />
        <el-table-column align="center" label="所属区域" prop="province" >
          <template #default="scope">
            <div>{{custom.getProvinceName(scope.row.province)}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="剩余积分" prop="balance" />
        <el-table-column align="center" label="累计积分" prop="total" />
        <el-table-column align="center" label="邀请好友(分)" min-width="150" prop="invite" />
        <el-table-column align="center" label="购买课程(分)" min-width="150" prop="course" />
        <!-- <el-table-column align="center" label="累计消费(分)" min-width="150" prop="phone" /> -->
        <el-table-column align="center" label="抽奖(分)" prop="raffle" />
        <el-table-column align="center" label="竞拍(分)" prop="bidding" />
        <el-table-column align="center" label="状态"  >
          <template #default="scope">
            <div>{{getStateName(scope.row.status)}}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="提交时间" min-width="200" >
          <template #default="scope">
            {{formatDate(scope.row.CreatedAt)}}
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="200">
          <template #default="scope">
            <el-button
                v-if="scope.row.status === 1"
              icon="close"
              size="small"
              type="danger"
              link
              @click="integralOperationFunc(1,scope.row)"
            >冻结</el-button>
            <el-button
                v-if="scope.row.status === 2"
              icon="select"
              size="small"
              type="primary"
              link
              @click="integralOperationFunc(2,scope.row)"
            >解冻</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination
          :current-page="queryParams.page"
          :page-size="queryParams.pageSize"
          :page-sizes="[10, 30, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
  .e-img{
    width: 150px;
    height: 100px;
  }
  .option-item{
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }
  .button-box {
    padding: 10px 20px;
    .el-button {
      float: right;
    }
  }
  .warning {
    color: #dc143c;
  }
</style>
