<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" :size="ELsize"
          @click="$refs.CalssDetailsRef.openHandle()">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :border="TableBorder" :size="ELTableSize" v-loading="loading" row-key="categoryId"
      :tree-props="{ children: 'childrenList', hasChildren: 'hasChildren' }" :data="list" default-expand-all>
      <!-- <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="scope">
          <div>
            {{ scope.$index + 1 }}
          </div>
        </template>
</el-table-column> -->
      <el-table-column label="分类名称" align="center" prop="categoryName" />
      <el-table-column label="分类图标" align="center" prop="image">
        <template slot-scope="{row}">
          <template v-if="row.categoryIcon">
            <el-image style="width: 50px; height: 50px" :src="row.categoryIcon"
              :preview-src-list="row.categoryIcon ? [row.categoryIcon] : []"></el-image>
          </template>
          <template v-else>
            {{ row.categoryIcon | TableItemEmpty }}
          </template>
        </template>
      </el-table-column>
      <el-table-column label="分类描述" align="center" prop="categoryDescription" show-overflow-tooltip />
      <el-table-column label="排序" align="center" prop="sortOrder" />
      <el-table-column label="上架状态" align="center" prop="shelvedStatus">
        <template slot-scope="{row}">
          <el-tag type="success" v-if="row.shelvedStatus">上架</el-tag>
          <el-tag type="danger" v-if="!row.shelvedStatus">下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="190">
        <template slot-scope="scope">
          <el-button :size="ELTableSize" type="text" icon="el-icon-edit"
            @click="$refs.CalssDetailsRef.openHandle(scope.row)">修改</el-button>
          <el-button :size="ELTableSize" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>
          <el-button :size="ELTableSize" type="text" @click="setStatus(scope.row)">{{ scope.row.shelvedStatus ? '下架' :
            '上架' }}</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <CalssDetails ref="CalssDetailsRef" @success="getList" />

  </div>
</template>

<script>
import { merchantCategoryAdd, merchantCategoryEdit, merchantCategoryTree, merchantCategoryTops, merchantCategoryInfo, merchantCategoryShelved, merchantCategoryDel } from "@/api/platformClass.js";
import { getToken } from "@/utils/auth";
import CalssDetails from "@/views/merchandiseAudit/platformClass/CalssDetails.vue";
export default {
  components: { CalssDetails },
  data() {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "添加广告",
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/demo/demo/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        categoryName: undefined,
        shelvedStatus: undefined,
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      this.queryParams["createTimeStart"] = undefined;
      this.queryParams["createTimeEnd"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["createTimeStart"] = this.daterangeCreateTime[0] + ' 00:00:00';
        this.queryParams["createTimeEnd"] = this.daterangeCreateTime[1] + ' 23:59:59';
      }
      merchantCategoryTree(this.queryParams).then(response => {
        // response.data.map(v => {
        //   // 确保每个节点都有 childrenList 属性，即使为空数组
        //   if (!v.childrenList || v.childrenList.length === 0) {
        //     v.childrenList = null;
        //   }
        //   // 如果有子节点，则设置 hasChildren 为 true
        //   if (v.childrenList && v.childrenList.length > 0) {
        //     v.hasChildren = true;
        //   }
        // })
        this.list = this.handleTree(response.data, "categoryId")
        console.log(this.list)
        this.loading = false;
      });
    },



    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },



    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.categoryId;
      this.$modal.confirm('是否确认删除分类名称为"' + row.categoryName + '"的数据项？').then(() => {
        this.loading = true;
        return merchantCategoryDel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 上下架按钮操作 */
    setStatus(row) {
      const ids = row.categoryId;
      let statusText = row.shelvedStatus ? '下架' : '上架';
      this.$modal.confirm('是否确认' + statusText + '分类名称为"' + row.categoryName + '"的数据项？').then(() => {
        this.loading = true;
        return merchantCategoryShelved(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess(statusText + "成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('demo/demo/export', {
        ...this.queryParams
      }, `demo_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
  }
};
</script>
<style lang="scss">
.el-drawer__body {
  padding: 0 20px !important;
}
</style>
<style lang="scss" scoped>
.flex {
  display: flex;
}

.demo-drawer__footer {
  margin-left: 120px;
}

::v-deep .el-input.is-disabled .el-input__inner {
  background-color: #fff;
  color: rgb(96, 98, 102);
}
</style>
