<template>
  <div>
    <el-dialog
      :title="dialogState === 1 ? 'به روزرسانی اطلاعات' : 'ساخت جایزه '"
      :visible.sync="centerDialogVisible"
      width="40%"
      center
    >
      <CRow>

          <CCol xl="12" class="my-3">
          <el-input v-model="award.desc" type="textarea" :rows="3" placeholder="توضیحات"> </el-input>
          <p v-if="validation.desc" class="errMsg">{{ validation.desc.msg }}</p>

        </CCol>

      </CRow>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">بستن</el-button>
        <el-button type="primary" @click="post( award)">تایید</el-button>
      </span>
    </el-dialog>

    <CustomSubHeader>
      <CBreadcrumb class="border-0 mb-0"> درخواست جوایز</CBreadcrumb>
    </CustomSubHeader>

    <CRow>
      <CCol xl="6">
        <CCard>
          <CCardHeader>
            <slot name="header">
              <i class="fas fa-filter"></i>
              <b>فیلتر ها</b>
            </slot>
          </CCardHeader>

          <CCardBody>
            <CRow>
              <CCol sm="12">
                <h4>جستجو در لیست:</h4>
                <el-input
                  v-model="filterText"
                  clearable
                  placeholder=""
                  style="display: inline-block; max-width: 500px; margin-top: 8px"
                ></el-input>
              </CCol>
            </CRow>
          </CCardBody>
        </CCard>
      </CCol>
    </CRow>

    <CCard>
      <CCardHeader>
        <slot name="header">
          <CIcon name="cil-grid" />
          درخواست جوایز 
        </slot>
      </CCardHeader>

      <CCardBody>
        <el-table :data="filteredItems" style="width: 100%">
          <el-table-column type="index" label="#"> </el-table-column>
          <el-table-column label="نوع" width="">
            <template slot-scope="scope">
              {{ (scope.row.awardId.title) }}
            </template>
          </el-table-column>
          <el-table-column label="توضیحات" width="">
            <template slot-scope="scope">
              {{ (scope.row.awardId.desc) }}
            </template>
          </el-table-column>
          <el-table-column label="امتیاز مورد نیاز" width="">
            <template slot-scope="scope">
              {{ (scope.row.awardId.score) }}
            </template>
          </el-table-column>
          <el-table-column label="مهلت استفاده" width="">
            <template slot-scope="scope">
              {{ $jDate(scope.row.awardId.expireDate) }}
            </template>
          </el-table-column>
          <el-table-column label="نام و نام خانودگی متقاضی" width="">
            <template slot-scope="scope">
              {{ (scope.row.userId.first_name)+" "+(scope.row.userId.last_name) }}
            </template>
          </el-table-column>
          <el-table-column label="توضیحات" width="">
            <template slot-scope="scope">
              {{ (scope.row.userId.national_code) }}
            </template>
          </el-table-column>
          <el-table-column label="توضیحات" width="">
            <template slot-scope="scope">
              {{ (scope.row.userId.mobile_number) }}
            </template>
          </el-table-column>


          <el-table-column label="تغییرات" width="110" align="center">
            <template slot-scope="scope">
              <CButton :key="scope.row._id" color="info" variant="outline" @click="showDialog(scope.row._id, scope.row.desc)">
                <i class="fa fa-pencil"></i>
              </CButton>
            </template>
          </el-table-column>
        </el-table>
      </CCardBody>
    </CCard>
  </div>
</template>

<script>
export default {
  name: 'AdminGpsAwardList',
  layout: 'admin',
  async asyncData({ $axios, error, query }) {
    try {
      const { data } = await $axios.get('/api/admin/gps/award-request')

      return {
        awards: data
      }
    } catch (error) {
      console.log(error)
    }
  },
  data() {
    return {
      awards: [],
      centerDialogVisible: false,
      award: {
        desc:'ی',
        status:1
      },
      filterText: '',
      dialogState: 0,
      validation: {}
    }
  },

  computed: {
    config() {
      return this.$config
    },
    filteredItems() {
      const filterText = this.filterText
      if (!this.filterText.length) return this.awards
      else {
        return this.awards.filter(item => {
          return item.score.includes(filterText) || item.title.includes(filterText) || item.desc.includes(filterText)
        })
      }
    }
  },
  methods: {
    showDialog( id,desc) {

        this.dialogState = 1
        this.award = {
          _id:id,
          desc,
          status:1
        }
        this.centerDialogVisible = true

    },

    post( data) {

        this.$axios.put(`/api/admin/gps/award-request/${data._id}`,data).then(res => {
          this.$message({
            type: 'success',
            message: res.data.msg
          })

          this.centerDialogVisible = false
          this.$nuxt.refresh()
        })
        .catch(err => {
          if (err.response.data.status === 401) {
            return this.$message({
              type: 'warning',
              message: 'لطفا دوباره وارد حساب خود شوید'
            })
          }
          if (err.response.status === 422) this.validation = err.response.data.validation
          else {
            console.log(err)
            return this.$message({
              type: 'warning',
              message:  err.response.data.msg
            })
          }
        })

    }
  }
}
</script>
<style>

</style>
