<template>
  <div class="department-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <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, null)"
        >
          {{ __('添加') }}
        </el-button>
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      ref="dataTreeList"
      v-loading="listLoading"
      border
      :data="list"
      height="685"
      row-key="store_product_cat_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_product_cat_id"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
          <el-button
            type="text"
            @click="handleEdit(null, row.store_product_cat_id)"
          >
            {{ __('新增') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('分类编号')"
        prop="store_product_cat_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺分类名称')"
        prop="store_product_cat_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分类图片')"
        prop="store_product_cat_image"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.store_product_cat_image"
            :preview-src-list="[scope.row.store_product_cat_image]"
            :src="scope.row.store_product_cat_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('父编号')"
        prop="parent_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('显示购物车数量')"
        prop="store_product_cat_num_enable"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.store_product_cat_num_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="store_product_cat_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="store_product_cat_order"
        show-overflow-tooltip
      />
    </el-table>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doDelete, enable, Tree } from '@/api/store/product/category'
  import Edit from './components/StoreProductCategoryEdit'

  export default {
    name: 'StoreProductCategory',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatEnable(row) {
        return row.store_product_cat_enable
          ? this.__('启动')
          : this.__('不启动')
      },
      handleEdit(row, store_product_cat_id) {
        if (!row || store_product_cat_id) {
          this.$refs['edit'].showEdit(null, store_product_cat_id)
        } else {
          this.$refs['edit'].showEdit(row)
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            store_product_cat_id: row.store_product_cat_id,
          })
          this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
          await this.fetchData()
        })
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await Tree()
        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)
          }
        })
      },
      async sChange(row) {
        const param = {
          store_product_cat_id: row.store_product_cat_id,
          store_product_cat_num_enable: row.store_product_cat_num_enable,
          enable_name: 'store_product_cat_num_enable',
          enable_id: 'store_product_cat_id',
        }
        const { msg, status } = await enable(param)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        await this.fetchData()
      },
    },
  }
</script>
