<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-button class="" type="primary" @click="exportAll">导出</el-button>
      </div>
      <el-form :inline="true">
        <el-form-item label="">
          <el-select
            v-model="firstForm.grade"
            placeholder="请选择年级"
            clearable
            @change="getListData"
          >
            <el-option
              v-for="item in gradeList"
              :key="item.grade_id"
              :label="item.grade_name"
              :value="item.grade_name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="firstForm.class"
            placeholder="请选择班级"
            clearable
            @change="getListData"
          >
            <el-option
              v-for="item in classList"
              :label="item.class_name"
              :value="item.class_name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select
            placeholder="请选择优胜班级"
            v-model="xdbs_ysbj"
            clearable
            @change="getListData"
          >
            <el-option
              v-for="item in ysbjList"
              :label="item.v"
              :value="item.n"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr10" label="">
          <el-date-picker
            v-model="firstForm.time"
            clearable
            @change="getTime"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item class="mr10" label="时间：">
        <el-date-picker v-model="firstForm.time" clearable @change="getTime" type="daterange" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item> -->
        <!-- <el-form-item prop="keyword">
        <el-input
          placeholder="请输入分值"
          v-model="keyword"
          clearable
          @clear="getListData"
          size="small"
        ></el-input>
      </el-form-item>
      <el-form-item prop="keyword">
        <el-input
          placeholder="请输入分值"
          v-model="keyword"
          clearable
          @clear="getListData"
          size="small"
        ></el-input>
      </el-form-item> -->

        <el-button type="primary" class="ml5" @click="getListData"
          >搜索</el-button
        >
        <!-- <el-button type="primary" class="ml5" @click="clearData">清空</el-button> -->
      </el-form>
    </div>

    <!-- <div class="big">
     
      <div class="small"><span class="ml-20px">优胜班级个数:</span>10{{num}}个</div>
    </div> -->

    <el-table
      @selection-change="handleSelectionChange"
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>

      <el-table-column label="年级" prop="grade_name" width="">
      </el-table-column>
      <el-table-column label="班级" prop="class_name" width="">
      </el-table-column>
      <el-table-column label="时间" prop="xdbs_sj" width="200">
      </el-table-column>
      <el-table-column
        label="常规分值"
        prop="xdbs_cgxmfz"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column
        label="卫生分值"
        prop="xdbs_wsxmfz"
        align="center"
        width=""
      >
      </el-table-column>
      <el-table-column label="总分值" prop="xdbs_sjzdf" align="center" width="">
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button
            type="text"
            class="btn"
            v-if="scope.row.xdbs_hqbj_option_k == 0"
            @click="hqbjSet(scope.row.xdbb_id)"
            >设为红旗班级</el-button
          >
          <el-button
            type="text"
            v-else
            class="btn"
            style="color: #a0a0a0"
            disabled
            >设为红旗班级</el-button
          >
          <el-button
            type="text"
            class="btn"
            v-if="scope.row.xdbs_ysbj_option_k == 0"
            @click="ysbjSet(scope.row.xdbb_id)"
            >设为优胜班级</el-button
          >
          <el-button
            type="text"
            v-else
            class="btn"
            style="color: #a0a0a0"
            disabled
            >设为优胜班级</el-button
          >
          <el-button type="text" @click="rate(scope.row)">总评 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog
      title="总评"
      :visible.sync="commentDialogVisible"
      width="550px"
      v-loading="loading"
    >
      <el-form
        :model="commentForm"
        ref="commentForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="班级总评" prop="xdbs_zp">
          <el-input
            type="textarea"
            :rows="5"
            placeholder="请输入总评"
            v-model="commentForm.xdbs_zp"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="commentDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit('commentForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { bjpb_list, bjpb_edit, grade_search, class_search } from "./api";
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      grade_name: "",
      class_name: "",
      grade_id: "",
      class_id: "",
      xdbs_sj: "",
      xdbs_ysbj: "",
      tableData: [],
      gradeList: [],
      classList: [],
      firstForm: {
        grade: "",
        class: "",
        time: [],
      },
      num: "0",
      loading: false,
      commentForm: {
        xdbs_zp: "",
      },
      xdbb_id: "",
      commentDialogVisible: false,
      multipleSelection1: [],
      rules: {
        xdbs_zp: [{ required: true, message: "请输入总评", trigger: "blur" }],
      },
      ysbjList: [
        {
          v: "是",
          n: "2",
        },
        {
          v: "否",
          n: "0",
        },
      ],
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        grade_name: this.firstForm.grade,
        class_name: this.firstForm.class,
        xdbs_sj: this.xdbs_sj,
        xdbs_ysbj: this.xdbs_ysbj,
      };
      if (this.firstForm.time.length > 0) {
        data.xdbs_sj = this.firstForm.time[0] + " - " + this.firstForm.time[1];
      } else {
        data.xdbs_sj = "";
      }
      bjpb_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);

        // for (let i = 0; i < this.tableData.length; i++) {
        //   console.log(this.tableData[i].xdbs_ysbj, 111);
        //   if (this.tableData[i].xdbs_ysbj == "是") {
        //     this.num++
        //   }
        // }
      });
    },
    gradeListData() {
      this.loading = true;
      let data = {};
      grade_search(data).then((res) => {
        this.loading = false;
        this.gradeList = res.data.page_data;
      });
    },

    classListData() {
      this.loading = true;
      let data = {};
      class_search(data).then((res) => {
        this.loading = false;
        this.classList = res.data.page_data;
      });
    },

    getTime() {
      if (!this.firstForm.time) {
        this.firstForm.time = [];
      }
      this.cur_page = 1;
      this.getListData();
    },

    ysbjSet(id) {
      let data = {
        xdbb_id: id,
        xdbs_ysbj: "2",
      };
      bjpb_edit(data).then((res) => {
        if (res.code == "1") {
          res.data.one_info.xdbs_ysbj = "2";

          this.$message({
            message: "优胜班级设置成功。",
            type: "success",
          });
          this.getListData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    hqbjSet(id) {
      let data = {
        xdbb_id: id,
        xdbs_hqbj: "2",
      };
      bjpb_edit(data).then((res) => {
        if (res.code == "1") {
          res.data.one_info.xdbs_hqbj = "2";
          this.$message({
            message: "红旗班级设置成功。",
            type: "success",
          });
          this.getListData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    rate(item) {
      this.title = "总评";
      this.id = item.xdbb_id;
      this.commentForm.xdbs_zp = item.xdbs_zp;
      this.commentDialogVisible = true;
    },
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            xdbs_zp: this.commentForm.xdbs_zp,
          };
          data.xdbb_id = this.id;

          bjpb_edit(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "评价成功！",
                type: "success",
              });
              this.commentDialogVisible = false;
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection1 = [];
      for (let i in val) {
        this.multipleSelection1.push(val[i].xdbb_id);
        console.log(this.multipleSelection1, 123);
      }
    },
    exportAll() {
      let time = "";
      if (this.firstForm.time.length > 0) {
        time = this.firstForm.time[0] + " - " + this.firstForm.time[1];
      } else {
        time = "";
      }
      let link =
        window.globalVariables.api +
        "/xddy/dygl_bjzzpb_bjpfgl/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xdbb_id=" +
        this.multipleSelection1.join(",");
      window.open(link, "_blank");
    },
    clearData() {
      this.firstForm.grade = "";
      this.firstForm.class = "";
      this.firstForm.time = "";
    },
  },
  mounted() {
    this.getListData();
    this.gradeListData();
    this.classListData();
  },
};
</script>

<style lang="scss" scoped>
.big {
  width: 204px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 0 15px;
}
::v-deep .el-input {
  width: 160px;
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 300px;
}
::v-deep .el-date-editor .el-range-separator {
  width: 8%;
}
</style>