<template>
  <div>
    <el-form class="flex-row border-bottom-1" :inline="true">
      <h3 class="font-size-16" style="position: relative; top: -2px">
        学校总人数
      </h3>

      <el-form-item label="学年学期：" style="margin-bottom: 10px">
        <el-select
          v-model="firstForm.team"
          placeholder="请选择学年学期"
          clearable
          @change="getListData"
          style="width: 300px"
        >
          <el-option
            v-for="item in teamList"
            :label="item.team_name"
            :value="item.team_id"
            :key="item.team_id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="d-flex">
      <div class="w70">
        <div id="lineBox" style="height: 500px"></div>
      </div>
      <div id="pieBox" class="w30" style="height: 500px"></div>
    </div>
    <div class="d-flex flex-between pt7">
      <div>
        <el-button type="primary" v-loading="loading" @click="addData"
          >生成数据</el-button
        >
        <el-button type="primary" @click="exportAll">导出打印</el-button>
        <el-button
          type="danger"
          @click="delData"
          :disabled="multipleSelection.length == 0"
          >删除</el-button
        >
      </div>
      <el-form class="mt20" :inline="true">
        <el-form-item label="">
          <el-select
            v-model="firstForm.team"
            placeholder="请选择学年学期"
            clearable
            @change="getListData"
          >
            <el-option
              v-for="item in teamList"
              :label="item.team_name"
              :value="item.team_id"
              :key="item.team_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="firstForm.grade"
            placeholder="请选择年级"
            clearable
            filterable
            @change="getListData"
          >
            <el-option
              v-for="(item, index) in gradeList"
              :label="item.grade_name"
              :value="item.grade_id"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" prop="num">
          <!-- <el-select
            v-model="num"
            placeholder="请选择人数"
            @change="getListData"
          >
            <el-option
              v-for="item in numList"
              :label="item.label"
              :value="item.label"
              :key="item.value"
              clearable
              filterable
            ></el-option>
          </el-select> -->
          <el-input v-model="num" placeholder="请选择人数"> </el-input>
        </el-form-item>
        <el-button type="primary" @click="getListData">搜索</el-button>
        <!-- <el-button type="primary" class="ml5" @click="clearData">清空</el-button> -->
      </el-form>
    </div>

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

      <el-table-column label="学期学年" prop="team_name" width="400">
      </el-table-column>
      <el-table-column label="年级" prop="grade_name" width="400">
      </el-table-column>
      <el-table-column label="人数" prop="num" width="auto"> </el-table-column>
      <el-table-column label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="text" @click="detailData(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>
  </div>
</template>

<script>
import * as echarts from "echarts";
import { Message } from "element-ui";
import { request } from "@/utils/request";
import { rstj_list, rstj_del, grade_search, team_search } from "./api";
import { useUserStore } from "@/stores/user";
const { token, user_org } = useUserStore();
export default {
  name: "index",
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      grade_id: "",
      grade_name: "",
      team_id: "",
      team_name: "",
      num: "",
      gradeList: [],
      tableData: [],
      teamList: [],
      firstForm: {
        grade: "",
        team: "",
      },
      numList: [
        {
          label: "50",
          value: "1",
        },
        {
          label: "60",
          value: "2",
        },
        {
          label: "70",
          value: "3",
        },
        {
          label: "80",
          value: "4",
        },
      ],
      title: "",
      id: "",
      loading: false,
      multipleSelection: [],
      dept_id: "",
      lineData: [],
      lineXData: [],
      pieData: [],
    };
  },

  methods: {
    getLine() {
      var chartDom = document.getElementById("lineBox");
      var myChart = echarts.init(chartDom);
      var option;
      option = {
        xAxis: {
          type: "category",
          boundaryGap: false,
          data: this.lineXData,
        },
        yAxis: {
          type: "value",
          axisLabel: {
            formatter: function (value) {
              if (Math.floor(value) === value) {
                return value; // 显示整数刻度
              }
              return ""; // 不显示小数刻度
            },
          },
        },
        grid: {
          left: "3%",
          right: "5%",
          bottom: "3%",
          containLabel: true,
        },
        series: [
          {
            data: this.lineData,
            type: "line",
            smooth: true,
            label: {
              show: true,
              position: "top",
            },
            areaStyle: {
              color: "#4351FF",
              opacity: ".15",
            },
            lineStyle: {
              color: "#4351FF",
            },
          },
        ],
      };
      option && myChart.setOption(option);
    },
    getPie() {
      var chartDom = document.getElementById("pieBox");
      var myChart = echarts.init(chartDom);
      var option;
      option = {
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            color: [
              "#4351FF",
              {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "#FBB0BF", // 0% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "#FBB0BF", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            ],
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "40",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: this.pieData,
          },
        ],
      };

      option && myChart.setOption(option);
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        team_id: this.firstForm.team,
        grade_id: this.firstForm.grade,
        num: this.num,
      };

      rstj_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        let leftData = res.data.chats.left;
        this.lineData = [];
        this.lineXData = [];
        leftData.map((item) => {
          this.lineData.push(item.num);
          this.lineXData.push(item.team_name);
        });
        let rightData = res.data.chats.right;
        this.pieData = [];
        rightData.map((item) => {
          this.pieData.push({ value: item.xdx_xb, value: item.num });
        });
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);

        this.$nextTick(() => {
          this.getPie();
          this.getLine();
        });
      });
    },
    gradeListData() {
      let data = {
        page: 1,
        limit: 9999,
      };
      grade_search(data).then((res) => {
        if (res.code == 1) {
          this.gradeList = res.data.page_data;
          // let obj = {
          //   grade_id: "",
          //   grade_name: "全部",
          // };
          // this.gradeList.unshift(obj);
        }
      });
    },
    teamListData() {
      let data = {
        page: 1,
        limit: 9999,
      };
      team_search(data).then((res) => {
        if (res.code == 1) {
          this.teamList = res.data.page_data;
          // let obj = {
          //   team_id: "",
          //   team_name: "全部",
          // };
          // this.teamList.unshift(obj);
        }
      });
    },
    addData() {
      this.loading = true;
      request({
        url: "xddy/dygl_xsrstjlb/recreate_data",
        data: {
          token: token,
          site: "jnmf",
          client: "web",
          api: "json",
          page: "1",
          limit: "10000",
          issubmit: 1,
          dept_id: this.dept_id,
        },
      }).then((res) => {
        if (res.code == "1") {
          this.loading = false;
          this.getListData();
        }
      });
    },
    delData() {
      this.$confirm("确认删除这些信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            id: this.multipleSelection,
          };

          rstj_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "信息删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },

    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].id);
      }
    },
    detailData(item) {
      this.$router.push({
        name: "xsxjk_xsrstjlb_xsrstjlbxq",
        params: {
          team_id: item.team_id,
          grade_id: item.grade_id,
          dept_id: this.dept_id,
        },
      });
    },
    exportAll() {
      let link =
        window.globalVariables.api +
        "/xddy/dygl_xsrstjlb/index?api=xls&page=1&limit=10000&token=" +
        token;
      window.open(link, "_blank");
    },
    clearData() {
      this.firstForm.grade = "";
      this.firstForm.team = "";
      this.num = "";
    },
  },
  mounted() {
    if (user_org) {
      user_org.map((item) => {
        if (item.uo_type === "2") {
          this.dept_id = item.uo_id;
        }
      });
    }
    this.getListData();
    this.getLine();
    this.getPie();
    this.gradeListData();
    this.teamListData();
  },
};
</script>

<style scoped>
</style>

  