<template>
  <div class="article-category-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          icon="el-icon-upload2"
          type="primary"
          @click="toggleRowExpansion(true)"
        >
          {{ __('全部展开') }}
        </el-button>
        <el-button
          icon="el-icon-download"
          type="primary"
          @click="toggleRowExpansion(false)"
        >
          {{ __('全部折叠') }}
        </el-button>
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(null, 0)"
        >
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-button icon="el-icon-refresh" type="primary" @click="fetchData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="dataTreeList"
      v-loading="listLoading"
      border
      :data="list"
      height="685"
      row-key="category_id"
    >
      <el-table-column align="center" :label="__('操作')" width="185">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
          <el-button type="text" @click="handleEdit(row, row.category_id)">
            {{ __('新增') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('分类编号')"
        prop="category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类名称')"
        prop="category_name"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('分类关键词')"
        prop="category_keywords"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类描述')"
        prop="category_desc"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('分类模板')"
        prop="category_template"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatCategoryBuildin"
        :label="__('系统内置')"
        prop="category_buildin"
        show-overflow-tooltip
      />
    </el-table>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

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

  import { doDelete, getList } from '@/api/article/category'
  import Edit from './components/ArticleCategoryEdit'

  export default {
    name: 'ArticleCategory',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        selectRows: '',
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatCategoryBuildin(row) {
        return row.category_buildin ? this.__('否') : this.__('是')
      },
      handleEdit(row, category_parent_id) {
        if (category_parent_id || category_parent_id === 0) {
          this.$refs['edit'].showEdit(null, category_parent_id)
        } else {
          this.$refs['edit'].showEdit(row)
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            category_id: row.category_id,
          })
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          await this.fetchData()
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList()
        this.list = data
        this.listLoading = false
      },

      toggleRowExpansion(isExpansion) {
        this.listLoading = true
        this.toggleRowExpansion_forAll(this.list, isExpansion)
        this.listLoading = false
      },
      toggleRowExpansion_forAll(data, isExpansion) {
        data.forEach((item) => {
          this.$refs.dataTreeList.toggleRowExpansion(item, isExpansion)
          if (item.children !== undefined && item.children != null) {
            this.toggleRowExpansion_forAll(item.children, isExpansion)
          }
        })
      },
    },
  }
</script>
