<template>
  <div>
    <el-card class="box-card">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :searchConfig="searchConfig"
        highlight-current-row
        @get-list="getList"
        @clickReset="clickReset">
        <template #imgUrl="{ item }">
          <div style="display: grid; place-items: center" v-if="item.imgUrl">
            <img
              v-lazy="item.imgUrl"
              style="cursor: pointer; border-radius: 5px; object-fit: contain; max-width: 100px; max-height: 100px"
              @click="openFile(item.imgUrl)" />
          </div>
        </template>
      </byTable>
    </el-card>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    artworkName: "",
    departmentId: proxy.useUserStore().user.deptId === "100" ? "" : proxy.useUserStore().user.deptId,
  },
});
const loading = ref(false);
//
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "artworkName",
      label: "图稿名称",
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "群组品号",
        slot: "imgUrl",
        width: 160,
        align: "center",
      },
    },
    {
      attrs: {
        label: "设计图",
        prop: "imgName",
      },
    },
    {
      attrs: {
        label: "图稿名称",
        prop: "artworkName",
      },
    },
    {
      attrs: {
        label: "图稿文件",
        prop: "fileName",
      },
    },
    {
      attrs: {
        label: "操作",
        width: 80,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "选择",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickSelect(row);
            },
          },
        ];
      },
    },
  ];
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/artworkLibrary/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const emit = defineEmits(["selectPic"]);
const clickSelect = (row) => {
  emit("selectPic", row);
};
const openFile = (path) => {
  window.open(path);
};
</script>

<style lang="scss" scoped></style>
