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

    <CCard>
      <CCardBody>
        <div>
          <h3>میزان رضایت کلی مشتریان در تمام نظرات ثبت شده:</h3>

          <ul class="mt-4">
            <li v-for="item in surveyOptions" :key="item.fieldName + '_Option'">
              <p>
                <span>{{ item.title }}</span>
                <b>%{{ getPercentage(item.fieldName) }}</b>
              </p>
            </li>
          </ul>
        </div>
      </CCardBody>
    </CCard>

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

          <el-table-column prop="title" label="کاربر" width="">
            <template slot-scope="scope">
              <nuxt-link :to="{ name: 'admin-customers-customer', params: { customer: scope.row.user._id } }">
                {{ scope.row.user.first_name + ' ' + scope.row.user.last_name }}
              </nuxt-link>
            </template>
          </el-table-column>

          <el-table-column prop="code" label="کد پذیرش دایم" width="" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.code || '-' }}</span>
            </template>
          </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="70" align="center">
            <template slot-scope="scope">
              <CButton
                :key="scope.row._id"
                color="success"
                variant="outline"
                :to="{ name: 'admin-surveys-private-survey', params: { survey: scope.row._id } }"
              >
                <i class="fal fa-eye"></i>
              </CButton>
            </template>
          </el-table-column>
        </el-table>
      </CCardBody>
    </CCard>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  name: 'AdminSurveysList',
  layout: 'admin',
  async asyncData({ $axios, error }) {
    try {
      const surveys = await $axios.$get(`/api/admin/surveys`)
      return {
        surveys
      }
    } catch (err) {
      error({ status: 404, message: 'There is a problem here' })
    }
  },
  data() {
    return {
      title: 'لیست نظرسنجی ها',
      list_title: 'لیست',
      surveys: null,
      search: ''
    }
  },
  head() {
    return {
      title: this.title
    }
  },
  computed: {
    ...mapGetters({
      surveyOptions: 'front/surveyOptions',
      surveyOptionsValues: 'front/surveyOptionsValues'
    })
  },
  methods: {
    getPercentage(fieldName) {
      let totalData = 0
      let userChoice = 0
      this.surveys.forEach(item => {
        totalData += 5
        userChoice += item[fieldName]
      })
      return Math.round((userChoice * 100) / totalData)
    }
  }
}
</script>
