<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">
                {{ scope.row.fullName }}
            </template>
          </el-table-column>

          <el-table-column prop="phoneNumber" label=" شماره تماس " width="" align="center">
            <template slot-scope="scope">
                {{ scope.row.phoneNumber }}
            </template>
          </el-table-column>

          <el-table-column prop="email" label=" ایمیل " width="" align="center">
            <template slot-scope="scope">
                {{ scope.row.email || '-' }}
            </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-public-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?type=1`)
      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>
