<template>
  <div class="story-category-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(null, null)"
        >
          {{ __('添加') }}
        </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"
      row-key="story_category_id"
    >
      <el-table-column align="center" fixed :label="__('操作')" width="180">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="!row.story_category_id"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
          <el-button
            type="text"
            @click="handleEdit(null, row.story_category_id)"
          >
            {{ __('新增') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('版块编号')"
        prop="story_category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('版块名称')"
        prop="story_category_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属父类')"
        prop="story_category_parent_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('评论图片')"
        prop="story_category_logo"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.story_category_logo"
            :preview-src-list="[scope.row.story_category_logo]"
            :src="scope.row.story_category_logo"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('版块背景')"
        prop="story_category_bg"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('关键词')"
        prop="story_category_keywords"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('版块描述')"
        prop="story_category_desc"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('版块内容数量')"
        prop="story_category_count"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('版块模板')"
        prop="story_category_template"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('版块别名')"
        prop="story_category_alias"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('版块排序')"
        prop="story_category_order"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatStoryCategoryCbuildin"
        :label="__('系统内置')"
        prop="story_category_buildin"
        show-overflow-tooltip
      />
    </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 { getList, doDelete } from '@/api/story/category'
  import Edit from './components/StoryCategoryEdit'
  import VabQueryFormLeftPanel from "@/vab/components/VabQueryForm/components/VabQueryFormLeftPanel.vue";
  import VabQueryForm from "@/vab/components/VabQueryForm/index.vue";
  import VabQueryFormRightPanel from "@/vab/components/VabQueryForm/components/VabQueryFormRightPanel.vue";

  export default {
    name: 'StoryCategory',
    components: {VabQueryFormRightPanel, VabQueryForm, VabQueryFormLeftPanel, Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      formatStoryCategoryCbuildin(row) {
        return row.story_category_buildin === 1
          ? this.__('内置')
          : this.__('非内置')
      },

      handleEdit(row, story_category_id) {
        if (!row || story_category_id) {
          this.$refs['edit'].showEdit(null, story_category_id)
        } else {
          this.$refs['edit'].showEdit(row)
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            story_category_id: row.story_category_id,
          })
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          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.list = data
        this.listLoading = false
      },
    },
  }
</script>
