<!--  -->
<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"
        ref="searchForm"
      >
        <el-form-item prop="search">
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="
              e => {
                e.target.blur();
                $refs.searchForm.save();
                search();
              }
            "
            placeholder="请输入编号/名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="parentSubjectId">
          <el-select
            v-model="searchForm.parentSubjectId"
            placeholder="选择声部分类"
            prop="parentSubjectId"
            clearable
            filterable
          >
            <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="createSubject" type="primary">添加</el-button>
      </auth>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
          row-key="id"
        >
          <el-table-column label="编号" prop="id"></el-table-column>
          <el-table-column label="声部名称" prop="name"></el-table-column>
          <el-table-column
            label="乐器名称"
            prop="instrumentName"
          ></el-table-column>
          <!-- <el-table-column label="是否启用" show-overflow-tooltip>
            <template #default="scope">
              <span>{{ scope.row.enableFlag ? "是" : "否" }}</span>
            </template>
          </el-table-column> -->
          <el-table-column label="乐器分类" prop="parentSubjectName">
          </el-table-column>

          <el-table-column label="更新时间" show-overflow-tooltip>
            <template #default="scope">
              <span>{{ scope.row.updateTime }}</span>
            </template>
          </el-table-column>

          <el-table-column label="操作" show-overflow-tooltip width="200px">
            <template #default="scope">
              <auth auths="subject/upset/reset">
                <el-button
                  size="small"
                  type="text"
                  @click="updateSubject(scope.row)"
                  >修改</el-button
                >
              </auth>
              <auth auths="subject/upset/del">
                <el-button
                  size="small"
                  type="text"
                  @click="removeSubjectCategory(scope.row)"
                  >删除</el-button
                >
              </auth>
              <!-- <el-button
                @click="onChangeStatus(scope.row)"
                size="small"
                type="text"
                >{{ !scope.row.enableFlag ? "启用" : "停用" }}</el-button
              > -->
              <auth auths="subject/upset/reset">
                <el-button
                  @click="updateInstrument(scope.row)"
                  size="small"
                  type="text"
                  >修改乐器</el-button
                >
              </auth>
            </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="instrumentVisible"
      width="770px"
    >
      <AddSubject
        v-if="instrumentVisible"
        :detail="activeRow"
        :subjectType="instrumentTitle"
        :fatherList="fatherList"
        @close="instrumentVisible = false"
        @getList="getList"
      />
    </el-dialog>

    <el-dialog
      :title="subjectTitle === 'add' ? '添加声部' : '修改声部'"
      :visible.sync="subjectVisible"
      width="560px"
    >
      <CreateSubject
        v-if="subjectVisible"
        :detail="activeRow"
        :subjectType="subjectTitle"
        :fatherList="fatherList"
        @close="subjectVisible = false"
        @getList="getList"
      />
    </el-dialog>

    <!-- <el-dialog
      title="修改乐器"
      :visible.sync="updateInstrumentVisiable"
      width="500px"
    >
      <UpdateInstrument
        v-if="updateInstrumentVisiable"
        :detail="activeRow"
        @close="updateInstrumentVisiable = false"
        @getList="getList"
      />
    </el-dialog> -->
  </div>
</template>

<script>
import pagination from "@/components/Pagination/index";
import AddSubject from "./components/add-subject";
import CreateSubject from "./components/create-subject";
import UpdateInstrument from "./components/update-instrument";
import {
  subjectListTree,
  subjectUpset,
  subSubjectList
} from "@/api/specialSetting";
export default {
  components: { pagination, AddSubject, UpdateInstrument, CreateSubject },
  data() {
    return {
      searchForm: {
        search: null,
        parentSubjectId: null
      },
      form: {
        name: null,
        id: null,
        img: null,
        parentSubjectId: null
      },
      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      subjectTitle: "add",
      subjectVisible: false,
      fatherList: [],
      instrumentVisible: false,
      instrumentTitle: "add",
      updateInstrumentVisiable: false,
      activeRow: {}
    };
  },
  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,
        page: this.rules.page,
        rows: this.rules.limit
      }).then(res => {
        this.tableList = res.data.rows;
        this.rules.total = res.data.total;
      });
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$nextTick(() => {
        this.search();
      });
    },
    // 修改乐器
    // onUpdateInstrument(row) {
    //   this.activeRow = row;
    //   this.updateInstrumentVisiable = true;
    // },
    onChangeInstrument(row) {
      const enableStr = row.enableFlag ? "停用" : "启用";
      this.$confirm(`是否${enableStr}${row.name}？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {});
    },
    /** 添加乐器 */
    createSubject() {
      this.subjectVisible = true;
      this.subjectTitle = "add";
      this.activeRow = {};
    },
    /** 修改乐器 */
    updateSubject(row) {
      this.subjectTitle = "update";
      this.activeRow = row;
      this.subjectVisible = true;
    },
    /** 修改乐器 */
    updateInstrument(row) {
      this.instrumentTitle = "update";
      this.activeRow = row;
      this.instrumentVisible = true;
    },
    removeSubjectCategory(row) {
      this.$confirm("是否确认删除声部", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        subjectUpset({
          delFlag: "YES",
          id: row.id
        }).then(res => {
          this.messageTips("删除", res);
          this.search();
        });
      });
    },
    // onChangeStatus(row) {
    //   const enableStr = row.enableFlag ? "停用" : "启用";
    //   this.$confirm(`是否${enableStr}${row.name}？`, "提示", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   }).then(async () => {

    //   });
    // },
    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({
    //         tenantId: 1,
    //         ...this.form
    //       }).then(res => {
    //         if (res.code == 200) {
    //           this.messageTips("修改", res);
    //           this.categoryVisible = false;
    //           this.getList();
    //         }
    //       });
    //     }
    //   });
    // },
  }
};
</script>
<style lang="scss" scoped></style>
