<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="createNew">
          <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
          :disabled="multipleSelection.length == 0"
          type="primary"
          size="medium"
          @click="exportAll"
        >
          <span class="d-in-block v-mid font-size-16">导出</span>
        </el-button>
        <el-button
          :disabled="multipleSelection.length == 0"
          type="danger"
          size="medium"
          @click="delData"
        >
          <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="state_value">
          <el-date-picker
            @change="getListData"
            v-model="state_value"
            format="yyyy-MM"
            type="month"
            value-format="yyyy-MM"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input
            placeholder="请输入搜索关键字"
            v-model="keyword"
            clearable
            @clear="getListData"
          >
          </el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData"
          >搜索
        </el-button>
      </el-form>
    </div>
    <el-table
      v-loading="loading"
      :data="applyTable"
      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="hybb_bbmc"
        width="200"
        min-width="150"
      >
      </el-table-column>
      <el-table-column
        label="创建人"
        width="200"
        prop="hybb_cjr"
      ></el-table-column>
      <el-table-column
        label="创建时间"
        width="auto"
        prop="hybb_cjsj"
      ></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <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="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 { real_name, dept_ids, token } = useUserStore();
import FileUpload from "@/components/FileUpload/index.vue";
import { bbtj_list, bbtj_add, bbtj_del, bbys } from "./api";
export default {
  name: "index",
  components: {
    FileUpload,
  },
  data() {
    return {
      button:false,
      loading: false,
      state_value: "",
      keyword: "",
      title: "",
      applyTable: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      years: [],
      dialogFormVisible: false,
      dialogSign: false,
      dialogForm: {
        hybb_bbmc: "",
        hybb_ny: "2022",
      },
      isEdit: false,
      multipleSelection: [],
      rules: {
        hybb_bbmc: [
          { required: true, message: "请填写报表名称", trigger: "submit" },
        ],
        hybb_ny: [
          {
            required: true,
            message: "请选择报表月份",
            trigger: "submit",
          },
        ],
      },
    };
  },
  methods: {
    getbbtjList() {
      this.loading = true;
      let data = {
        page: 1,
        limit: 999,
        site: "jnmf",
      };
      bbtj_list(data).then((res) => {
        if (res.code == "1") {
          this.applyTable = res.data.page_data;
          this.loading = false;
        }
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        site: "jnmf",
        page: this.cur_page,
        limit: this.limit,
        hybb_bbmc: this.keyword,
        hybb_ny: this.state_value,
      };
      bbtj_list(data).then((res) => {
        this.applyTable = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
        this.loading = false;
      });
    },
    exportAll() {
      let link = window.globalVariables.api + "jdbg/hy_bb/zip";
      let zipLink;
      let data = {
        site: "jnmf",
        hybb_ids: this.multipleSelection.join(","),
      };
      bbys(data).then((res) => {
        zipLink = res.data.files;
        console.log(window.globalVariables.api);
        let href = window.globalVariables.api + zipLink;
        window.open(href, "_blank");
      });
    },
    delData() {
      this.$confirm("确认删除这些报表?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            site: "jnmf",
            hybb_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(() => {});
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    handleSelectionChange(val) {
      this.multipleSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].hybb_id);
      }
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    createNew() {
      this.button = false;
      this.isEdit = false;
      this.title = "新建报表";
      this.dialogForm = {
        hybb_bbmc: "",
        hybb_ny: "",
      };
      this.dialogFormVisible = true;
    },
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.button = true;
          let data = {
            site: "jnmf",
            issubmit: "1",
            jdbg_hy_bb: {
              hybb_bbmc: this.dialogForm.hybb_bbmc,
              hybb_ny: this.dialogForm.hybb_ny,
            },
          };
          bbtj_add(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "会议报表新建成功！",
                type: "success",
              });
              this.dialogFormVisible = false;
              this.getListData();
            }
          });
        }
      });
    },
    detailData(row) {
      this.$router.push({
        name: "hysyd_bbtj_bbxq",
        params: {
          bb_id: row.hybb_id,
        },
      });
    },
  },
  mounted() {
    // this.getUserList();
    this.getbbtjList();
    // this.getListData();
  },
};
</script>
<style lang="scss" scoped>
.el-dialog {
  .el-input {
    width: 280px;
  }
}
</style>