<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="addData">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">新建数据汇总</span>
        </el-button>
        <el-button type="primary" size="medium" @click="exportAll">
          <span class="d-in-block v-mid font-size-16">导出</span>
        </el-button>
        <el-button type="danger" size="medium" @click="delData" :disabled="multipleSelection.length == 0">
          <span class="d-in-block v-mid font-size-16">删除</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="create_dateline">
          <el-date-picker style="width: 100%" v-model="create_dateline" value-format="yyyy/MM/dd" type="date" placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input placeholder="请输入搜索关键字" v-model="keyword" clearable @clear="getListData">
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData">搜索</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="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column label="报表名称" show-overflow-tooltip prop="bb_name" width="auto">
      </el-table-column>
      <el-table-column label="创建人" width="250" prop="create_user_name"></el-table-column>
      <el-table-column label="创建时间" width="300" prop="create_dateline"></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="toDetail(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="title" :visible.sync="dialogFormVisible" width="580px">
      <el-form :model="dialogForm" ref="dialogForm" :rules="rules" label-width="110px">
        <el-form-item label="报表名称" prop="bb_name">
          <el-input v-model="dialogForm.bb_name" placeholder="请输入报表名称"></el-input>
        </el-form-item>

        <el-form-item label="选择年份" prop="bb_year">
          <el-select v-model="dialogForm.bb_year" multiple placeholder="请选择" style="width: 100%">
            <el-option v-for="item in yearData" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择学校" prop="bb_school_name">
          <el-select v-model="dialogForm.bb_school_name" style="width: 100%" placeholder="请选择学校">
            <el-option v-for="(item,index) in schoolData" :key='index' :value="item.xs_school_name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择比赛成绩" prop="ry_id">
          <el-select v-model="dialogForm.ry_id" @change="handleChange" style="width: 100%">
            <el-option v-for="item in ryData" :key="item.ry_id" :label="item.ry_name" :value="item.ry_id"></el-option>
          </el-select>

        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" :disabled="isButton" @click="formSubmit('dialogForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { useUserStore } from "@/stores/user";
const { real_name, token } = useUserStore();
import { sjhz_list, sjhz_add, sjhz_del, rygl_list, school_list, bbys } from "./api";
export default {
  name: "index",
  data() {
    return {
      time: "",
      keyword: "",
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      create_dateline: "",
      dialogForm: {
        bb_name: "",
        create_user_name: "",
        create_dateline: "",
        bb_year: "",
        bb_school_id: "",
        bb_school_name: "",
        ry_id: "",
        ry_name: "",
      },
      yearData: [],
      ryData: [],
      title: "",
      multipleSelection: [],
      id: "",
      loading: false,
      isEdit: false,
      schoolData: [],
      rules: {
        bb_name: [
          { required: true, message: "请输入报表名称", trigger: "blur" },
        ],
        bb_year: [
          { required: true, message: "请选择年份", trigger: "blur" },
        ],
        bb_school_name: [
          { required: true, message: "请选择学校", trigger: "blur" },
        ],
        ry_id: [
          { required: true, message: "请选择成绩", trigger: "blur" },
        ],

      },
      isButton: false,
    };
  },
  methods: {
    //学校
    schoolListData() {
      school_list().then((res) => {
        this.schoolData = res.data.data;
        console.log(this.schoolData, 333);
      });
    },
    //比赛成绩 即荣誉
    ryListData() {
      this.loading = true;
      let data = {

      };
      rygl_list(data).then((res) => {
        this.loading = false;
        this.ryData = res.data.page_data;
        console.log(this.ryData, 1344);
      });
    },
    handleChange() {
      this.ryData.map((item) => {
        if (item.ry_id == this.dialogForm.ry_id) {
          this.dialogForm.ry_name = item.ry_name;
        }
      })
      console.log(this.ryData, this.dialogForm.ry_id, this.dialogForm.ry_name, 1234);
    },
    //年份下拉框
    getYear() {
      var myDate = new Date();
      var year = myDate.getFullYear();
      this.initSelectYear(year);
    },
    initSelectYear(year) {
      this.yearData = [];
      for (let i = 0; i < 30; i++) {
        this.yearData.push({ value: year - i, label: year - i + "年" });
      }
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        create_dateline: this.create_dateline,
        site: 'jnmf'
      };

      sjhz_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);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {

      this.title = "新建数据汇总";
      this.isButton = false;
      this.dialogForm = {
        bb_name: "",
        create_user_name: "",
        create_dateline: "",
        bb_year: "",
        bb_school_id: "",
        bb_school_name: "",
        ry_id: "",
        ry_name: "",
      };
      this.isEdit = false;

      this.dialogFormVisible = true;
    },

    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isButton = true;
          let data = {
            bb_name: this.dialogForm.bb_name,
            create_user_name: real_name,
            create_dateline: this.dialogForm.create_dateline,
            bb_year: this.dialogForm.bb_year,
            bb_school_id: this.dialogForm.bb_school_id,
            bb_school_name: this.dialogForm.bb_school_name,
            ry_id: this.dialogForm.ry_id,
            ry_name: this.dialogForm.ry_name,
          };

          sjhz_add(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "报表添加成功！",
                type: "success",
              });

              this.dialogFormVisible = false;
              this.getListData();
            } else {
              this.isButton = false;
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        } else {
          this.isButton = false;
        }
      });
    },

    formCancel() {
      this.dialogFormVisible = false;
    },
    delData() {
      this.$confirm("确认删除这些报表?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            bb_id: this.multipleSelection,
          };

          sjhz_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].bb_id);
      }
    },
    toDetail(item) {
      this.$router.push({
        name: "sjhz_detail", params: {
          bb_id: item.bb_id,
        }
      });
    },
    exportAll() {
      let ids = this.multipleSelection.join(",");
      let data = {
        site: "jnmf",
        limit: "999",
        page: "1",
        ids: ids,
        api: 'xls'
      };
      bbys(data).then(res => {
        if (res.code == '1') {
          window.open(res.data.download_url, "_blank");
        }
      })
    },
  },
  mounted() {
    this.getListData();
    this.getYear();
    this.ryListData();
    this.schoolListData()
  },
};
</script>

<style scoped>
::v-deep .el-dialog__body {
  border-top: 1px solid #f0f3f8;
}
</style>