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

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

      <CCardBody>
        <el-table :data="filteredItems" style="width: 100%">
          <el-table-column type="index" label="#"> </el-table-column>

          <el-table-column prop="message" label="متن" width="">
            <template slot-scope="scope">
              <span class="singleLineTxt" :title="scope.row.message"> {{ scope.row.message }} </span>
            </template>
          </el-table-column>

          <el-table-column prop="recievers" label="تعداد مخاطبین" width="">
            <template slot-scope="scope">
              <span> {{ scope.row.recievers.length }} </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 label="ویرایش" width="75" align="center">
            <template slot-scope="scope">
              <CButton
                :key="scope.row._id"
                color="success"
                variant="outline"
                :to="{ name: 'admin-sms-item', params: { item: scope.row._id } }"
              >
                <i class="fal fa-eye"></i>
              </CButton>
            </template>
          </el-table-column>
        </el-table>
      </CCardBody>
    </CCard>
  </div>
</template>

<script>
export default {
  name: 'AdminSMSBroadcastList',
  layout: 'admin',
  async asyncData({ $axios, error }) {
    try {
      const smsBroadcasts = await $axios.get('/api/admin/smsBroadcasts')
      return {
        smsBroadcasts: smsBroadcasts.data
      }
    } catch (err) {
      error({ status: 404, message: 'There is a problem here' })
    }
  },
  data() {
    return {
      list_title: 'لیست',
      smsBroadcasts: null
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  computed: {
    title() {
      return 'لیست پیامک های ارسال شده'
    },
    filteredItems() {
      return this.smsBroadcasts
    }
  }
}
</script>
