<template>
  <Search :tag="tag" :type="'FILE'" ref="searchRef"></Search>
  <div style="background-color: #f0f2f5" class="dark:bg-dark-900">
    <div class="datacenter-right">
      <div class="resource_list" id="map_list">
        <div v-for="(i, n) in list" v-if="list.length" :key="n" data-permission="true" class="resource_item"
          data-checking="false" data-searching="1" data-ispub="1">
          <div class="mapItem-top-box">
            <div class="item-top">
              <div class="img_container" data-num="1">
                <img class="tab-list-icon-img-MR00001936 img_mr MR00001936" :id="i.SERVICEID + n" :src="setHtmlImg(i, n)"
                  alt="文件预览图">
              </div>
            </div>
            <div class="item-title">
              <span class="r_name">{{ i.SERVICENAME }}</span>
              <!-- <span class="r-number" data-num="1" title="编目1次">1</span> -->
            </div>
            <div class="item-msg">
              <div class="item-msg-val">坐标系：<span>{{ i.CRS }}</span></div>
              <div class="item-msg-val">适用流程：<span>国家秘密和工作秘密数据成果申请,</span></div>
              <div class="item-msg-val">关键字：
                <span :id="'KEYWORDS' + i.SERVICEID + n">{{ getTag(i.KEYWORDS, `KEYWORDS${i.SERVICEID}${n}`) }}</span>
              </div>
              <div class="item-msg-val">服务类型：<span>{{ i.TYPENAME }}</span></div>
            </div>
          </div>
          <div class="operation-box">
            <div class="left">
              <div class="operation-item browse-item-btn browse-item-MR00001936">
                <a target="_blank"
                  :href="(i.info && i.info.length && i.info[0].thumbnail) || './static/img/default-dr3.jpg'">
                  <span>浏览</span>
                </a>
              </div>
              <div class="operation-item" @click="handleQuery(i, n)">
                <a href="javascript:void(0)" class="">
                  <span>详细</span>
                </a>
              </div>
            </div>
            <div class="right">
              <div class="operation-item" v-if="i.SFJRSQK === '未加入'" @click="applyWay(i)">
                <a href="javascript:void(0)" class="">
                  <span>加入申请库</span>
                </a>
              </div>
              <div class="operation-item" v-else style="cursor: not-allowed;background-color: #e8e8e8;">
                <a href="javascript:void(0)" class="" style="cursor: not-allowed;color:#656363;">
                  <span>{{ i.SFJRSQK == '已加入' ? '已加入申请库' : i.SFJRSQK }}</span>
                </a>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <MyPage :total="total"></MyPage>
    <!-- 弹出框 -->
    <DetailModal v-if="showResDeatil" :type="'FILE'" @closeModal="showResDeatil = false" :resId="detailResId" />
  </div>
</template>
<script>
import { defineComponent, nextTick, onMounted, ref, defineProps, watch } from 'vue';
// 导入子页面【详情】
import DetailModal from './child/DetailModal.vue';
import Search from './child/Search.vue';
import { platList, img, apply, tagAjax } from '/@/api/resource/plat';
import { session } from '/@/utils/Memory';
import { message } from 'ant-design-vue';
import MyPage from './child/MyPage.vue';
import { setHtmlImg } from '/@/views/minWidgets/CommonWay.js';

const props = {
  listData: {
    type: Object,
    default: () => { },
  }
}

