<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="位置">
        <el-select
          v-model="queryParams.location"
          placeholder="请选择位置"
          clearable
          @change="queryParams.pageNum = 1;getList()"
        >
          <el-option
            :label="item.dictLabel"
            :value="item.dictValue"
            v-for="(item, index) in bannerLocationList"
            :key="index"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="getList(true)"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:category:add']"
          >添加分类</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList()"
      ></right-toolbar>
    </el-row>
    <!-- 列表 -->
    <el-table ref="table" v-loading="loading" :data="list" row-key="categoryId" :tree-props="{children: 'goodsCategoryList', hasChildren: 'hasChildren'}">
      <el-table-column label="分类名称" prop="name" />
      <el-table-column label="分类图片">
        <div slot-scope="{ row }">
          <el-image
            style="width: 100px; height: 100px"
            :src="row.picUrl"
            :preview-src-list="[row.picUrl]"
          />
        </div>
      </el-table-column>
      <el-table-column label="排序" align="center">
        <template slot-scope="{ row }">
          <div>{{ row.sort }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="{ row }">
          <el-tag :type="row.isShow === 1 ? 'success' : 'info'">{{
            row.isShow === 1 ? "显示" : "不显示"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <div>
            <el-button
              v-hasPermi="['business:category:edit']"
              type="text"
              @click="edit(row)"
              >编辑</el-button
            >
            <el-button
              v-hasPermi="['business:category:remove']"
              type="text"
              class="del"
              @click="del(row)"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageParams.pageNum"
      :limit.sync="pageParams.pageSize"
      @pagination="getList"
    />

    <category-create
      v-if="createShow"
      :category-id="categoryId"
      :dialog-show="createShow"
      @close="close"
    />
  </div>
</template>
<script>
import { publicFileGetUrl } from "@/api/common";
import {
  goodsCategoryListTree,
  goodsCategoryRemove,
} from "@/api/business/category";
import CategoryCreate from "./components/CategoryCreate";
export default {
  name: "ListCate",
  components: {
    CategoryCreate,
  },
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {},
      pageParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      list: [],
      createShow: false,
      categoryId: '',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      goodsCategoryListTree(
        "pageNum=" +
          this.pageParams.pageNum +
          "&pageSize=" +
          this.pageParams.pageSize +
          "&",
        this.queryParams
      )
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            res.rows.forEach((item) => {
              item.picUrl = publicFileGetUrl + item.picUrl.split(",")[0];
              item.goodsCategoryList.forEach(item1 => {
                item1.picUrl = publicFileGetUrl + item1.picUrl.split(",")[0];
              })
            });

            this.total = res.total;
            this.list = res.rows;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    del(item) {
      this.$confirm(`确认删除分类 “${item.name}” 吗？`, "删除分类", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        goodsCategoryRemove({ categoryId: item.categoryId }).then((res) => {
          if (res.code === 0) {
            this.$message.success("操作已完成！");
            this.getList();
          }
        });
      });
    },

    edit(item){
      this.categoryId = item.categoryId
      this.createShow = true;
    },

    // 重置
    resetQuery() {
      this.pageParams.pageNum = 1
      this.getList();
    },

    handleAdd() {
      this.createShow = true;
    },

    // 关闭弹框
    close() {
      this.createShow = false;
      this.categoryId = ''
      this.getList();
    },
  },
};
</script>
<style>
</style>
