<template>
  <div>
    <CustomSubHeader>
      <CBreadcrumb class="border-0 mb-0">{{ title }}</CBreadcrumb>
    </CustomSubHeader>

    <CRow>
      <CCol col="col">
        <CCard>
          <CCardHeader>
            <slot name="header">
              <i class="fal fa-filter"></i>
              <span>فیلتر</span>
            </slot>
          </CCardHeader>
          <CCardBody>
            <CButtonGroup>
              <CButton color="primary" :class="filter === 'all' && 'selected'" @click="filter = 'all'">همه</CButton>
              <CButton color="warning" :class="filter === 'fa_unread' && 'selected'" @click="filter = 'fa_unread'">خوانده نشده فارسی</CButton>
              <CButton color="warning" :class="filter === 'en_unread' && 'selected'" @click="filter = 'en_unread'">خوانده نشده انگلیسی</CButton>
              <CButton color="success" :class="filter === 'fa_read' && 'selected'" @click="filter = 'fa_read'">خوانده شده فارسی</CButton>
              <CButton color="success" :class="filter === 'en_read' && 'selected'" @click="filter = 'en_read'">خوانده شده انگلیسی</CButton>
            </CButtonGroup>
          </CCardBody>
        </CCard>
      </CCol>
    </CRow>

    <CCard>
      <CCardHeader>
        <slot name="header">
          <i class="fal fa-bars"></i>
          <span>{{ list_title }}</span>
        </slot>
      </CCardHeader>
      <CCardBody>
        <el-table
            :data="filteredMessages"
            style="width: 100%"
            :row-class-name="readStatus">
          <el-table-column
              type="index"
              label="#">
          </el-table-column>

          <el-table-column
              prop="name"
              label="نام"
              width="">
          </el-table-column>

          <el-table-column
              prop="email"
              label="ایمیل"
              width="">
          </el-table-column>

          <!--          <el-table-column-->
          <!--              prop="phone"-->
          <!--              label="شماره تماس"-->
          <!--              width=""-->
          <!--              class-name="phoneNumber">-->
          <!--          </el-table-column>-->

          <el-table-column
              label="مشاهده"
              width="80"
              align="center">
            <template slot-scope="scope">
              <CButton color="success" variant="outline" :key="scope.row._id" :to="{name: 'admin-contact-us-messages-message',params: {message: scope.row._id}}">
                <i class="far fa-eye"></i>
              </CButton>
            </template>
          </el-table-column>

        </el-table>
      </CCardBody>
    </CCard>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: 'لیست پیغام های کاربران',
      list_title: 'لیست',
      messages: null,
      filter: 'all'
    }
  },
  computed: {
    filteredMessages() {
      const filter = this.filter
      if (filter === 'all') return this.messages
      else if (filter === 'fa_unread') return this.messages.filter(item => item.locale === 'fa' && !item.read)
      else if (filter === 'en_unread') return this.messages.filter(item => item.locale === 'en' && !item.read)
      else if (filter === 'fa_read') return this.messages.filter(item => item.locale === 'fa' && item.read)
      else if (filter === 'en_read') return this.messages.filter(item => item.locale === 'en' && item.read)
    }
  },
  methods: {
    readStatus({row, rowIndex}) {
      return row.read ? null : 'unread'
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  layout: 'admin',
  async asyncData({$axios, error}) {
    try {
      const messages = await $axios.get('/api/admin/contactUsMessages')
      return {
        messages: messages.data
      }
    } catch (e) {
      error({status: 500, message: 'there is a problem here'})
    }
  }
}
</script>

<style lang="scss">
.phoneNumber {
  div {
    direction: ltr;
    text-align: right;
  }
}

.unread {
  background: rgba(255, 173, 0, 0.12) !important;
}
</style>
