<template>
  <div class="p2520">
    <div class="d-flex flex-between">
      <div class="title d-flex">
        <h3 class="font-size-18">{{ detailMsg.hybb_bbmc }}</h3>
        <span class="ml10 lh20">{{ detailMsg.hybb_ny }}</span>
      </div>
      <div>
        <el-button type="primary" size="medium" @click="editData">
          <span class="d-in-block v-mid font-size-16">编辑</span>
        </el-button>
        <el-button :disabled="multipleSelection.length == 0" type="primary" size="medium" @click="exportAll">
          <span class="d-in-block v-mid font-size-16">导出</span>
        </el-button>
      </div>
    </div>
    <el-table
      :data="hysList"
      tooltip-effect="dark"
      style="width: 100%"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="序号" align="center" type="index" width="100">
      </el-table-column>
      <el-table-column
        label="科室"
        show-overflow-tooltip
        prop="hysyd_ssks"
        width="200"
        min-width="150"
      >
      </el-table-column>
      <el-table-column label="发起人" width="200" prop="fqr"></el-table-column>
      <el-table-column
        label="会议主题"
        show-overflow-tooltip
        width="auto"
        prop="hysyd_hymc"
      ></el-table-column>
      <el-table-column label="时间" width="auto" prop="time"> </el-table-column>
    </el-table>
    <el-dialog title="编辑报表" :visible.sync="dialogFormVisible" width="580px">
      <el-form
        :model="dialogForm"
        :ref="dialogForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="报表名称" prop="hybb_bbmc">
          <el-input
            v-model="dialogForm.hybb_bbmc"
            placeholder="请输入报表名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="选择月份" prop="hybb_ny">
          <el-date-picker
            v-model="dialogForm.hybb_ny"
            format="yyyy-MM"
            type="month"
            value-format="yyyy-MM"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button :disabled="button" type="primary" @click="formSubmit(dialogForm)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();

import { bbtj_detail, hysyd, bbtj_edit } from "./api";
export default {
  name: "index",
  data() {
    return {
      button:false,
      dialogFormVisible: false,
      dialogForm: {
        hybb_bbmc: "",
        hybb_ny: "",
      },
      rules: {
        hybb_bbmc: [
          { required: true, message: "请填写报表名称", trigger: "submit" },
        ],
        hybb_ny: [
          {
            required: true,
            message: "请选择报表月份",
            trigger: "submit",
          },
        ],
      },
      multipleSelection: [],
      detailMsg: {},
      hysList: [],
    };
  },
  methods: {
    getDetail() {
      let data = {
        site: "jnmf",
        hybb_id: this.$route.params.bb_id,
      };
      bbtj_detail(data).then((res) => {
        if (res.code == "1") {
          this.detailMsg = res.data.one_info;
          this.getHysyd();
        }
      });
    },
    getHysyd() {
      let data = {
        site: "jnmf",
        page: "1",
        limit: "999",
        filter_date: this.detailMsg?.hybb_ny,
      };
      hysyd(data).then((res) => {
        if (res.code == "1") {
          this.hysList = res.data.page_data;
          res.data.page_data.forEach((item) => {
            item.time =
              item.hysyd_hysj_ks + "-" + item.hysyd_hysj_js.split(" ")[1];
          });
        }
      });
    },
    editData() {
      this.button = false;
      this.dialogFormVisible = true;
      this.dialogForm = {
        hybb_bbmc: this.detailMsg.hybb_bbmc,
        hybb_ny: this.detailMsg.hybb_ny,
      };
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.button = true;
          let data = {
            site: "jnmf",
            issubmit: "1",
            hybb_id: this.$route.params.bb_id,
            jdbg_hy_bb: {
              hybb_bbmc: this.dialogForm.hybb_bbmc,
              hybb_ny: this.dialogForm.hybb_ny,
            },
          };
          bbtj_edit(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "会议报表编辑成功！",
                type: "success",
              });
              this.dialogFormVisible = false;
              this.getDetail();
            }
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].hysyd_id);
      }
    },
    exportAll() {
      let link =
        window.globalVariables.api +
        "/jdbg/hy_hysyd/index?api=xls&page=1&limit=10000&site=jnmf&token=" +
        token +
        "&filter_date=" +
        this.detailMsg?.hybb_ny +
        "&hysyd_id=" +
        this.multipleSelection;
      window.open(link, "_blank");
    },
  },
  mounted() {
    this.getDetail();
  },
};
</script>
<style lang="scss" scoped>
.el-dialog {
  .el-input {
    width: 280px;
  }
}
</style>