<template>
  <!-- 项目管控台 -->
  <div class="xmgktContent">
    <div class="d-flex flex-between">
      <div>
        <FileUpload :full.sync="fileData" @success="handleSuccess"></FileUpload>
        <!-- <el-popover placement="bottom-start" width="250" v-model="visible">
          <FileUpload v-if="visible" :full.sync="fileData" @success="handleSuccess"></FileUpload>
          <div style="text-align: right; margin: 0;margin-top: 20px;">
            <el-button size="mini" type="text" @click="visible = false">取消</el-button>
            <el-button type="primary" size="mini" @click="commitData">确定</el-button>
          </div>
          <el-button type="success" size="medium" slot="reference" icon="el-icon-plus">上传</el-button>
        </el-popover> -->
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="keyword">
          <el-input placeholder="请输入搜索关键字" v-model="searchForm.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 v-for="(item, index) in columnData" :key="index" :label="item.name" show-overflow-tooltip :prop="item.key" width="auto"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="lookDetail(scope.row)">详情</el-button>
          <el-button type="text" @click="delData(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 width="80%" :visible.sync="dialogVisible" title="查看" append-to-body :close-on-click-modal="false" @close="handleClose">
      <preview-attach :commonUrl="commonUrl" :filename="filename" style="width: 100%; height: 300px;"></preview-attach>
    </el-dialog>
  </div>
</template>

<script>
import { dddx_xnddpdList, dddx_xnddpdAdd, dddx_xnddpdDelete } from "./api";
import PreviewAttach from "@/components/PreviewAttach/index.vue";
import FileUpload from "./FileUpload.vue";
import { useUserStore } from "@/stores/user";
import { dealWithDate } from "@/utils/common-util.js";
export default {
  data() {
    return {
      userInfo: useUserStore(),
      searchForm: {
        keyword: "",
      },
      fileData: "",
      fileObj: {},
      loading: false,
      columnData: [
        {
          name: "名称",
          key: "jdxnpd_name"
        },
        {
          name: "上传人",
          key: "jdxnpd_scr"
        },
        {
          name: "上传时间",
          key: "jdxnpd_date"
        }
      ],
      multipleSelection: [],
      tableData: [],
      limit: 10,
      total: 0,
      cur_page: 1,
      dialogVisible: false,
      commonUrl: "",
      filename: "",
      visible: false,
    }
  },
  components: { FileUpload, PreviewAttach },
  methods: {
    async getListData() {
      this.loading = true;
      let obj = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.searchForm.keyword,
        site: "jnmf",
      }
      let data = await dddx_xnddpdList(obj);
      if (data) {
        this.tableData = data.page_data;
        this.cur_page = Number(data.page_now);
        this.total = Number(data.total_rows);
        this.loading = false;
      }
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = [...val];
    },
    delData(data) {
      if (data == undefined && this.multipleSelection.length == 0) {
        this.$message.error("请选择要删除的数据！");
        return false;
      }
      this.$confirm("是否确定删除当前数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = "";
          if (data != undefined) {
            res = await dddx_xnddpdDelete({ jdxnpd_id: data.jdxnpd_id });
          } else {
            let arr = [];
            this.multipleSelection.map((item) => {
              arr.push(item.jdxnpd_id);
            });
            res = await dddx_xnddpdDelete({ jdxnpd_id: arr });
          }
          if (res && res != "") {
            this.$message.success("删除成功！");
            this.getListData();
          }
        })
        .catch(() => { });
    },
    handleClose() {
      this.dialogVisible = false;
      this.commonUrl = "";
      this.filename = "";
    },
    lookDetail(data) {
      if (!data.jdxnpd_file) {
        this.$message.error("文件未上传！");
        return false;
      }
      this.dialogVisible = true;
      this.commonUrl = data.jdxnpd_file;
      this.filename = data.jdxnpd_name;
    },
    handleSuccess(fileList) {
      if (fileList[fileList.length - 1]) {
        this.fileObj = {
          name: fileList[fileList.length - 1].name,
          url: fileList[fileList.length - 1].part_url
        }
      }
      this.commitData();
    },
    async commitData() {
      let obj = {
        jdxnpd_name: this.fileObj.name,
        jdxnpd_file: this.fileObj.url,
        jdxnpd_scr: this.userInfo.real_name,
        jdxnpd_scr_user_id: this.userInfo.user_id,
        jdxnpd_date: dealWithDate(new Date()),
      }
      let data = {
        issubmit: '1',
        jdbg_dddx_xnddpd: obj
      };
      let res = await dddx_xnddpdAdd(data);
      if (res) {
        // this.$message.success("保存成功！");
        this.visible = false;
        this.fileData = "";
        this.getListData();
      }
    }
  },
  created() {
    this.getListData();
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-upload-list {
  display: none;
}
</style>