<template>
  <!-- 项目管控台 -->
  <div class="xmgktContent">
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="createNew" icon="el-icon-plus">新建报表</el-button>
        <el-button type="primary" size="medium" @click="handleDownload">导出</el-button>
        <el-button type="danger" size="medium" @click="delData()">删除</el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="create_dateline">
          <el-date-picker v-model="searchForm.create_dateline" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" @change="getListData"></el-date-picker>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input placeholder="请输入搜索关键字" v-model="searchForm.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" style="width: 100%" @selection-change="handleSelectionChange">
      <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 v-for="(item, index) in columnData" :key="index" :label="item.name" show-overflow-tooltip :prop="item.key" width="auto"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="lookDetail(scope.row)">详情</el-button>
          <!-- <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <el-button type="text" @click="delData(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="dialogTitle" :visible.sync="dialogFormVisible" width="600px" @close="handleClose">
      <add-and-edit ref="addAndEdit" :currentId="currentId" :selectObj="selectObj" :yearData="yearData" @close="handleClose"></add-and-edit>
      <div slot="footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { tjbbList, tjbbDelete } from "./api";
import AddAndEdit from "./AddAndEdit.vue";
import { download } from "@/utils/request";
export default {
  data() {
    return {
      searchForm: {
        create_dateline: "",
        keyword: "",
      },
      loading: false,
      columnData: [
        {
          name: "报表名称",
          key: "bb_name"
        },
        {
          name: "创建人",
          key: "create_user_name"
        },
        {
          name: "创建时间",
          key: "create_dateline"
        }
      ],
      multipleSelection: [],
      tableData: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogTitle: "",
      dialogFormVisible: false,
      currentId: "",
      selectObj: {},
      yearData: [],
    }
  },
  components: { AddAndEdit },
  methods: {
    async getListData(flag) {
      this.loading = true;
      let obj = {
        page: this.cur_page,
        limit: this.limit,
        create_dateline: this.searchForm.create_dateline,
        keyword: this.searchForm.keyword,
        site: "jnmf",
      }
      let data = await tjbbList(obj);
      if (data) {
        this.tableData = data.page_data;
        this.cur_page = Number(data.page_now);
        this.total = Number(data.total_rows);
        this.loading = false;

        if (flag == 1) {
          for (let key in data.table_structure.field) {
            if (data.table_structure.field[key].type == "radio") {
              this.selectObj[key] = data.table_structure.field[key];
            }
          }
        }
      }
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = [...val];
    },
    createNew() {
      this.dialogFormVisible = true;
      this.dialogTitle = "新建报表";
    },
    editData(data) {
      this.dialogFormVisible = true;
      this.currentId = data.bb_id;
      this.dialogTitle = "编辑报表";
    },
    delData(data) {
      if (data == undefined && this.multipleSelection.length == 0) {
        this.$message.error("请选择要删除的数据！");
        return false;
      }
      this.$confirm("是否确定删除当前数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = "";
          if (data != undefined) {
            res = await tjbbDelete({ bb_id: data.bb_id });
          } else {
            let arr = [];
            this.multipleSelection.map((item) => {
              arr.push(item.bb_id);
            });
            res = await tjbbDelete({ bb_id: arr });
          }
          if (res && res != "") {
            this.$message.success("删除成功！");
            this.getListData();
          }
        })
        .catch(() => { });
    },
    handleClose(type) {
      this.dialogFormVisible = false;
      this.$refs.addAndEdit && this.$refs.addAndEdit.resetForm();
      this.currentId = "";
      if (type == 1) {
        this.getListData();
      }
    },
    formSubmit() {
      this.$refs.addAndEdit.commitData();
    },
    lookDetail(data) {
      this.$router.push({ name: "xmgl-bbtj-detail", params: { id: data.bb_id } })
    },
    async handleDownload() {
      let arr = [];
      this.multipleSelection.map((value) => {
        arr.push(value.bb_id);
      });
      let obj = {
        api: 'xls',
        page: this.cur_page,
        limit: this.limit,
        create_dateline: this.searchForm.create_dateline,
        keyword: this.searchForm.keyword,
        site: "jnmf",
        ids: arr.join(","),
      };
      let res = await tjbbList(obj);
      if (res) {
        window.open(res.download_url, "_blank");
      }
    }
  },
  created() {
    this.getListData(1);

    this.yearData = [];
    let currentYear = new Date().getFullYear();
    for (let i = currentYear - 5; i < currentYear + 10; i++) {
      this.yearData.push({
        value: i + "",
        label: i + "年"
      })
    }
  }
}
</script>

<style>
</style>