<!--  -->
<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      服务指标明细
    </h2>
    <!-- <filter-search
        ref="filterSearch"
        @reload="reloadSearch"
        :keys="['searchType']"
        :moreKeys="['organId']"
      /> -->
    <div class="m-core">
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
        ref="form"
      >
        <el-form-item prop="organId">
          <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 prop="teacherId">
          <remote-search
            :commit="'setTeachers'"
            v-model="searchForm.teacherId"
          />
        </el-form-item>
        <el-form-item prop="unDone">
          <el-select
            class="multiple"
            v-model.trim="searchForm.unDone"
            clearable
            placeholder="是否异常"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="reminded">
          <el-select
            class="multiple"
            v-model.trim="searchForm.reminded"
            clearable
            placeholder="是否提醒"
          >
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="operatorId">
          <remote-search
            :commit="'setEmploys'"
            v-model.trim="searchForm.operatorId"
          />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model.trim="searchForm.jobNature"
            filterable
            clearable
            placeholder="工作类型"
          >
            <el-option
              v-for="item in jobNature"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            style="width: 410px"
            v-model.trim="orderDate"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
            @change="changeWeek"
            :picker-options="bigin()"
          >
          </el-date-picker>
        </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-button @click="onExport" type="primary" v-permission="'export/teacherServeInfo'">导出</el-button>
        </el-form-item>
      </save-form>
      <div class="btnWraps" style="margin-bottom: 20px">
        <el-button
          type="primary"
          v-permission="'exercisesSituation/teacherServeRemindPush'"
          @click="remindTeachers"
          >批量提醒</el-button
        >
      </div>
      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
            :selectable="checkSelectable"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="organName"
            label="分部"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="teacherId"
            label="老师编号"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="teacherName"
            label="老师姓名"
          ></el-table-column>
           <el-table-column
            align="center"
            prop="teacherName"
            label="服务周期"
            width="180px"
          >
          <template slot-scope="scope">
            <div>
              {{scope.row.monday | dayjsFormat}}~{{scope.row.sunday | dayjsFormat}}
            </div>
          </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="homeworkNum"
            label="课后作业(节)"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="exerciseNum"
            label="课外作业(人)"
          ></el-table-column>
          <el-table-column align="center" prop="studentId" label="是否异常">
            <template slot-scope="scope">
              {{ scope.row.unDone ? "是" : "否" }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="remindDate"
            label="提醒时间"
          >
          <template slot-scope="scope">
            <div>
            {{scope.row.remindDate | dateForMinFormat}}
            </div>
          </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="operatorName"
            label="操作人"
          ></el-table-column>
          <el-table-column align="center" prop="studentId" label="操作" width="160px">
            <template slot-scope="scope">
              <div>
                <el-button type="text" @click="gotoDteail(scope.row)"
                  >查看</el-button
                >
                <el-button
                  type="text"
                  v-if="scope.row.unDone"
                  v-permission="'exercisesSituation/teacherServeRemindPush'"
                  @click="remindTeacher(scope.row)"
                  >提醒老师</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
      <el-dialog
        title="查看"
        destroy-on-close
        :close-on-click-modal="false"
        :visible.sync="lookVisible"
        v-if="lookVisible"
      >
        <serverMask
          :detail="activeRow"
          @close="lookVisible = false"
          @submited="getList"
        />
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="lookVisible = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import serverMask from "./conponent/serverMask";
import load from "@/utils/loading";
import { getTeacherServeInfo, teacherServeRemindPush } from "./api";
import { getTimes } from "@/utils";
import { getNowDateAndSunday, getNowDateAndMonday } from "@/utils/date";
import { jobNature } from "@/utils/searchArray";
import { Export } from '@/utils/downLoadFile'
import qs from 'qs'
export default {
  components: { pagination, serverMask },
  data() {
    return {
      searchForm: {
        organId: "",
        teacherId: "",
        operatorId: "",
        reminded: "",
        unDone: "",
        operatorId: "",
        jobNature:""
      },
      orderDate: [],
      tableList: [],
      organList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      lookVisible: false,
      activeRow: null,
      chioseList: [],
       jobNature: jobNature,
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部

    this.init();
  },
  methods: {
    init() {
      this.$store.dispatch("setBranchs");
      this.$store.dispatch("setEmploys");
      this.getDefaultTime();
      if (this.$route.query.searchType) {
        this.searchForm.organId = this.$route.query.organId * 1 || "";
        this.searchForm.unDone = this.$route.query.unDone+'' || ""
         this.searchForm.reminded = this.$route.query.reminded+'' || ""
        const dayjs = this.$helpers.dayjs;
        let nowDate = dayjs(new Date()).format("YYYY-MM-DD");
        this.orderDate = [
          getNowDateAndMonday(nowDate),
          getNowDateAndSunday(nowDate),
        ];
      }

      this.getList();
    },
    async getList() {

      try {
        const {orderDate, ...rest} = this.searchForm;
        const result = await getTeacherServeInfo({
          ...rest,
          ...getTimes(this.orderDate, ["monday", "sunday"]),
          rows: this.rules.limit,
          page: this.rules.page,
        });
        this.rules.total = result.data.total;
        this.tableList = result.data.rows;
      } catch (e) {}
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$refs.form.resetFields();
      this.getDefaultTime();
      this.search();
    },
    gotoDteail(row) {
      this.activeRow = row;
      this.lookVisible = true;
    },
    reloadSearch() {
      this.getList();
    },
    getDefaultTime() {
      const dayjs = this.$helpers.dayjs;
      let nowDate = dayjs(new Date()).format("YYYY-MM-DD");
      let lastWeek = dayjs(nowDate).subtract(1, "week").format("YYYY-MM-DD");
      this.orderDate = [
        getNowDateAndMonday(lastWeek),
        getNowDateAndSunday(nowDate),
      ];
      this.searchForm.jobNature = 'FULL_TIME'
    },
    changeWeek(val) {
      if (val) {
        this.orderDate =  [
          getNowDateAndMonday(val[0]),
          getNowDateAndSunday(val[1]),
        ]
      } else {
        this.getDefaultTime();
      }
    },
    remindTeacher(row) {
      this.$confirm("是否给老师发送提醒?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        try {
          const result = await teacherServeRemindPush([row]);
          this.getList();
          this.$message.success("提醒成功");
        } catch (e) {
          console.log(e);
        }
      });
    },
    remindTeachers() {
      if (this.chioseList.length <= 0) {
        this.$message.error("请至少选择一条信息");
        return;
      }
      this.$confirm("是否批量给老师发送提醒?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        try {
          const result = await teacherServeRemindPush(this.chioseList);
          this.getList();
          this.$message.success("提醒成功");
        } catch (e) {
          console.log(e);
        }
      });
    },
    checkSelectable(row) {
      return row.unDone;
    },
    handleSelectionChange(arr) {
      this.chioseList = arr;
    },
    bigin(){
      let self = this;
      return {
        firstDayOfWeek: 1,
        disabledDate(time) {
            return time.getTime() >= Date.now();
        },
      };
    },
    onExport() {
      const { orderDate, ...rest } = this.searchForm;
      Export(
        this,
        {
          url: "/api-web/export/teacherServeInfo",
          fileName: "服务指标明细.xls",
          method: "post",
          params: qs.stringify({
            ...rest,
            ...getTimes(this.orderDate, ["monday", "sunday"]),
          }),
        },
        "您确定导出服务指标明细?"
      );
    },
  },
};
</script>
<style lang='scss' scoped>
</style>
