<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="文件名称" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
          clearable
        />
      </el-form-item>
      <el-form-item label="分享状态" prop="shareStatus">
        <el-select
          v-model="queryParams.shareStatus"
          placeholder="请选择分享状态"
          clearable
        >
          <el-option
            v-for="item in dict.type.share_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <div class="mb8">
      <el-button
        type="primary"
        plain
        size="mini"
        :disabled="multiple"
        @click.native="handleMultiVerify"
        icon="el-icon-s-check"
        >批量审核</el-button
      >
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </div>
    <el-table
      v-loading="loading"
      :data="shareList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="文件名称"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <div class="fileName">
            <img :src="$file.setFileImg(scope.row)" />
            <el-link @click="handleOpenFile(scope.row)">
              {{ scope.row.fileName }}
            </el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="分享人"
        align="center"
        prop="userName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="分享类型"
        align="center"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.share_type"
            :value="scope.row.shareType"
          />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.shareStatus == shareStatusConstant.REJECTED">
            <el-tooltip
              class="item"
              effect="dark"
              :content="'驳回原因：' + scope.row.verifyDesc"
              placement="top-start"
            >
              <el-tag :type="getShareStatusTagType(scope.row.shareStatus)"
                ><dict-tag
                  :options="dict.type.share_status"
                  :value="scope.row.shareStatus"
              /></el-tag>
            </el-tooltip>
          </span>
          <span v-else>
            <el-tag :type="getShareStatusTagType(scope.row.shareStatus)"
              ><dict-tag
                :options="dict.type.share_status"
                :value="scope.row.shareStatus"
            /></el-tag>
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="审核意见"
        align="center"
        prop="verifyDesc"
        :show-overflow-tooltip="true"
      /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <span
            style="margin-right: 8px"
            v-if="scope.row.shareStatus == shareStatusConstant.IS_REVIEW"
          >
            <el-link
              type="primary"
              icon="el-icon-check"
              style="margin-right: 16px"
              @click="
                handleAudit(shareStatusConstant.SHARED, scope.row.shareId)
              "
              >通过</el-link
            >
            <el-popover
              placement="bottom"
              width="300"
              trigger="click"
              ref="popoverRef"
            >
              <el-form :model="form" size="mini">
                <el-form-item label="驳回意见：">
                  <el-input
                    placeholder="请填写驳回意见（非必填）"
                    v-model="form.verifyDesc"
                  ></el-input>
                </el-form-item>
                <div style="display: flex; justify-content: flex-end">
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleCloseRejectPopover"
                    >取消</el-button
                  >
                  <el-button
                    size="mini"
                    type="primary"
                    @click="
                      handleAudit(
                        shareStatusConstant.REJECTED,
                        scope.row.shareId
                      )
                    "
                    >确认驳回</el-button
                  >
                </div>
              </el-form>
              <el-link
                slot="reference"
                type="danger"
                icon="el-icon-close"
                style="margin-right: 8px"
              >
                驳回</el-link
              >
            </el-popover>

            <el-divider
              direction="vertical"
              style="margin-right: 4px"
            ></el-divider>
          </span>

          <el-link
            type="primary"
            icon="el-icon-view"
            @click="handleOpenFile(scope.row)"
          >
            预览
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.currentPage"
      :limit.sync="queryParams.pageCount"
      @pagination="getList"
    />
    <el-dialog title="批量审核" :visible.sync="multiVerifyVisible" width="40%">
      <div class="multi-verify">
        <el-form :model="form" size="mini">
          <el-form-item>
            <div class="text">
              一共选择了{{ shareIdsArr.length }}条数据，请选择操作：
            </div>
          </el-form-item>
          <el-form-item>
            <el-radio-group v-model="multiVerifyRadio">
              <el-radio :label="shareStatusConstant.SHARED">批量同意</el-radio>
              <el-radio :label="shareStatusConstant.REJECTED"
                >批量驳回</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-input
              placeholder="请填写驳回意见（非必填）"
              v-model="form.verifyDesc"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div class="buttons">
              <el-button
                type="primary"
                icon="el-icon-check"
                @click="handleSubmitMultiVerfy"
                >确定</el-button
              >
              <el-button type="text" @click="handleCloseMultiVerify"
                >取消</el-button
              >
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAdminShareList, verifyShare } from "@/api/user/share";
import { shareStatusConstant } from "./constant";

export default {
  name: "myShare",
  dicts: ["share_type", "share_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      shareIdsArr: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      shareList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        currentPage: 1,
        pageCount: 10,
        fileName: "", //文件名称
        shareStatus: "", //分享状态
      },
      // 分享状态常量
      shareStatusConstant,
      // 驳回意见弹窗可见性
      popoverVisible: false,
      // 驳回的弹出框中的表单
      form: {
        verifyDesc: "", //驳回意见
      },
      // 批量审核的弹窗
      multiVerifyVisible: false,
      // 批量审核弹窗的默认值，批量同意或批量驳回
      multiVerifyRadio: shareStatusConstant.SHARED,
    };
  },
  computed: {
    getShareStatusTagType(shareStatus) {
      return (shareStatus) => {
        switch (shareStatus) {
          case shareStatusConstant.SHARED: //已分享
            return "success";
          case shareStatusConstant.IS_REVIEW: //审核中
            return "primary";
          case shareStatusConstant.REVOKED: //已撤销
            return "info";
          case shareStatusConstant.REJECTED: //已驳回
            return "danger";
        }
      };
    },
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      getAdminShareList(this.queryParams).then((res) => {
        this.total = res.data.total;
        this.loading = false;
        this.shareList = res.data.records;
      });
    },
    // 关闭驳回弹窗
    handleCloseRejectPopover() {
      this.$refs.popoverRef.doClose();
    },
    // 清空驳回表单
    handleClearRejectInput() {
      this.form.verifyDesc = "";
    },
    // 审核分享
    async handleAudit(verifyStatus, shareIds) {
      this.loading = true;
      const params = {
        endTime: "", //
        shareUserId: "", //
        verifyDesc: this.form.verifyDesc,
        verifyStatus,
        shareIds, //分享ID
      };
      let res = await verifyShare(params);
      this.loading = false;
      if (res.code == 200) {
        this.$message.success("操作成功！");
        this.getList();
        this.handleClearRejectInput();
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.shareIdsArr = selection.map((item) => item.shareId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 点击批量审核
    handleMultiVerify() {
      this.multiVerifyVisible = true;
      console.log(this.shareIds);
    },
    // 关闭批量审核弹窗
    handleCloseMultiVerify() {
      this.multiVerifyVisible = false;
      this.handleClearRejectInput();
    },
    handleSubmitMultiVerfy() {
      const shareIds = this.shareIdsArr.join(",");
      this.handleAudit(this.multiVerifyRadio, shareIds);
      this.handleCloseMultiVerify();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("queryForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.currentPage = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消分享
    handleCancelShare(row) {
      console.log(row);
    },
    // 点击打开文件
    handleOpenFile(file) {
      this.$file.handleFileNameClickNew(file,this.$fileKeys.zysShare);
    },
  },
};
</script>
<style scoped lang="scss">
.fileName {
  display: flex;
  justify-content: center;
  align-items: center;
  img {
    width: 18px;
    height: 18px;
    object-fit: contain;
    margin-right: 4px;
  }
}
.text {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 8px;
  display: flex;
  justify-content: center;
}
.buttons {
  display: flex;
  justify-content: flex-end;
}
</style>
