<template>
  <div class>
    <!-- <h2>
      <div class="squrt"></div>网管课列表
    </h2>-->
    <div
      class="newBand"
      v-permission="'export/practiceGroup'"
      @click="onStudentExport"
      style="width: 120px;"
    >网管课续费提醒</div>
    <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>
        <el-form-item>
          <el-select
            v-model.trim="searchForm.practiceGroupType"
            clearable
            filterable
            placeholder="课程组类型"
          >
            <el-option
              v-for="(item,index) in practiceGroupType"
              :key="index"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <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-select v-model.trim="searchForm.groupStatus" clearable filterable placeholder="课程组状态">
            <el-option
              v-for="(item,index) in commGroupStatus"
              :key="index"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model.trim="searchForm.firstOrRenew" clearable filterable placeholder="是否续费">
            <el-option label="首充" value="1"></el-option>
            <el-option label="续费" value="0"></el-option>
            <el-option label="免费" value="2"></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="课程组编号" width="100"></el-table-column>
          <el-table-column align="center" prop="name" label="课程组名称" width="100"></el-table-column>
          <el-table-column align="center" prop="type" label="课程组类型" width="100">
            <template slot-scope="scope">
              <div>
                <p>{{scope.row.type | comType}}</p>
              </div>
            </template>
          </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="开课时间" width="120">
            <template slot-scope="scope">
              <div>
                <p>{{scope.row.coursesStartDate | formatTimer}}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="coursesExpireDate" label="结束时间" width="120">
            <template slot-scope="scope">
              <div>
                <p>{{scope.row.coursesExpireDate | formatTimer}}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="groupStatus" label="课程组状态" width="100">
            <template slot-scope="scope">
              <div>
                <p>{{scope.row.groupStatus | comCourseGroup}}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="是否续费" fixed="right">
            <template slot-scope="scope">
              <div>
                <p v-if="scope.row.buyMonths == 0">免费</p>
                <p v-if="scope.row.buyMonths > 0">{{scope.row.beRenewGroupId>0?'续费':'首充'}}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" width="150" prop="memo" label="备注" fixed="right"></el-table-column>

          <el-table-column align="center" width="200" 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' && permission('practiceGroupManage/cancelGroup')"
                  @click="closeCrouse(scope.row)"
                >关闭</el-button>
                <el-button
                  type="text"
                  v-if="permission('practiceGroupManage/updateMemo')"
                  @click="resetMemo(scope.row)"
                >修改备注</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>
    <el-dialog title="备注" width="400px" :visible.sync="memoVisible">
      <el-input type="textarea" v-model.trim="memoForm.memo" :rows="7" :maxlength="255" show-word-limit></el-input>
      <div slot="footer" class="dialog-footer">
        <el-button @click="memoVisible = false">取 消</el-button>
        <el-button type="primary" @click="subMemo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import { permission } from "@/utils/directivePage";
import { commGroupStatus, practiceGroupType } from "@/utils/searchArray";
import {
  getTeacher,
  getEmployeeOrgan,
  practiceGroupManage,
  cancelGroup,
  practiceUpdateMemo
} from "@/api/buildTeam";
import axios from "axios";
import { getToken } from "@/utils/auth";
import load from "@/utils/loading";
export default {
  components: { pagination },
  data() {
    return {
      searchForm: {
        search: null,
        status: null,
        teacherId: null,
        organIdList: null,
        firstOrRenew: null,
        practiceGroupType: null,
        groupStatus: null
      },
      closeForm: {
        isBack: false,
        money: null
      },
      memoForm: {
        memo: "",
        groupId: ""
      },
      memoVisible: false,
      closeVisible: false,
      activeRow: null,
      organList: [],
      statusList: [],
      teacherList: [],
      tableData: [],
      practiceGroupType: practiceGroupType,
      commGroupStatus: commGroupStatus,
      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();
    },
    permission(str, parent) {
      return permission(str, parent);
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.searchForm = {
        search: null,
        status: null,
        teacherId: null,
        organIdList: null,
        firstOrRenew: null,
        practiceGroupType: null,
        groupStatus: null
      };
      this.search();
    },
    onStudentExport() {
      // 导出VIP课
      // let searchForm = this.searchForm;
      let data = {
        // teacherId: searchForm.teacherId || null,
        // activityId: searchForm.activityId || null,
        // organId: searchForm.orgin || null,
        // status: searchForm.status || null,
        // search: searchForm.search || null
      };
      let url = "/api-web/export/practiceGroup";
      const options = {
        method: "get",
        headers: {
          Authorization: getToken()
        },
        params: data,
        url,
        responseType: "blob"
      };
      this.$confirm("网管课续费提醒导出?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          load.startLoading();
          axios(options)
            .then(res => {
              let blob = new Blob([res.data], {
                // type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
                type: "application/vnd.ms-excel;charset=utf-8"
                //word文档为application/msword,pdf文档为application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8
              });

              let text = new Response(blob).text();
              text.then(res => {
                // 判断是否报错
                if (res.indexOf("code") != -1) {
                  let json = JSON.parse(res);
                  this.$message.error(json.msg);
                } else {
                  let objectUrl = URL.createObjectURL(blob);
                  let link = document.createElement("a");
                  let fname = "网管课续费提醒" + new Date().getTime(); //下载文件的名字
                  link.href = objectUrl;
                  link.setAttribute("download", fname);
                  document.body.appendChild(link);
                  link.click();
                }
              });
              load.endLoading();
            })
            .catch(error => {
              this.$message.error("导出数据失败，请连接管理员");
              load.endLoading();
            });
        })
        .catch(() => {});
    },
    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,
        type: this.searchForm.firstOrRenew || null,
        practiceGroupType: this.searchForm.practiceGroupType || null,
        groupStatus: this.searchForm.groupStatus || 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, type: row.type }
      });
    },
    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;
        }
      });
    },
    resetMemo(row) {
      this.$set(this.memoForm, "memo", row.memo);
      this.$set(this.memoForm, "groupId", row.id);
      this.memoVisible = true;
    },
    subMemo() {
      practiceUpdateMemo(this.memoForm).then(res => {
        if (res.code == 200) {
          this.$message.success("修改成功");
          this.memoVisible = false;
          this.getList();
        }
      });
    }
  },
  watch: {
    closeVisible(val) {
      if (!val) {
        this.activeRow = null;
        (this.closeForm = {
          isBack: false,
          money: null
        }),
          this.$refs.closeForm.resetFields();
        this.closeVisible = false;
      }
    }
  }
};
</script>
<style lang="sass">

</style>