
<template>
  <div class="tabManage app-container">
    <div class="search">
      <el-button class="add-btn" type="primary" size="small" @click="handleAdd"
        >添加</el-button
      >
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
     <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        align="center"
        label="社区标签名称"
        prop="communityName"
        show-overflow-tooltip
      >
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button type="text" size="small" @click="handleDel(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog :title="title" width="550px" :visible.sync="dialogFormVisible">
    <el-form label-width="130px" :model="setQuery"
      ref="setQuery"
      :rules="rules">
        <el-form-item label="标签名称"  prop="communityName">
            <el-input
            v-model="setQuery.communityName"
            class="item-width-300"
            ></el-input>
        </el-form-item>
        <el-form-item label="研学栏目"  prop="columnId">
          <el-select
          v-model="setQuery.columnId"
          clearable
          class="item-width-300"
          placeholder="请选择栏目"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="排序"  prop="sort">
          <el-input
          v-model="setQuery.sort"
          class="item-width-300"
          ></el-input>
        </el-form-item>
    </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" :loading="btnLoading" @click="save">确 定</el-button>
  </div>
</el-dialog>


  </div>
</template>
  
  <script>
import { debounce } from "@/utils/index";
import { getTabList,delTab,addOrUpdateTab,getAllColumn } from "@/api/study";
export default {
  name: "tabManage",
  data() {
    return {
      title:'',
      tableData: [],
      loading: false,
      btnLoading: false,
      dialogFormVisible: false,
      query: {
        currentPage: 1,
        pageSize: 10,
      },
      total: 0,
      setQuery:{
        communityName:'',
        columnId:'',
        sort:''
      },
      options:[],
      rules:{
         communityName: [{ required: true, message: "请输入社区标签", trigger: "blur" }],
          columnId: [{ required: true, message: "请输入研学栏目", trigger: "blur" }],
          sort: [{ required: true, message: "请输入排序", trigger: "blur" },
            {
              validator: (rule, value, callback) => {
                console.log(value)
                if(isNaN(value)){
                  callback(new Error("请输入数字"));
                }
                if (value < 1) {
                  callback(new Error("排序不能小于1"));
                } else {
                  callback();
                }
              },
              trigger: "blur"
            }
          ]
      }
    };
  },
  watch: {
    query: {
      handler: debounce(function (val) {
          this.getTabList()

      }),
      deep: true,
    },
  },
  methods: {
    // 获取栏目
    getAllColumn() {
      getAllColumn().then((res) => {
        if (res.state == "Success") {
          res.content.map((item) => {
            this.options.push({
              value: `${item.id}`,
              label: item.columnName,
            });
          });
        }
      });
    },
    handleEdit(row){
        this.title = '编辑标签'
      this.dialogFormVisible = true
      this.setQuery = JSON.parse(JSON.stringify(row)) 
    },
    handleAdd() {
        this.title = '添加标签'
      this.dialogFormVisible = true
      this.setQuery = {
        communityName:''
      }
    },
    // 删除
     handleDel(id) {
       this.$confirm(`此操作将删除该数据, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delTab({id}).then(res=>{
            if(res.state == 'Success'){
               this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success'
              });
              this.getTabList()
            }
          })
        })
    },
    handleSizeChange(val) {
      this.query.currentPage = 1;
      this.query.pageSize = val;
      this.getTabList();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getTabList();
    },

    search() {
      this.query.currentPage = 1;
      this.getTabList();
    },
    getTabList() {
      this.loading = true
      getTabList(this.query).then((res) => {
        this.loading = false
        if (res.state == "Success") {
          this.tableData = res.content.records;
          this.total = res.content.total
        }
      });
    },
    save(){
        this.$refs.setQuery.validate((v) => {
        if (v) {
          this.btnLoading = true
          addOrUpdateTab(this.setQuery).then(res=>{
            this.btnLoading = false
            if(res.state == 'Success'){
                this.dialogFormVisible = false
              this.$notify({
                  title: '成功',
                  message: '操作成功',
                  type: 'success'
                });
                this.query.currentPage = 1
                this.getTabList();

            }
          })
              
        }
      });
    }
  },
  created() {
    this.getTabList();
    this.getAllColumn();
  },
};
</script>
  
<style lang="scss" >
.tabManage {
}
</style>
  