<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      声部设置
    </h2>

    <div class="m-core">
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item>
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="search"
            placeholder="请输入编号、名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.parentSubjectId"
            placeholder="选择声部分类"
            prop="parentSubjectId"
          >
            <el-option
              v-for="item in fatherList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
      </save-form>
      <auth auths="subject/upset/insert" style="margin-bottom:20px;">
        <el-button @click="addCategory" type="primary">添加</el-button>
      </auth>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="id"
            label="分类编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="name"
            label="分类名称"
          ></el-table-column>
          <el-table-column align="center" prop="name" label="操作">
            <template slot-scope="scope">
              <div>
                <!-- v-permission="'subject/upset/del' -->
                <auth auths="subject/upset/insert">
                  <el-button
                    type="text"
                    @click="resetSubjectCategory(scope.row)"
                    >修改</el-button
                  >
                </auth>
                <auth auths="subject/upset/del">
                  <el-button
                    type="text"
                    @click="removeSubjectCategory(scope.row)"
                    >删除</el-button
                  >
                </auth>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="name" label="分类图片">
            <template slot-scope="scope">
              <el-image
                v-if="scope.row.img"
                style="width: 60px; height: 60px"
                fit="cover"
                :src="scope.row.img.split(',')[0]"
                :previewSrcList="scope.row.img.split(',')"
              >
              </el-image>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>
    <el-dialog
      title="修改分类"
      :visible.sync="categoryVisible"
      width="500px"
      v-if="categoryVisible"
    >
      <el-form :model="form" :inline="true" ref="form">
        <el-form-item
          prop="name"
          label="分类名称"
          :rules="{
            required: true,
            message: '请输入分类名称',
            trigger: 'change,blur',
          }"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="categoryVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitReset">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { subjectListTree, subjectUpset,subSubjectList } from "@/api/specialSetting";
export default {
  components: { pagination },
  data() {
    return {
      searchForm: {
        search: null,
        parentSubjectId: null,
      },
      form: {
        name: null,
        id: null,
      },
      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      categoryVisible: false,
      fatherList: [],
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部

    this.init();
  },
  methods: {
    init() {
      this.getList();
      this.getFatherList()
    },
    getFatherList() {
      subjectListTree({
        delFlag: "NO",
        rows: 9999,
        page: 1,
      }).then((res) => {
        if (res.code == 200) {
          this.fatherList = res.data.rows;
        }
      });
    },
    getList() {
      subSubjectList({...this.searchForm}).then(res=>{

      })
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$nextTick(()=>{
        this.search()
      })
    },
    resetSubjectCategory(row) {
      this.form.name = row.name;
      this.form.id = row.id;
      this.categoryVisible = true;
    },
    removeSubjectCategory(row) {
      this.$confirm("是否确认删除分类", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        subjectUpset({
          delFlag: "YES",
          id: row.id,
        }).then((res) => {
          this.messageTips("删除", res);
          this.search();
        });
      });
    },
    messageTips(title, res) {
      if (res.code == 200) {
        this.$message.success(title + "成功");
      } else {
        this.$message.error(res.msg);
      }
    },
    submitReset() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          subjectUpset({
            parentSubjectId: 0,
            tenantId: 1,
            name: this.form.name,
            id: this.form.id,
          }).then((res) => {
            if (res.code == 200) {
              this.messageTips("修改", res);
              this.categoryVisible = false;
              this.getList();
            }
          });
        }
      });
    },
    addCategory() {
      this.form.id = null;
      this.form.name = null;
      this.categoryVisible = true;
    },
  },
};
</script>
<style lang='scss' scoped>
</style>

