<template>
  <div class="app-container">
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      fit
      highlight-current-row
    >
      <el-table-column align="center" :label="$t('article.title')" prop="TITLE">
        <template slot-scope="{row}">
          <span @click="handleArticle(row.ID)">
						<el-link type="primary" href="#" target="_self" >{{ row.TITLE }}</el-link>
					</span>
        </template>
      </el-table-column>
			<el-table-column align="center" :label="$t('article.category')" prop="CATE_NAME" v-if="['windows', 'linux'].includes(system)">
				<template slot-scope="{row}">
          {{ row.CATE_NAME }}
				</template>
			</el-table-column>
      <el-table-column align="center" :label="$t('article.createTime')" prop="CREATED_AT">
        <template slot-scope="{row}">
          <span>{{ tool.convertToNigeriaDayTime(row.CREATED_AT) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />

		<el-dialog title="" :visible.sync="dialog" :width="screenWidth" v-loading="loading" center style="margin-top: -80px;">
			<div class="white-box" style="margin-top: -20px; padding: 0;">
				<div class="white-box-title" style="text-align: center">
					<h1>{{ article.TITLE }}</h1>
					<span>{{ tool.convertToNigeriaTime(article.CREATED_AT) }}</span>
				</div>
				<div v-html="article.CONTENT" class="white-box-content">
				</div>
			</div>
		</el-dialog>
  </div>
</template>

<script>
import {fetchArticleList, fetchArticleOne} from '@/api/article'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import {formatAmount, getOperatingSystem, getScreenWidth} from "@/utils"
import tool from "@/utils/tool";

export default {
  name: 'ArticleList',
  computed: {
    tool() {
      return tool
    }
  },
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
			category: {},
			listQuery: {
				page: 1,
				pageSize: 20
			},
			loading: false,
			dialog: false,
			article: {
				ID: '',
				CID: '',
				TITLE: '',
				CONTENT: '',
				CREATED_AT: '',
			},

			screenWidth: getScreenWidth() > 600 ? '70%' : getScreenWidth() + 'px',
			labelPosition: getScreenWidth() > 600 ? 'right' : 'top',
			system: getOperatingSystem(),
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
			fetchArticleList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = +response.data.totalCount
				this.category = response.data.allCategory

				this.list.forEach(item => {
					item.CATE_NAME = this.category[item.CID].CATE_NAME ?? ''
				})

				setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },
		handleArticle(id) {
			this.loading = true
			this.listLoading = true
			fetchArticleOne(id).then(response => {
				this.article = response.data
				this.dialog = true
				this.listLoading = false
				setTimeout(() => {
					this.loading = false
				}, 0.5 * 1000)
			})
		},
  }
}
</script>

<style scoped>
.white-box-title span{color: #999}
.white-box-content{font-size: 16px;padding: 15px 0;line-height: 2;}
</style>
