<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="mini" @click="handleAdd(null)">新增根分类</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="categoryList" row-key="categoryId"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
              border default-expand-all>
      <el-table-column label="分类名称" prop="categoryName" min-width="200" />
      <el-table-column label="排序" prop="sort" width="80" align="center" />
      <el-table-column label="状态" width="80" align="center">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'" size="mini">
            {{ scope.row.status === '0' ? '启用' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" class-name="col-ops" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)">新增子类</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" style="color:#f56c6c" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="dialogTitle" :visible.sync="dialogOpen" width="480px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item v-if="form.parentId !== null && form.parentId !== undefined" label="父级分类">
          <span style="color:#606266">{{ parentName || '根节点' }}</span>
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序">
              <el-input-number v-model="form.sort" :min="0" :max="999" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio label="0">启用</el-radio>
                <el-radio label="1">禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogOpen = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryList, addCategory, updateCategory, delCategory } from "@/api/bid/category";
export default {
  name: "Category",
  data() {
    return {
      loading: false,
      categoryList: [],
      dialogOpen: false,
      dialogTitle: "",
      parentName: "",
      form: {},
      rules: {
        categoryName: [{ required: true, message: "分类名称不能为空", trigger: "blur" }]
      }
    };
  },
  created() { this.getList(); },
  methods: {
    getList() {
      this.loading = true;
      getCategoryList().then(res => {
        this.categoryList = res.data || [];
        this.loading = false;
      }).catch(() => { this.loading = false; });
    },
    handleAdd(parent) {
      this.form = { categoryName: "", parentId: parent ? parent.categoryId : 0, sort: 0, status: "0" };
      this.parentName = parent ? parent.categoryName : "";
      this.dialogTitle = parent ? "新增子分类（" + parent.categoryName + "）" : "新增根分类";
      this.dialogOpen = true;
    },
    handleUpdate(row) {
      this.form = { categoryId: row.categoryId, categoryName: row.categoryName, sort: row.sort, status: row.status };
      this.dialogTitle = "修改分类";
      this.dialogOpen = true;
    },
    handleDelete(row) {
      this.$modal.confirm("确认删除分类【" + row.categoryName + "】？子分类也将被删除！").then(() => {
        return delCategory(row.categoryId);
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      });
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) return;
        const action = this.form.categoryId ? updateCategory : addCategory;
        action(this.form).then(() => {
          this.$modal.msgSuccess("保存成功");
          this.dialogOpen = false;
          this.getList();
        });
      });
    }
  }
};
</script>
