<template>
  <div class="department-management-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"
      :tree-props="{ children: 'sub' }"
    >
      <el-table-column align="center" :label="__('操作')" width="180">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="!row.category_id"
            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"
      />
      <el-table-column
        align="center"
        :label="__('商品分类名称')"
        prop="category_name"
      />
      <el-table-column
        align="center"
        :label="__('分类图片')"
        prop="category_image"
      >
        <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="type_name"
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('允许虚拟商品')"
        prop="category_virtual_enable"
      />
      <el-table-column
        align="center"
        :formatter="formatCommission"
        :label="__('平台佣金比率')"
        prop="category_commission_rate"
      />
      <el-table-column
        align="center"
        :label="__('是否显示')"
        prop="category_is_enable"
        width="100"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.category_is_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('排序')"
        prop="category_order"
        width="100"
      />
      <el-table-column
        v-if="isAdmin"
        align="center"
        label="店铺名称"
        prop="store_name"
        width="200"
      />
      <el-table-column
        align="center"
        :label="__('移动专题')"
        prop="page_id"
        width="140"
      />
    </el-table>
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, doEdit, getList } from '@/api/base/product/category'
  import Edit from './components/categoryManagementEdit'
  import { translateTitle as __ } from '@/utils/i18n'
  import { mapState } from "vuex";
  export default {
    name: 'CategoryManagement',
    // eslint-disable-next-line vue/no-unused-components
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
      }
    },
    computed:{
      ...mapState('user',['isAdmin'])
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      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({
            category_id: row.category_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList()
        this.list = data
        this.listLoading = false
      },
      formatEnable(row) {
        if (row.category_virtual_enable === 1) {
          return this.__('是')
        } else {
          return this.__('否')
        }
      },
      formatCommission(row) {
        return row.category_commission_rate + '%'
      },
      async sChange(row) {
        const param = {
          category_id: row.category_id,
          category_is_enable: row.category_is_enable,
        }
        const { msg, status } = await doEdit(param)

        if (status == 200) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
      },
      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.sub !== undefined && item.sub != null) {
            this.toggleRowExpansion_forAll(item.sub, isExpansion)
          }
        })
      },
    },
  }
</script>
