<template>
  <div class="m-core">
    <save-form
      :inline="true"
      class="searchForm"
      ref="searchForm"
      @submit="search"
      @reset="reset"
      :saveKey="'courseTimerSetting'"
      :model.sync="searchForm"
    >
      <el-form-item :rules="[]">
        <el-select
          class="multiple"
          v-model.trim="searchForm.organId"
          filterable
          clearable
          placeholder="请选择分部"
        >
          <el-option
            v-for="(item, index) in selects.branchs"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          class="multiple"
          v-model.trim="searchForm.courseScheduleType"
          filterable
          clearable
          placeholder="课程类型"
        >
          <el-option
            v-for="(item, index) in courseType"
            :key="index"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button native-type="submit" type="danger">搜索</el-button>
        <el-button native-type="reset" type="primary">重置</el-button>
      </el-form-item>
    </save-form>
    <div
      class="newBand"
      v-permission="'courseHomeworkTemplate/add'"
      @click="openJob('create')"
    >
      添加
    </div>
    <!-- 列表 -->
    <div class="tableWrap">
      <el-table
        :data="tableList"
        :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
      >
        <el-table-column align="center" prop="organ.name" label="分部名称">
        </el-table-column>
        <el-table-column
          align="center"
          prop="classGroupTypeName"
          label="课程形式"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.courseType | coursesType }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="课程课时" prop="duration">
          <!-- <template slot-scope="scope">
            {{ scope.row.subjectName ? scope.row.subjectName : "无" }}
          </template> -->
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              @click="resetCourseTime(scope.row)"
              v-permission="'courseHomeworkTemplate/update'"
              type="text"
              >修改</el-button
            >
            <!-- <el-button
              @click="delCourseTime(scope.row)"
              v-permission="'courseHomeworkTemplate/del'"
              type="text"
              >删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <pagination
        sync
        :total.sync="pageInfo.total"
        :page.sync="pageInfo.page"
        :limit.sync="pageInfo.limit"
        :page-sizes="pageInfo.page_size"
        @pagination="getList"
      />
    </div>
    <el-dialog
      :title="isAdd ? '新增课程时长' : '修改课程时长'"
      class="courseMask"
      width="500px"
      :visible.sync="courseVisible"
    >
      <courseTimeForm
        ref="courseTimeForm"
        :activeRow="activeRow"
        v-if="courseVisible"
        :organList="selects.branchs"
        :courseType="courseType"
        @close="close"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="courseVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import { musicCourseType } from "@/utils/searchArray";
import { getOrganizationCourseDurationSettings,delOrganizationCourseDurationSettings } from "@/api/specialSetting";
import courseTimeForm from "./modals/courseTimeForm";
const initSearch = {
  courseScheduleType: null,
  organId: null,
};
export default {
  components: { pagination, courseTimeForm },
  data() {
    return {
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      searchForm: { ...initSearch },
      courseType: musicCourseType,
      isAdd: true,
      courseVisible: false,
      activeRow: null,
    };
  },
  mounted() {
    this.$store.dispatch("setBranchs");
    this.getList();
  },
  methods: {
    async getList() {
      try {
        const res = await getOrganizationCourseDurationSettings({
          ...this.searchForm,
          page: this.pageInfo.page,
          rows: this.pageInfo.limit,
        });
        this.pageInfo.total = res.data.total;
        this.tableList = res.data.rows;
      } catch (e) {}
    },
    search() {
      this.pageInfo.page = 1;
      this.$refs.searchForm.save(this.searchForm);
      this.$refs.searchForm.save(this.pageInfo, "page");
      this.getList();
    },
    reset() {
      this.searchForm = { ...initSearch };
      this.search();
    },
    resetCourseTime(row) {
      this.isAdd = false;
      this.activeRow = row;
      this.courseVisible = true;
    },
    submitInfo() {
      const str = this.isAdd ? "create" : "update";
      this.$refs.courseTimeForm.submitInfo(str);
    },
    close() {
      this.courseVisible = false;
      this.getList();
    },
    openJob() {
      this.isAdd = true;
      this.activeRow = null;
      this.courseVisible = true;
    },
    async delCourseTime(row) {
      this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then( async() => {
        try{
         const res =   await delOrganizationCourseDurationSettings({ id:row.id})
         this.$message.success('删除成功')
         this.getList()
        }catch{}
      });
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
