<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" :disabled="multipleSelection.length == 0">
          <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%" @change="getListData" v-model="create_dateline" value-format="yyyy-MM-dd" type="date" placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="jkbb_bbmc">
          <el-input placeholder="请输入搜索关键字" clearable @clear="getListData" v-model="jkbb_bbmc">
          </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="jkbb_bbmc" width="auto">
      </el-table-column>
      <el-table-column label="创建人" width="250" prop="jkbb_cjr"></el-table-column>
      <el-table-column label="创建时间" width="300" prop="create_date"></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <!-- <el-button type="text" @click="editData(scope.row)">编辑 </el-button> -->
          <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>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="580px">
      <el-form :model="dialogForm" ref="dialogForm" :rules="rules" label-width="80px">
        <el-form-item label="报表名称" prop="jkbb_bbmc">
          <el-input v-model="dialogForm.jkbb_bbmc" placeholder="请输入报表名称"></el-input>
        </el-form-item>
        <el-form-item label="选择学期" prop="jkbb_xq">
          <el-select v-model="dialogForm.jkbb_xq" style="width: 100%" placeholder="请选择学期">
            <el-option v-for="item in teamData" :key="item.id" :label="item.team_name" :value="item.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 { bbtj_list, bbtj_add, bbtj_del, team_list, bbys } from "./api";
export default {
  name: "index",
  data() {
    return {
      isButton: false,
      time: "",
      jkbb_bbmc: "",
      create_dateline: "",
      tableData: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogFormVisible: false,
      dialogForm: {
        jkbb_bbmc: "",
        jkbb_xq: "",
        jkbb_xq_mc: "",
        jkbb_cjr: ""
      },
      teamData: [],
      title: "",
      multipleSelection: [],
      id: "",
      loading: false,
      isEdit: false,
      ids: [],
      rules: {
        jkbb_bbmc: [
          { required: true, message: "请输入报表名称", trigger: "submit" },
        ],
        jkbb_xq: [
          { required: true, message: "请选择学期", trigger: "blur" },
        ]
      }
    };
  },

  methods: {
    getTeamListData() {
      this.loading = true;
      team_list().then((res) => {
        this.loading = false;
        this.teamData = res.data.page_data;
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        jkbb_bbmc: this.jkbb_bbmc,
        create_dateline: this.create_dateline
      };

      bbtj_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.getTeamListData();
      this.title = "新建报表";
      this.isButton = false;
      this.dialogForm = {
        jkbb_bbmc: "",
        jkbb_xq: "",
        jkbb_xq_mc: "",
        jkbb_cjr: ""
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },

    formSubmit(formName) {
      this.teamData.forEach((item) => {
        if (item.id == this.dialogForm.jkbb_xq) {
          this.dialogForm.jkbb_xq_mc = item.team_name;
        }
      });
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isButton = true;
          let data = {
            jkbb_xq: this.dialogForm.jkbb_xq,
            jkbb_xq_mc: this.dialogForm.jkbb_xq_mc,
            jkbb_bbmc: this.dialogForm.jkbb_bbmc,
            jkbb_cjr: real_name
          };

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

          bbtj_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].jkbb_id);
      }
    },
    exportAll() {
      let link = window.globalVariables.api + "/jdbg/jkda_bb/zip";
      let zipLink;
      let data = {
        site: "jnmf",
        jkbb_id: this.multipleSelection.join(","),
      }; console.log(window.globalVariables.api);
      bbys(data).then((res) => {
        zipLink = res.data.files;
        let href = window.globalVariables.api + zipLink;
        window.open(href, "_blank");
      });
    },
    detailData(item) {
      this.$router.push({
        name: "bbtj_detail",
        params: {
          jkbb_xq: item.jkbb_xq,
          jkbb_bbmc: item.jkbb_bbmc
        },
      });
      console.log(item, 11);
    }
  },
  mounted() {
    this.getListData();
  },
};
</script>

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