<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" 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="filteTime">
          <el-date-picker style="width: 100%" v-model="filteTime" type="date" placeholder="请选择时间">
          </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 :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" @click="formSubmit('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { bbtj_list, bbtj_add, bbtj_edit, bbtj_detail,bbtj_del,bbys,org_users } from "./itemApi";
import { useUserStore } from "@/stores/user";
const { real_name, dept_ids } = useUserStore();
export default {
  name: "bbtj",
  data() {
    return {
      filteTime: "",
      keyword: "",
      tableData: [
        {
          bbmc: "工作日夜班",
          cjr: "李华",
          cjsj: "2022/03/13",
        },{
          bbmc: "节假日加班",
          cjr: "李华",
          cjsj: "2022/03/13",
        },{
          bbmc: "重大事项加班",
          cjr: "李华",
          cjsj: "2022/03/13",
        },{
          bbmc: "科研标题文字文字",
          cjr: "李华",
          cjsj: "2022/03/13",
        },{
          bbmc: "科研标题文字文字",
          cjr: "李华",
          cjsj: "2022/03/13",
        },{
          bbmc: "科研标题文字文字",
          cjr: "李华",
          cjsj: "2022/03/13",
        },
      ],
      type_value: "",
      multipleSelection:[],
      limit: 10,
      total: 12,
      cur_page: 1,
      yearData:[{
          value: '2016',
          label: '2016年'
        }, {
          value: '2017',
          label: '2017年'
        }, {
          value: '2018',
          label: '2018年'
        }, {
          value: '2019',
          label: '2019年'
        }, {
          value: '2020',
          label: '2020年'
        },{
          value: '2021',
          label: '2021年'
        }, {
          value: '2022',
          label: '2022年'
        }, {
          value: '2023',
          label: '2023年'
        }, {
          value: '2024',
          label: '2024年'
        }, {
          value: '2025',
          label: '2025年'
        }, {
          value: '2026',
          label: '2026年'
        }, {
          value: '2027',
          label: '2027年'
        }, {
          value: '2028',
          label: '2028年'
        }, {
          value: '2029',
          label: '2029年'
        }, {
          value: '2030',
          label: '2030年'
      }],
      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: [],
    };
  },
  mounted() {
    // this.getUserList();
    this.getListData();
  },
  methods: {
    getUserList() {
      let data = {
        dept_id: dept_ids[0],
        page: 1,
        limit: 9999,
      };
      org_users(data).then((res) => {
        if (res.code == 1) {
          this.userList = res.data.page_data;
        }
      });
    },
    //初始化列表数据
    getListData() {
      this.loading = true;
      let transObj = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        bb_year:this.filteTime
      };
      bbtj_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);
      });
    },
    //多选
     handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //导出
    exportAll() {
      // let expId = [];
      //  this.multipleSelection.forEach(item => {
      //   expId.push(item.bb_id);
      //  })
      // let zipLink;
      // let data = {
      //   api:'xls',
      //   site: "jnmf",
      //   ids: expId.join(","),
      // };
      // bbys(data).then((res) => {
      //   zipLink = res.data.download_url;
      //   window.open(zipLink, "_blank");
      // });

      let expId = [];
      this.multipleSelection.forEach(item => {
        expId.push(item.bb_id);
      })
      let ids = expId.join(",");
      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 delId = [];
       this.multipleSelection.forEach(item => {
         delId.push(item.bb_id);
      })
      let transObj = {
        bb_id:delId
      }
      this.$confirm("确认删除选中项目?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          bbtj_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.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;
    },
    //新建-提交
    formSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.showType.length == 0) {
            this.$message({
              message: '请至少勾选一项 类型选择~',
              type: 'error'
            });
            return 
          } else {
            let transObj = {
              issubmit:1,
              jdbg_xmlxsb_tjbb: {
                bb_name: this.dialogForm.bbmc,
                bb_year: this.dialogForm.year.join(),
                bb_type:this.showType.join(),
                create_user_name:real_name,
              }
            };
            if (!this.isEdit) {
              bbtj_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: "xmlxsb_bbtj_xq", params: { id: chooseID } });
    },
  },
  
};
</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>
