<template>
  <div>
    <div class="flex-row" style="margin: 10px;">
      <div class="d-flex flex-v-mid">
        <div>{{detailInfo.bb_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>

    <el-dialog title="编辑" :visible.sync="dialogFormVisible" width="600px" @close="handleClose">
      <add-and-edit ref="addAndEdit" :currentId="parentId" :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 { tjbbDetail } from "./api";
import { download } from "@/utils/request";
import AddAndEdit from "./AddAndEdit.vue";
export default {
  data() {
    return {
      parentId: "",
      detailInfo: {},
      year: [],
      loading: false,
      columnData: [
        {
          name: "责任科室",
          key: "department_name"
        },
        {
          name: "项目类别",
          key: "xm_type_name"
        },
        {
          name: "项目完成数量",
          key: "xm_wc_num"
        },
        {
          name: "项目预算(万元)",
          key: "ys"
        },
        {
          name: "中标金额(万元)",
          key: "zbje"
        },
        {
          name: "实际支付(万元)",
          key: "sjzf"
        }
      ],
      tableData: [],
      multipleSelection: [],
      dialogFormVisible: false,
      selectObj: {
        bb_type: {
          option: [
            { v: "1", n: "采购项目" },
            { v: "2", n: "工程项目" },
            { v: "3", n: "培训项目" },
            { v: "4", n: "服务项目" }
          ]
        }
      },
      yearData: [],
    }
  },
  components: { AddAndEdit },
  methods: {
    async initDetailInfo() {
      this.loading = true;
      let data = await tjbbDetail({ bb_id: this.parentId });
      if (data) {
        this.detailInfo = data.one_info;
        this.year = this.detailInfo.bb_year.split(",");
        this.tableData = data.one_info.xm_list;
        this.loading = false;
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = [...val];
    },
    handleDownload() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("请先选择数据！");
        return
      }
      let arr = [];
      this.multipleSelection.map((value) => {
        arr.push(value.xm_id);
      });
      let obj = {
        bb_id: this.parentId,
        site: "jnmf",
        ids: arr.join(","),
        api: "xls"
      };

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

    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>