<template>
  <div>
    <div class="d-flex flex-between">
      <div>
        <el-button type="primary" size="medium" @click="downLoad" :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="qjy_zylb_id">
          <el-select class="mr10" v-model="qjy_zylb_id" @change="getListData" placeholder="请选择类别">
            <el-option v-for="item in lbList" :key="item.qjylb_id" :label="item.qjylb_zylbmc" :value="item.qjylb_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="qjy_scsj">
          <el-date-picker style="width: 100%" @change="getListData" v-model="qjy_scsj" value-format="yyyy-MM-dd" type="date" placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="qjy_zymc" clearable @clear="getListData">
          <el-input placeholder="请输入关键字搜索" v-model="qjy_zymc" 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" ref="multipleTable" @selection-change="handleSelectionChange" style="width: 100%">
      <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="qjy_zymc" width="auto">
      </el-table-column>
      <el-table-column label="资源类别" prop="qjy_zylb_mc" width="auto">
      </el-table-column>
      <el-table-column label="资源类型" width="150" prop="qjy_zylx"></el-table-column>
      <el-table-column label="资源大小" width="150" prop="qjy_zydx"></el-table-column>
      <el-table-column label="上传人" width="150" prop="qjy_scr"></el-table-column>
      <el-table-column label="上传时间" width="200" prop="qjy_scsj"></el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" @click="detailData(scope.row.qjy_id)">详情
          </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="addDialogFormVisible" width="580px">
      <el-form :model="dialogForm" label-width="100px">
        <el-form-item label="资源类别" prop="qjy_zylb_id">
          <el-select v-model="dialogForm.qjy_zylb_id" style="width: 100%" placeholder="请选择资源类别">
            <el-option v-for="item in lbList" :key="item.qjylb_id" :label="item.qjylb_zylbmc" :value="item.qjylb_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资源名称" prop="qjy_zymc">
          <el-input v-model="dialogForm.qjy_zymc" placeholder="请输入资源名称"></el-input>
        </el-form-item>
        <el-form-item label="" prop="qjy_wjmc">
          <FileUpload :limit="1" :full.sync="dialogForm.qjy_wjmc" :key="timestamp">
            <div slot="tip" class="el-upload__tip">
              <!-- 支持文件类型:doc(.docx)、.ppt(.pptx)、.xls(.xlsx)、.pps、.wps、.pdf、.txt文件格式。 -->
            </div>
          </FileUpload>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue";
import { zyxxlb_list, zyxxlb_del, zyxxlb_add, zylbgl_list, zyxxlb_edit, bbys } from "./api";
import Clipboard from "clipboard";
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();
export default {
  name: "index",
  components: {
    FileUpload,
    Clipboard,
  },
  data() {
    return {
      ids: [],
      time: "",
      lbList: [],
      tableData: [],
      keyword: "",
      qjy_zylb_id: "",
      qjy_scsj: "",
      qjy_zymc: "",
      limit: 10,
      total: 12,
      cur_page: 1,
      addDialogFormVisible: false,
      detailInfo: {},
      title: "",
      multipleSelection: [],
      id: "",
      loading: false,
      index: "",
      isEdit: false,
      dialogForm: {
        qjy_zymc: "",
        qjy_zylb_id: "",
        qjy_zylb_mc: ""
      },
      timestamp: Date.now(),

    };
  },
  methods: {
    getlbList() {
      let data = {
        page: 1,
        limit: 999,
        qjy_sfgx: "1"
      };

      zylbgl_list(data).then((res) => {
        this.lbList = res.data.page_data;
        let obj = {
          qjylb_id: "",
          qjylb_zylbmc: "全部",
        };
        this.lbList.unshift(obj);
      });
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        qjy_zylb_id: this.qjy_zylb_id,
        qjy_scsj: this.qjy_scsj,
        qjy_zymc: this.qjy_zymc
      };

      zyxxlb_list(data).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);
      });
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    addData() {
      this.title = "上传";
      this.dialogForm = {
        qjy_zymc: "",
        qjy_zylb_id: "",
        qjy_zylb_mc: ""
      };
      this.isEdit = false;
      this.addDialogFormVisible = true;
    },
    formSubmit() {
      this.lbList.forEach((item) => {
        if (item.qjylb_id == this.dialogForm.qjy_zylb_id) {
          this.dialogForm.qjy_zylb_mc = item.qjylb_zylbmc;
        }
      });
      let data = {
        qjy_zymc: this.dialogForm.qjy_zymc,
        qjy_zylb_id: this.dialogForm.qjy_zylb_id,
        qjy_zylb_mc: this.dialogForm.qjy_zylb_mc,
        qjy_zylx: this.dialogForm.qjy_zylx,
        qjy_zydx: this.dialogForm.qjy_zydx,
        qjy_wjmc: this.dialogForm.qjy_wjmc,
        qjy_wjdz: this.dialogForm.qjy_wjdz,
      };
      zyxxlb_add(data).then((res) => {
        if (res.code == 1) {
          this.$message({
            message: "上传成功！",
            type: "success",
          });

          this.addDialogFormVisible = false;
          this.getListData();
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    formCancel() {
      this.addDialogFormVisible = false;
    },
    delData() {
      this.$confirm("确认删除这些资源?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            qjy_id: this.multipleSelection,
          };

          zyxxlb_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => { });
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.ids = [];
      this.fileUrl = [];
      this.multipleSelection.forEach((item) => {
        if (item.qjy_wjdz) {
          console.log(item.qjy_wjdz, 2222);
          let a = item.qjy_wjdz.indexOf('|');
          let b = a != -1 ? item.qjy_wjdz.substring(0, a) : item.qjy_wjdz;
          console.log(a, b, 11123);
          this.ids.push(item.qjy_id);
          this.fileUrl.push(b);
        }
      });
      console.log(this.fileUrl, 123);
    },
    downLoad() {
      //  let link = window.globalVariables.api + "/upload/main/download_batch";
      let zipLink;
      let data = {
        site: "jnmf",
        files: this.fileUrl,
      };
      console.log(this.fileUrl, 999);
      bbys(data).then((res) => {
        zipLink = window.globalVariables.api + res.data.download_url;
        console.log(zipLink, 11111);
        console.log(window.globalVariables.api);
        let href = zipLink;
        window.open(href, "_blank");
      });
    },

    detailData(id) {
      this.$router.push({
        name: "zygx_detail",
        params: { id: id }
      });
    }
  },
  mounted() {
    this.getListData();
    this.getlbList()
  },
};
</script>

<style scoped>
::v-deep .el-dialog__body {
  border-top: 1px solid #f0f3f8;
}
</style>