<template>
  <div>
    <div class="flex-row" style="margin: 10px;">
      <div class="d-flex flex-v-mid">
        <div>{{detailInfo.jdtj_name}}</div>
        <div class="ml30">
          <span class="mr10" v-for="item in year" :key="item">{{item}}年</span>
        </div>
      </div>
      <div>
        <el-button size="medium" type="primary" @click="dialogFormVisible=true">编辑</el-button>
        <el-button size="medium" type="primary" @click="handleDownload">导出</el-button>
      </div>
    </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>
    <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="dialogFormVisible" width="600px" @close="handleClose">
      <add-and-edit ref="addAndEdit" :currentId="parentId" :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 { dddxtjbbDetail, dddx_ddrwList } from "./api";
import { download } from "@/utils/request";
import AddAndEdit from "./AddAndEdit.vue";
export default {
  data() {
    return {
      parentId: "",
      detailInfo: {},
      year: [],
      loading: false,
      columnData: [
        {
          name: "督导任务",
          key: "jdd_name"
        },
        {
          name: "类型",
          key: "jdd_ddlx"
        },
        {
          name: "对象",
          key: "jdd_dddx"
        }
      ],
      tableData: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      multipleSelection: [],
      dialogFormVisible: false,
      yearData: [],
    }
  },
  components: { AddAndEdit },
  methods: {
    async initDetailInfo() {
      let data = await dddxtjbbDetail({ jdtj_id: this.parentId });
      if (data) {
        this.detailInfo = data.one_info;
        this.year = this.detailInfo.jdtj_year.split(",");
      }
    },
    async getListData() {
      this.loading = true;
      let obj = {
        page: this.cur_page,
        limit: this.limit,
        bb_id: this.parentId,
      }
      let data = await dddx_ddrwList(obj);
      if (data) {
        this.tableData = data.page_data;
        this.cur_page = Number(data.page_now);
        this.total = Number(data.total_rows);
        this.loading = false;
      }
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = [...val];
    },
    handleDownload() {
      let arr = [];
      this.multipleSelection.map((value) => {
        arr.push(value.jdd_id);
      });
      let obj = {
        // page: this.cur_page,
        // limit: this.limit,
        bb_id: this.parentId,
        site: "jnmf",
        jdd_id: arr.join(","),
      };

      download("/jdbg/dddx_ddrw/index", obj);
    },
    handleClose(type) {
      this.dialogFormVisible = false;
      this.$refs.addAndEdit && this.$refs.addAndEdit.resetForm();
      if (type == 1) {
        this.initDetailInfo();
        this.getListData();
      }
    },
    formSubmit() {
      this.$refs.addAndEdit.commitData();
    },
  },
  created() {
    this.parentId = this.$route.params.id;
    this.initDetailInfo();
    this.getListData();

    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>