<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="handleAdd">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" row-key="id" :tree-props="{ children: 'childrenList', hasChildren: 'hasChildren' }"
      :data="list" default-expand-all :size="ELTableSize" :border="TableBorder" >
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="{$index}">
          {{ $getTableIndex(queryParams, $index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="属性名称" align="center" prop="name" />
      <el-table-column label="排序" align="center" prop="orderIndex" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button :size="ELTableSize" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button :size="ELTableSize" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <shopSkuDialog ref="shopSkuDialogRef" :topList="topList" @submit="getList" />
  </div>
</template>

<script>
import { skusTree, skusTops, skusInfo, skusDel } from "@/api/shopSku.js";
import shopSkuDialog from "./components/shopSkuDialog.vue";
export default {
  components: { shopSkuDialog },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 测试单表表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      topList: [],
    };
  },
  created() {
    this.getList();
    this.getTopList();
  },
  methods: {
    /** 查询测试单表列表 */
    getList() {
      this.loading = true;
      skusTree(this.queryParams).then((response) => {
        this.list = this.handleTree(response.data, "categoryId");
        // console.log(this.list);
        this.loading = false;
      });
    },
    // 获取顶级分类列表
    getTopList() {
      skusTops().then((res) => {
        if (res.code == 200) {
          this.topList = [{ name: "顶级", id: 0 }, ...res.data];
        }
      });
    },
    handleResourceTip(type) {
      for (let item of this.resourceModelList) {
        if (type == item.value) {
          return item.label || "";
        }
      }
    },
    // 表单重置
    reset() {
      this.form = {
        parentId: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getTopList();
      this.$refs.shopSkuDialogRef.open();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      const id = row.id;
      skusInfo(id)
        .then((response) => {
          this.loading = false;
          if (response.code == 200) {
            this.form = response.data;
            this.$refs.shopSkuDialogRef.open(response.data);
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$modal
        .confirm('是否确认删除属性名称为"' + row.name + '"的数据项？')
        .then(() => {
          this.loading = true;
          return skusDel(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</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>