export default defineComponent({
  name: 'FileData',
  components: { Search, DetailModal, MyPage },
  props,
  emits: ['resAddToCar'],
  setup(props, { emit }) {
    const showResDeatil = ref(false)
    const searchRef = ref(null)
    const list = ref([]);
    const detailResId = ref('')

    const total = ref(0);
    const tag = ref('');
    onMounted(() => getList(props.listData));
    async function getList(res) {
      console.log("文件传进来的文件资源:", res)
      total.value = res.count;
      tag.value = res.keywords;
      var res = res.items;
      var val = JSON.parse(JSON.stringify(res))
      list.value = val;
    }
    watch(() => props.listData, (val) => getList(val), {
      deep: true,
      immediate: true
    })

    //获取资源标签
    function getTag(i, id) {
      //判断是不是数组
      i = i.indexOf('[') > -1 ? JSON.parse(i) : [i];
      if (i.length) {
        tagAjax().then(r => {
          var name = r.filter(j => i.includes(j.code)).map(j => j.name).toString();
          document.querySelector(`#${id}`).innerHTML = name;
        })
      } else {
        return ''
      }
    }

    function applyWay(i) {
      console.log(i)
      apply({
        addRes: [{
          resDataType: "2",
          resId: i.SERVICEID,
          resName: `${i.SERVICENAME}(${i.CRS})`,
          resType: 0,
          workflowType: "FILE",
        }],
        userId: session.getItem('userId'),
      }).then((r) => {
        if (r.datas && r.resp_code == 0) {
          message.success('申请成功');
          // searchRef.value.getResData();
          eventBus.emit('addResToCarEventBus')
          emit('resAddToCar')
        }
      })
    }

    function handleQuery(record, n) {
      //打开详情弹窗
      session.setItem('thumbnail2', document.getElementById(record.SERVICEID + n).src);
      detailResId.value = record.SERVICEID
      showResDeatil.value = true
    }

    return {
      setHtmlImg,
      getTag,
      tag,
      total,
      showResDeatil,
      detailResId,
      searchRef,
      list,
      applyWay,
      handleQuery,
    };
  },
});
</script>

<style scoped>
.datacenter-right .resource_list .item-title .r_name {
  color: #5e5d5e;
  display: inline-block;
  width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: Source Han Sans CN;
  font-size: 16px;
  font-weight: bold;
  line-height: 16.38px;
  letter-spacing: 0px;
  color: #333333;
}

.datacenter-right .resource_list {
  box-sizing: border-box;
  width: 100%;
  float: left;
  clear: both;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.datacenter-right .resource_list>div:not(:nth-of-type(4n + 4)) {
  margin-right: 7px;
}

.resource_item {
  height: 318px;
  width: 224px;
  margin-bottom: 14px;
  border: 2px dashed transparent;

  width: 386px;
  height: 402px;
  border-radius: 4px;
  opacity: 1;
  background: #FFFFFF;
  border: 1px solid #DEDEDE;
}

.mapItem-top-box {
  height: 350px;
}

.datacenter-right .resource_list .operation-box {
  height: 42px;
  display: flex;
  margin-bottom: 10px;
  /* margin-left: 20px; */
  justify-content: space-between;
  padding: 0 24px;

  .left {
    display: flex;

    .operation-item {
      display: flex;
      align-items: center;
      justify-content: center;
      min-width: 50px;
      height: 34px;
      opacity: 1;
      margin-right: 22px;
      padding: 0 10px;
      background: #E8E8E8;

      a {
        font-size: 12px;
        width: 100%;
        text-align: center;
        color: #333333;
      }
    }
  }

  .right {
    .operation-item {
      display: flex;
      align-items: center;
      justify-content: center;
      min-width: 50px;
      height: 34px;
      opacity: 1;
      padding: 0 10px;
      background: #0671DD;

      a {
        font-size: 12px;
        width: 100%;
        text-align: center;
        color: #fff;
      }
    }
  }
}

.datacenter-right .resource_list .item-top {
  padding: 10px 16px 10px;
}

.datacenter-right .resource_list .item-top img {
  width: 360px;
  height: 190px;
  border: solid 1px #eeebeb;
}

.datacenter-right .resource_list .item-title {
  font-size: 14px;
  font-family: PingFang SC;
  font-weight: bold;
  color: #5e5d5e;
  line-height: 16px;
  padding: 0 0 0 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.datacenter-right .resource_list .item-msg {
  padding: 0 13px 13px;
}

.img_container {
  text-align: center;
}

.img_container {
  width: 100%;
  height: 100%;
  background: #b7bed3;
  border-radius: 4px;
}

.datacenter-right .resource_list .item-msg-val {
  font-size: 12px;
  font-family: PingFang SC;
  font-weight: bold;
  color: #888888;
  line-height: 26px;
  /* opacity: 0.5; */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-msg-val>span {
  font-family: Source Han Sans CN;
  font-size: 14px;
  font-weight: normal;
  line-height: 16.38px;
  letter-spacing: 0px;
  color: #333333;
}
</style>
