<template>
  <div class="article-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('编号')"
        prop="article_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('标题')"
        prop="article_title"
        show-overflow-tooltip
        width="200px"
      />
      <el-table-column
        align="center"
        :label="__('所属分类')"
        prop="category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatArticleType"
        :label="__('文章类型')"
        prop="article_type"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="article_sort"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('添加时间')"
        prop="article_add_time"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('文章图片')"
        prop="article_image"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.article_image"
            :preview-src-list="[scope.row.article_image]"
            :src="scope.row.article_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('文章标签')"
        show-overflow-tooltip
        width="130"
      >
        <template #default="{ row }">
          <span
            v-for="article_tag_row in row.article_tag_rows"
            :key="article_tag_row.tag_id"
          >
            {{ article_tag_row.tag_name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column :label="__('状态')" prop="article_status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.article_status"
            :active-value="1"
            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column :label="__('启用留言')" prop="article_reply_flag">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.article_reply_flag"

            :active-value="1"
            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column :label="__('是否默认')" prop="article_is_popular">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.article_is_popular"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doDelete, doEdit, getList } from '@/api/article/base'
  import Edit from './components/ArticleBaseEdit'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'ArticleBase',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatArticleType(row) {
        return row.article_type == 1 ? this.__('文章') : this.__('公告')
      },
      formatTime(row) {
        const article_add_time = row.article_add_time
        return article_add_time ? renderTime(article_add_time) : ''
      },
      handleEdit(row) {
        if (row.article_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            article_id: row.article_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      async sChange(row) {
        const param = {
          article_id: row.article_id,
          article_is_popular: row.article_is_popular,
          article_status: row.article_status,
          article_reply_flag: row.article_reply_flag,
        }
        const { msg, status } = await doEdit(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },
    },
  }
</script>
