<template>
  <div class="base-store-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="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      ref="dataTreeList"
      v-loading="listLoading"
      border
      :data="list"
      height="685"
      row-key="store_category_id"
    >
      <el-table-column align="center" :label="__('操作')" width="180">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="!row.store_category_id"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
          <el-button
            type="text"
            @click="handleEdit(row, row.store_category_id)"
          >
            {{ __('新增') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('分类编号')"
        prop="store_category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('上级编号')"
        prop="store_category_parent_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺分类名称')"
        prop="store_category_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="分账比例"
        prop="split_ratio"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('保证金数额')"
        prop="store_category_deposit"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类图片')"
        prop="category_image"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img
              v-if="scope.row.category_image"
              alt=""
              :src="scope.row.category_image"
              style="width: 150px; height: 150px"
            />
            <img
              v-if="scope.row.category_image"
              slot="reference"
              :src="scope.row.category_image"
              style="width: 30px; height: 30px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="store_category_order"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="category_is_enable"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-tag
            :type="
              { '0': 'info', '1': 'success', '2': 'info' }[
                row.category_is_enable
              ]
            "
          >
            {{
              { '0': '禁用', '1': '启用', '2': '启用' }[row.category_is_enable]
            }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
import { translateTitle as __ } from '@/utils/i18n'
import { doDelete, getTree } from '@/api/base/store/category'
import Edit from './components/BaseStoreCategoryEdit'

export default {
  name: 'BaseStoreCategory',
  components: { Edit },
  data() {
    return {
      list: [],
      listLoading: true,
      layout: 'total, sizes, prev, pager, next, jumper',
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    formatEnable(row) {
      return row.category_is_enable ? this.__('显示') : this.__('不显示')
    },
    __,
    handleEdit(row, category_parent_id) {
      if (category_parent_id === 0 || category_parent_id) {
        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({
          store_category_id: row.store_category_id,
        })
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }

        await this.fetchData()
      })
    },
    queryData() {
      this.fetchData()
    },
    async fetchData() {
      this.listLoading = true
      const { data } = await getTree()
      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>
