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

    <CCard>
      <CCardHeader>
        <slot name="header">
          <CIcon name="cil-grid" />
          {{ list_title }}
        </slot>
      </CCardHeader>

      <CCardBody>
        <el-table :data="filteredByType" 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="caption" label="متن" width="">
            <template slot-scope="scope">
              <span class="singleLineTxt" :title="scope.row.caption"> {{ scope.row.caption }} </span>
            </template>
          </el-table-column>

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

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

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

          <el-table-column label="ویرایش" width="110" align="center">
            <template slot-scope="scope">
              <CButton :key="scope.row._id" color="danger" variant="outline" @click="removeAnnouncement(scope.row._id)">
                <i class="fal fa-trash-alt"></i>
              </CButton>
              <CButton
                :key="scope.row._id + type"
                color="success"
                variant="outline"
                :to="{ name: 'admin-announcements-item', params: { item: scope.row._id }, query: { ...$route.query } }"
              >
                <i class="fal fa-eye"></i>
              </CButton>
            </template>
          </el-table-column>
        </el-table>
      </CCardBody>
    </CCard>
  </div>
</template>

<script>
export default {
  name: 'AdminAnnouncementsList',
  layout: 'admin',
  async asyncData({ $axios, error }) {
    try {
      const announcements = await $axios.get('/api/admin/announcements')
      return {
        announcements: announcements.data
      }
    } catch (err) {
      error({ status: 404, message: 'There is a problem here' })
    }
  },
  data() {
    return {
      list_title: 'لیست',
      announcements: null
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  computed: {
    title() {
      if (this.type === 'user') return 'اعلانات مشتریان'
      if (this.type === 'agent') return 'اطلاعیه های نمایندگان'
      return 'invalid params'
    },
    type() {
      if (this.$route.query?.user) return 'user'
      if (this.$route.query?.agent) return 'agent'
      return null
    },
    filteredByType() {
      return this.announcements.filter(item => {
        if (this.type === 'user' && !item.isForAgents) return item
        if (this.type === 'agent' && item.isForAgents) return item
        return false
      })
    }
  },
  methods: {
    removeAnnouncement(id) {
      this.$confirm('این مورد حذف شود؟', 'هشدار', {
        confirmButtonText: 'بله',
        cancelButtonText: 'لغو',
        type: 'warning'
      })
        .then(() => {
          this.$axios
            .delete(`/api/admin/announcements/${id}`)
            .then(res => {
              this.announcements = this.announcements.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>
