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

    <CCard>
      <CCardHeader>
        <slot name="header">
          <i class="fal fa-bars"></i>
          <span>{{ list_title }}</span>
        </slot>
      </CCardHeader>
      <CCardBody>
        <el-table
            :data="messages.docs"
            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>
    <CCard class="col" v-if="messages.totalDocs>20">
      <CRow alignHorizontal="center" style="padding : 10px">
        <el-pagination :hide-on-single-page="true" layout="prev, pager, next" :page-size="messages.limit"
                       @current-change="handleCurrentChange" :current-page.sync="messages.page" :total="messages.totalDocs">
        </el-pagination>
      </CRow>
    </CCard>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: 'لیست اسلایدهای صفحه اصلی',
      list_title: 'لیست',
      messages: null,
    }
  },
  computed: {
    pageQuery() {
      return this.$route.query.page;
    }
  },
  watch: {
    pageQuery(newPage, oldPage) {
      this.$nuxt.refresh();
    }
  },
  methods: {
    handleCurrentChange(page) {
      // if (!_.isEmpty(this.search)) {
      //   this.handleSearch(page);
      // } else {
      this.$router.push({
        name: "admin-contact-us-messages",
        query: {
          page: page
        }
      });
      // }
    },
    readStatus({row, rowIndex}) {
      return row.read ? null : 'unread'
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  layout: 'admin',
  async asyncData({$axios, query, error}) {
    try {
      const messages = await $axios.get(`/api/admin/contactUsMessages?page=${query.page || 1}`)
      return {
        messages: messages.data
      }
    } catch (e) {
      if (e?.response?.status === 401) {
        error({
          status: 401,
          message: 'شما اجازه دسترسی به این صفحه را ندارید لطفا دوباره وارد شوید'
        })
      } else {
        error({
          status: 500,
          message: "مشکلی در گرفتن اطلاعات بوجود آمده است",
        });
      }
    }
  }
}
</script>

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

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