<template>
  <div>
    <save-form
      :inline="true"
      class="searchForm"
      ref="searchForm"
      :model="searchForm"
      @submit="onSearch"
      @reset="onReSet"
      save-key="studentDetail-studentLebao"
    >
      <el-form-item prop="specification">
        <el-input
          placeholder="具体型号"
          clearable
          @keyup.enter.native="onSearch"
          v-model.trim="searchForm.specification"
        ></el-input>
      </el-form-item>
      <el-form-item prop="goodsCategoryId">
        <el-select
          v-model.trim="searchForm.goodsCategoryId"
          clearable
          placeholder="乐器分类"
        >
          <el-option
            v-for="(item, index) in categoryList"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="status">
        <el-select
          v-model.trim="searchForm.status"
          filterable
          clearable
          placeholder="是否乐保"
        >
          <el-option value="0" label="否"></el-option>
          <el-option value="1" label="是"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button native-type="submit" type="danger">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" native-type="reset">重置</el-button>
      </el-form-item>
    </save-form>
    <el-button @click="operationStudent('create')" type="primary" v-permission="'studentInstrument/add'"
      >新增乐器</el-button
    >
    <div class="tableWrap">
      <el-table
        :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
        :data="tableList"
      >
        <el-table-column label="乐器名称" align="center">
          <template slot-scope="scope">
            <copy-text>{{ scope.row.goodsName }}</copy-text>
          </template>
        </el-table-column>
        <el-table-column label="具体型号" align="center">
          <template slot-scope="scope">
            <copy-text>{{ scope.row.specification }}</copy-text>
          </template>
        </el-table-column>
        <el-table-column label="乐器分类" align="center">
          <template slot-scope="scope">
            <copy-text>{{ scope.row.goodsCategoryName }}</copy-text>
          </template>
        </el-table-column>
        <el-table-column label="是否乐保" align="center" prop="teacherName">
           <template slot-scope="scope">
            {{ scope.row.status?'是':'否' }}
          </template>
        </el-table-column>
        <el-table-column label="乐保有效期" align="center" width="350px" prop="studentNum">
          <template slot-scope="scope">
            <div v-if="scope.row.startTime && scope.row.endTime">
              {{ scope.row.startTime}}~{{scope.row.endTime}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <auth :auths="['studentInstrument/update']">
              <el-button
                type="text"
                @click="operationStudent('update', scope.row)"
              >修改</el-button>
            </auth>
            <auth :auths="['studentInstrument/del']">
              <el-button
                type="text"
                @click="removeMusic(scope.row)"
              >删除</el-button>
            </auth>

          </template>
        </el-table-column>
      </el-table>
      <pagination
        save-key="studentDetail-studentLebao"
        sync
        :total.sync="pageInfo.total"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.limit"
        :page-sizes="pageInfo.page_size"
        @pagination="getList"
      />
    </div>
    <el-dialog
      :title="operationStatus == 'create' ? '新增乐器' : '修改乐器'"
      width="600px"
      :visible.sync="addMusicVisible"
      v-if="addMusicVisible"
    >
      <addMusic :categoryList='categoryList' :operationData="operationData" :operationStatus="operationStatus" ref='addMusic' @getList='getList' @close='addMusicVisible = false'/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addMusicVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMusicSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import addMusic from "../modals/addMusic";
import { getInstrument, studentInstrumentDel } from "@/api/buildTeam";
import { categoryListTree } from "@/api/businessManager";
export default {
  components: { pagination, addMusic },
  data() {
    return {
      soundLists: [],
      tableList: [],
      searchForm: {
        specification:'',
        goodsCategoryId: "",
        studentId: this.$route.query.userId
      },
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      operationStatus: 'create',
      operationData: null,
      addMusicVisible: false,
      categoryList: [],
    };
  },
  mounted() {
    this.getCategory();
    this.getList()
  },
  methods: {
    onSearch() {
      this.pageInfo.page = 1;
      this.getList()
    },
    onReSet() {
      this.$refs['searchForm'].resetFields()
      this.getList()
    },
   async getList() {
     try{
       const res = await getInstrument({...this.searchForm,rows:this.pageInfo.limit,page:this.pageInfo.page})
        this.tableList = res.data.rows
        this.pageInfo.total = res.data.total
     }catch(e){
       console.log(e)
     }

    },
    addMusicSubmit() {
      this.$refs.addMusic.addMusicSubmit()
    },
    operationStudent(type, row) {
      if(type == 'update') {
        this.operationData = row
      } else if(type == 'create') {
        this.operationData = null
      }
      this.operationStatus = type
      this.addMusicVisible = true
    },
    removeMusic(row) {
      // 删除乐器
      this.$confirm(`您确定删除吗？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async() => {
        try {
          let res = await studentInstrumentDel({ id: row.id, studentId: this.searchForm.studentId})
          this.$message.success("修改成功");
          this.getList()
        } catch { }
      });
    },
    getCategory() {
      let params = {
        delFlag: 0,
        rows: 9999,
      };
      categoryListTree(params).then((res) => {
        let result = res.data;
        if (res.code == 200) {
          let tempArray = [];
          result.rows.forEach((row) => {
            tempArray.push({
              label: row.name,
              value: row.id,
            });
          });
          this.categoryList = tempArray;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.tableWrap {
  margin-top: 20px;
}
</style>
