<template>
  <div>
    <CustomSubHeader>
      <CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
      <CButton
        size="sm"
        color="success"
        :to="{ name: 'admin-lottery-lottery', params: { lottery: 'new' } }"
        class="mr-auto"
        >افزودن</CButton
      >
    </CustomSubHeader>

    <CCard>
      <CCardHeader>
        <slot name="header">
          <el-input v-model="search" size="mini" clearable placeholder="کد پیگیری برنده را وارد کنید" />
        </slot>
      </CCardHeader>
      <CCardBody>
        <el-table :data="filteredLotteries" style="width: 100%">
          <el-table-column type="index" label="#"> </el-table-column>

          <el-table-column prop="title" label="عنوان قرعه کشی" width=""> </el-table-column>

          <el-table-column prop="winnersCount" label="تعداد شانس برنده شدن" width=""> </el-table-column>

          <el-table-column label="تاریخ ایجاد" width="">
            <template slot-scope="scope">
              <span> {{ $jDate(scope.row.created_at) }} </span>
            </template>
          </el-table-column>

          <el-table-column label="تاریخ پایان" width="">
            <template slot-scope="scope">
              <span> {{ $jDate(scope.row.expireDate) }} </span>
            </template>
          </el-table-column>

          <el-table-column label="تعداد شرکت کنندگان" width="">
            <template slot-scope="scope">
              <span> {{ scope.row.participants.length }} </span>
            </template>
          </el-table-column>

          <el-table-column prop="expired" label="وضعیت قرعه کشی" width="">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.expired" type="info">بسته شده</el-tag>
              <el-tag v-else type="success">باز</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="ویرایش" width="105" align="left">
            <template slot-scope="scope">
              <CButton
                :key="scope.row._id"
                :color="scope.row.expired ? 'success' : 'warning'"
                variant="outline"
                :to="{ name: 'admin-lottery-lottery', params: { lottery: scope.row._id } }"
              >
                <i v-if="scope.row.expired" class="fal fa-eye"></i>
                <i v-else class="fal fa-pencil"></i>
              </CButton>
              <CButton color="danger" variant="outline" @click="del(scope.row._id)">
                <i class="fal fa-trash-alt"></i>
              </CButton>
            </template>
          </el-table-column>
        </el-table>
      </CCardBody>
    </CCard>
  </div>
</template>

<script>
export default {
  name: 'AddminLotteryList',
  layout: 'admin',
  async asyncData({ $axios, error }) {
    try {
      const lotteries = await $axios.get(`/api/admin/lotteries`)
      return {
        lotteries: lotteries.data
      }
    } catch (err) {
      error({ status: 404, message: 'There is a problem here' })
    }
  },
  data() {
    return {
      title: 'لیست قرعه کشی ها',
      list_title: 'لیست',
      lotteries: null,
      search: ''
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  computed: {
    filteredLotteries() {
      if (this.search.length) {
        const filteredArray = []
        for (const lottery of this.lotteries) {
          for (const winner of lottery.winnersList) {
            if (winner.enrollCode.toLowerCase() === this.search.toLowerCase()) filteredArray.push(lottery)
          }
        }
        return filteredArray
      } else return this.lotteries
    }
  },
  methods: {
    del(id) {
      this.$confirm('این قرعه کشی حذف شود؟', 'هشدار', {
        confirmButtonText: 'بله',
        cancelButtonText: 'لغو',
        type: 'warning'
      })
        .then(() => {
          this.$axios
            .delete(`/api/admin/lottery/${id}`)
            .then(response => {
              this.lotteries = this.lotteries.filter(item => item._id !== id)
              this.$message({
                type: 'success',
                message: 'قرعه کشی حذف شد'
              })
            })
            .catch(err => {
              this.$message({
                type: 'error',
                message: err.response.data.message
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'warning',
            message: 'عملیات لغو شد'
          })
        })
    }
  }
}
</script>
