<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="栏目名称" prop="catgName">
        <el-input
          v-model="queryParams.catgName"
          placeholder="请输入栏目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      v-loading="loading" :data="categoryList" >
      <el-table-column label="栏目名称" align="left" prop="catgName" />
      <el-table-column label="栏目级别" align="center" prop="catgLevel">
        <template slot-scope="scope">
          {{scope.row.catgLevel}}级栏目
        </template>
      </el-table-column>
      <el-table-column label="栏目类型" align="center" prop="catgType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.category_type" :value="scope.row.catgType"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="修改时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span v-if="scope.row.updateTime == null">{{scope.row.createTime}}</span>
          <span v-else>{{scope.row.updateTime}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleChildAdd(scope.row)"
            v-hasPermi="['busi:category:edit']"
          >添加子栏目</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['busi:category:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['busi:category:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listCategory,delCategory} from "@/api/busi/category";

export default {
  name: "Category",
  components:{},
  dicts: ['category_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 网站栏目表格数据
      categoryList: [],
      // 查询参数
      queryParams: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询网站栏目列表 */
    getList() {
      this.loading = true;
      listCategory(this.queryParams).then(response => {
        console.log(response.data)
        this.categoryList = response.data;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path:'/category/categoryForm',query:{catgLevel:1}})
    },

    /**添加子栏目*/
    handleChildAdd(row){
      this.$router.push({path:'/category/categoryForm',query:{catgLevel:row.catgLevel+1,parentId:row.id}})
    },

    /** 修改按钮操作 */
    handleUpdate(row) {

    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除网站栏目"' + row.catgName + '"？').then(function() {
        return delCategory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

  }
};
</script>
