<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>网管课列表
    </h2>

    <div class="m-core">
      <el-form :inline="true" class="searchForm" v-model.trim="searchForm">
        <!-- 状态 指导老师 活动方案-->
        <el-form-item>
          <el-input
            v-model.trim="searchForm.search"
            @keyup.enter.native="search"
            placeholder="课程组名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            class="multiple"
            v-model.trim="searchForm.organIdList"
            filterable
            clearable
            placeholder="请选择分部"
          >
            <el-option
              v-for="(item,index) in organList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- statusList -->
        <el-form-item prop="status">
          <el-select
            class="multiple"
            v-model.trim="searchForm.hasEducationalTeacherId"
            filterable
            clearable
            placeholder="是否有教务老师"
          >
            <el-option label="是" value="true"></el-option>
            <el-option  label="否" value="false"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-select v-model.trim="searchForm.teacherId" clearable filterable placeholder="指导老师">
            <el-option
              v-for="(item,index) in teacherList"
              :key="index"
              :value="item.id"
              :label="item.realName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search" type="danger">搜索</el-button>
          <el-button @click="onReSet" type="primary">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- tab -->
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{background:'#EDEEF0',color:'#444'}"
          :data="tableData"
        >
          <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="organName" label="所属分部"></el-table-column>
          <!-- educationalTeacherId -->
          <el-table-column align="center" prop="educationalTeacherName" label="教务老师"></el-table-column>
          <el-table-column align="center" prop="teacherName" label="指导老师"></el-table-column>
          <el-table-column align="center" prop="studentNum" label="班级人数"></el-table-column>
          <!-- <el-table-column align="center" label="课程单价">
            <template slot-scope="scope">
              <div>
                <p>线上:{{scope.row.onlineClassesUnitPrice}}</p>
                <p>线下:{{scope.row.offlineClassesUnitPrice}}</p>
              </div>
            </template>
          </el-table-column>-->
          <el-table-column align="center" label="当前课次">
            <template slot-scope="scope">
              <div>
                <p>{{scope.row.currentClassTimes + '/' + scope.row.totalClassTimes}}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="coursesStartDate" label="开课时间">
            <template slot-scope="scope">
              <div>
                <p>{{scope.row.coursesStartDate | formatTimer}}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="coursesExpireDate" label="结束时间">
            <template slot-scope="scope">
              <div>
                <p>{{scope.row.coursesExpireDate | formatTimer}}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="groupStatus" label="课程组状态">
            <template slot-scope="scope">
              <div>
                <p>{{scope.row.groupStatus | courseGroup}}</p>
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" width="150px" fixed="right" label="操作">
            <template slot-scope="scope">
              <div>
                <!-- v-permission="'courseSchedule/classStartDateAdjust'"  v-if="!scope.row.isSettlement" -->
                <el-button type="text" @click="lookCrouse(scope.row)">查看</el-button>
                <el-button
                  type="text"
                  v-if="scope.row.groupStatus == 'NORMAL'"
                  @click="closeCrouse(scope.row)"
                  v-permission="'practiceGroupManage/cancelGroup'"
                >关闭</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>
    <el-dialog title="关闭课程组" width="400px" :visible.sync="closeVisible">
      <el-form
        :model="closeForm"
        ref="closeForm"
        label-position="right"
        label-width="80px;"
        :inline="true"
      >
        <el-form-item label="是否退费" prop="isBasck">
          <el-radio v-model="closeForm.isBack" :label="true">是</el-radio>
          <el-radio v-model="closeForm.isBack" :label="false">否</el-radio>
        </el-form-item>
        <el-form-item label="退费金额" v-if="closeForm.isBack" prop="money">
          <el-input type="number" @mousewheel.native.prevent v-model.trim="closeForm.money"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeVisible = false">取 消</el-button>
        <el-button type="primary" @click="submieCloseCrouse">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import {
  getTeacher,
  getEmployeeOrgan,
  practiceGroupManage,
  cancelGroup
} from "@/api/buildTeam";
export default {
  components: { pagination },
  data() {
    return {
      searchForm: {
        search: null,
        status: null,
        teacherId: null,
        organIdList: null
      },
      closeForm: {
        isBack: false,
        money: null
      },
      closeVisible: false,
      activeRow: null,
      organList: [],
      statusList: [],
      teacherList: [],
      tableData: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      }
    };
  },
  created() {
    if (this.$route.query.searchForm) {
      this.$route.query.searchForm instanceof Object
        ? (this.searchForm = this.$route.query.searchForm)
        : (this.searchForm = JSON.parse(this.$route.query.searchForm));
    }
    if (this.$route.query.rules) {
      this.$route.query.rules instanceof Object
        ? (this.rules = this.$route.query.rules)
        : (this.rules = JSON.parse(this.$route.query.rules));
    }
    this.init();
  },
  mounted() {},
  activated() {
    this.init();
  },
  methods: {
    init() {
      getEmployeeOrgan().then(res => {
        if (res.code == 200) {
          this.organList = res.data;
        }
      });
      //   <!-- 状态 指导老师 活动方案-->
      getTeacher().then(res => {
        if (res.code == 200) {
          this.teacherList = res.data;
        }
      });
      this.getList();
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.searchForm = {
        search: null,
        status: null,
        teacherId: null,
        organIdList: null
      };
      this.search();
    },
    getList() {
      let obj = {
        search: this.searchForm.search || null,
        teacherId: this.searchForm.teacherId || null,
        organId: this.searchForm.organIdList || null,
        page: this.rules.page,
        rows: this.rules.limit,
        hasEducationalTeacherId:this.searchForm.hasEducationalTeacherId || null
      };
      practiceGroupManage(obj).then(res => {
        if (res.code == 200) {
          this.tableData = res.data.rows;
          this.rules.total = res.data.total; 
        }
      });
    },
    lookCrouse(row) {
      let rules = JSON.stringify(this.rules);
      let searchForm = JSON.stringify(this.searchForm);
      this.$router.push({
        path: "/business/accompanys",
        query: { id: row.id, rules, searchForm }
      });
    },
    closeCrouse(row) {
      this.activeRow = row;
      this.closeVisible = true;
    },
    courseVisibleClose() {
      // 关闭弹窗前
    },
    submieCloseCrouse() {
      // 提交关闭课程组
      if (this.closeForm.isBack) {
        if (!this.closeForm.money) {
          this.$message.error("请输入退费金额");
          return;
        }
      } else {
        this.closeForm.money = null;
      }
      // 请求数据
      cancelGroup({
        groupId: this.activeRow.id,
        groupType: "PRACTICE",
        refundAmount: this.closeForm.money
      }).then(res => {
        if (res.code == 200) {
          this.$message.success("关闭成功");
          this.getList();
          this.closeVisible = false;
        }
      });
    }
  },
  watch: {
    closeVisible(val) {
      if (!val) {
        console.log(val);
        this.activeRow = null;
        (this.closeForm = {
          isBack: false,
          money: null
        }),
          this.$refs.closeForm.resetFields();
        this.closeVisible = false;
      }
    }
  }
};
</script>
<style lang="sass">

</style>