<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="addData">
          <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
          type="primary"
          size="medium"
          @click="exportAll"
          :disabled="multipleSelection.length == 0"
        >
          <span class="d-in-block v-mid font-size-16">导出</span>
        </el-button>
        <el-button
          type="danger"
          size="medium"
          @click="batchDel"
          :disabled="multipleSelection.length == 0"
        >
          <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="">
          <el-select
            v-model="filteTime"
            clearable
            placeholder="请选择"
            @change="getListData"
            style="width: 100%"
          >
            <el-option
              v-for="item in yearData"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="">
          <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
      :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
        label="报表名称"
        show-overflow-tooltip
        prop="bb_name"
      ></el-table-column>
      <el-table-column label="创建人" prop="create_user_name"></el-table-column>
      <el-table-column
        label="创建时间"
        prop="create_dateline"
      ></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="toDetail(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>

    <!-- dialogt弹出 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px">
      <el-form
        :model="dialogForm"
        ref="addForm"
        :rules="rules_check"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="报表名称" prop="bbmc">
          <el-input v-model="dialogForm.bbmc"></el-input>
        </el-form-item>
        <el-form-item label="选择年份" prop="year">
          <el-select
            v-model="dialogForm.year"
            multiple
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in yearData"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型选择">
          <el-checkbox-group v-model="showType" @change="handleCheckAllChange">
            <el-checkbox label="1">列表</el-checkbox>
            <el-checkbox label="2">图表</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取消</el-button>
        <el-button type="primary" :disabled="button" @click="formSubmit('addForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { bb_list, bb_add, bb_edit, bb_del, org_users, bbys } from "./api";
import { useUserStore } from "@/stores/user";
const { real_name, dept_ids, token } = useUserStore();
export default {
  name: "bbtj",
  data() {
    return {
      button:false,
      filteTime: "",
      keyword: "",
      tableData: [],
      type_value: "",
      type_list: [],
      shlxList: [],
      btlxList: [],
      multipleSelection: [],
      limit: 10,
      total: 12,
      cur_page: 1,
      yearData: [],
      dialogFormVisible: false,
      showType: [],
      dialogForm: {
        bbmc: "",
        year: "",
        valiType: [],
      },
      rules_check: {
        bbmc: [{ required: true, message: "请输入报表名称", trigger: "blur" }],
        year: [{ required: true, message: "请选择年份", trigger: "change" }],
        valiType: [
          {
            type: "array",
            required: true,
            message: "请选择类型",
            trigger: "change",
          },
        ],
      },
      title: "",
      isEdit: false,
      id: "",
      loading: false,
      userList: [],
      ids: [],
    };
  },
  methods: {
    getUserList() {
      let data = {
        dept_id: dept_ids[0],
        page: 1,
        limit: 9999,
        site: "jnmf",
      };
      org_users(data).then((res) => {
        if (res.code == 1) {
          this.userList = res.data.page_data;
        }
      });
    },
    getYear() {
      var myDate = new Date();
      var year = myDate.getFullYear();
      this.initSelectYear(year);
    },
    initSelectYear(year) {
      this.yearData = [];
      for (let i = 0; i < 30; i++) {
        this.yearData.push({ value: year - i, label: year - i + "年" });
      }
    },
    //初始化列表数据
    getListData() {
      this.loading = true;
      let transObj = {
        site: "jnmf",
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        bb_year: this.filteTime,
      };
      bb_list(transObj).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    timeClear(time) {
      if (!time) {
        time = "";
        this.getListData();
      }
    },
    //多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.ids = [];
      this.multipleSelection.forEach((item) => {
        this.ids.push(item.bb_id);
      });
    },
    //导出
    exportAll() {
      let ids = this.ids.join(",");
      // let link =
      //   window.globalVariables.api +
      //   "/jdbg/sbcg_cgxm/index?api=xls&page=1&limit=10000&token=" +
      //   token +
      //   "&ids=" +
      //   ids;
      let data = {
        site: "jnmf",
        limit: "999",
        page: "1",
        ids: ids,
        api:'xls'
      };
      bbys(data).then(res => {
        if (res.code == '1') {
          window.open(res.data.download_url, "_blank");
        }
      })
    },
    //批量删除
    batchDel() {
      let transObj = {
        bb_id: this.ids,
      };
      this.$confirm("确认删除选中项目?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          bb_del(transObj).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    //新建弹出
    addData() {
      this.button = false;
      this.title = "新建报表";
      this.showType = [];
      this.dialogForm = {
        bbmc: "",
        year: "",
        valiType: [], //1:列表 2:图表 3：全选
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    //多选框选择
    handleCheckAllChange(val) {
      this.showType = val;
      this.dialogForm.valiType = val;
      if (val.length == 2) {
        this.dialogForm.valiType = 3;
      }
    },
    editData(item) {
      this.title = "编辑设置";
      this.isEdit = true;
      this.id = item.cbs_id;
      this.dialogForm.gzmc = item.cbs_name;
      this.dialogForm.sqr = item.cbs_sqr;
      this.dialogForm.cjsj = item.cbs_cjsj;
      this.dialogForm.yydx = item.cbs_yydx;
      this.dialogForm.btrqlx = item.cbs_btrqlx;
      this.dialogForm.btgzlx = item.cbs_btgzlx;
      this.dialogForm.zxbtsj = item.cbs_zxbtsj;
      this.dialogForm.btsjje = item.cbs_btsjje;
      this.dialogForm.shr = item.cbs_shr;

      this.dialogFormVisible = true;
    },
    //新建-提交
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.button = true;
          if (this.showType.length == 0) {
            this.$message({
              message: "请至少勾选一项 类型选择~",
              type: "error",
            });
            return;
          } else {
            let transObj = {
              site: "jnmf",
              issubmit: 1,
              jdbg_sbcg_bbtj: {
                bb_name: this.dialogForm.bbmc,
                bb_year: this.dialogForm.year.join(","),
                bb_type: this.dialogForm.valiType,
                create_user_name: real_name,
              },
            };
            console.log(transObj);
            if (!this.isEdit) {
              bb_add(transObj).then((res) => {
                if (res.code == 1) {
                  this.$message({
                    message: "添加成功！",
                    type: "success",
                  });

                  this.dialogFormVisible = false;
                  this.getListData();
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
            } else {
              transObj.bb_id = this.id;
              shsz_edit(data).then((res) => {
                if (res.code == 1) {
                  this.$message({
                    message: "编辑成功！",
                    type: "success",
                  });

                  this.dialogFormVisible = false;
                  this.getListData();
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
            }
          }
        }
      });
    },
    formCancel() {
      this.dialogFormVisible = false;
    },
    //翻页
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    //跳转详情页
    toDetail(item) {
      let chooseID = item.bb_id;
      this.$router.push({ name: "sbcgsq_bbtj_bbxq", params: { id: chooseID } });
    },
  },
  mounted() {
    this.getYear();
    // this.getUserList();
    this.getListData();
  },
};
</script>

<style lang="scss" scoped>
.time-type-list {
  width: 100%;
  border-bottom: 1px solid #f3f3f3;
  margin-bottom: 20px;
  display: flex;

  li {
    position: relative;
    top: 2px;
    cursor: pointer;
    width: 50px;
    font-size: 14px;
    line-height: 30px;
    text-align: center;

    & + li {
      margin-left: 20px;
    }

    &.selected {
      color: #265cd4;
      border-bottom: 2px solid #265cd4;
    }
  }
}
</style>
