<template>
  <el-dialog
    :title="'历史凭证列表'"
    :center="true"
    align="left"
    top="8vh"
    width="740px"
    @close="showModelThis = false"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    append-to-body
  >
    <el-card style="margin-top: -20px" class="modal-form-style">
      <ex-table
        v-loading="loading"
        :table="table"
        :data="tableData"
        :columns="columns"
        :page="pageInfo"
        :size="size"
        @page-curr-change="handlePageChange"
        @page-size-change="handleSizeChange"
        @screen-reset="
          pageInfo.curr = 1;
          parmValue.page = 1;
          searchList();
        "
        @screen-submit="
          pageInfo.curr = 1;
          parmValue.page = 1;
          searchList();
        "
      >
        <template #table-header="{ selection }">
          <div style="width: 100%">
            <el-row style="margin: -15px 0 0 0">
              <el-col
                :span="3"
                class="fr"
                style="width: 66px; padding: 0 0 0 10px"
              >
                <el-button
                  type="success"
                  :size="searchSize"
                  @click="resultList(selection)"
                >
                  保存
                </el-button>
              </el-col>

              <el-col
                :span="3"
                class="fr"
                style="width: 66px; padding: 0 0 0 10px"
              >
                <el-button
                  type="primary"
                  :size="searchSize"
                  @click="searchList"
                >
                  刷新
                </el-button>
              </el-col>
            </el-row>
          </div>
        </template>
        <template #proof_type="{ scope }">
          <el-tag
            :size="tablebtnSize"
            v-text="
              (options.find((item) => item.value == scope.row.proof_type) || {})
                .label || '--'
            "
          ></el-tag>
        </template>

        <template #proof_url="{ scope }">
          <div
            v-if="scope.row.proof_type === '1'"
            style="width: 20px; height: 20px"
            class="hover"
          >
            <i
              class="el-icon-video-camera"
              style="background: #000; color: #fff"
            ></i>
          </div>
          <div
            v-else-if="scope.row.proof_type === '2'"
            style="width: 20px; height: 20px"
            class="hover"
            v-viewer
          >
            <img
              :src="scope.row.proof_url"
              style="display: inline-block; width: 100%; height: 100%"
              alt=""
            />
          </div>
          <a v-else :href="scope.row.proof_url" download="凭证文件">点击下载</a>
        </template>
      </ex-table>
    </el-card>
  </el-dialog>
</template>

<script>
import asyncRequest from "@/apis/components/show-voucher-model";
import resToken from "@/mixins/resToken";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import columns from "./columns";
export default {
  name: "showVoucherModel",
  mixins: [resToken, mixinPage],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
  },
  props: ["showModel", "once", "id"],

  /**
   * 属性集合
   * @param {Boolean}       showModel             : 是否弹出弹窗        必填
   * @param {Boolean}       once                  : 是否只选一条        非必填
   */
  /**
   * 事件集合
   * @resultList             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      loading: true,
      showModelThis: false,
      options: [
        {
          value: "1",
          label: "视频",
        },
        {
          value: "2",
          label: "图片",
        },
        {
          value: "3",
          label: "其他",
        },
      ],
      parmValue: {
        spuCode: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        // _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columns,
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.supplierCode = [];
        this.searchList();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },

  methods: {
    restSearch() {
      this.supplierCode = [];
      this.parmValue = {
        spuCode: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.searchList();
    },
    async searchList() {
      this.loading = true;
          this.parmValue.spuCode=this.id
      const res = await asyncRequest.list(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
    resultList(selection) {
      let length = selection ? selection.length : 0;
      if (length === 0) {
        this.$message.warning("请选择历史凭证！");
        return;
      }
      if (this.once && length !== 1) {
        this.$message.warning("只能选择一条凭证！");
        return;
      }
      this.showModelThis = false;
      this.$emit("searchChange", JSON.parse(JSON.stringify(selection[0])));
    },
  },
};
</script>

<style>
.a {
  background: #000;
  color: #fff;
}
</style>