<template>
  <div style="width:50%;">
    <el-form-item label="藏品名称：" prop="name">
      <el-input v-model="form.name" :maxlength="INPUTLENGTH * 5" show-word-limit></el-input>
    </el-form-item>

    <el-form-item label="藏品类型：">
      <el-radio disabled v-model="radio" label="选中且禁用">专题图库</el-radio>
    </el-form-item>

    <el-form-item label="藏品年代：">
      <el-input v-model="form.ageId" class="fadeDom"></el-input>
      <el-select v-model="form.ageId" placeholder="请选择" style="width:100%">
        <el-option v-for="item in ageList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="藏品种类：">
      <el-input v-model="form.textureId" class="fadeDom"></el-input>
      <el-select v-model="form.textureId" placeholder="请选择" style="width:100%">
        <el-option v-for="item in textureList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="所属博物馆：">
      <el-input v-model="form.museumId" class="fadeDom"></el-input>
      <el-select v-model="form.museumId" placeholder="请选择" style="width:100%">
        <el-option v-for="item in museumList" :key="item.id" :label="item.name" :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="是否显示：">
      <el-switch v-model="form.display"> </el-switch>
    </el-form-item>


    <el-form-item label="描述：">
      <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" v-model="form.description"></el-input>
    </el-form-item>

    <el-form-item label="封面图片：" prop="thumb">
      <div v-if="!form.museumId">请选择所属博物馆</div>
      <uploadThumb  v-else :data="{'type':'thumb','museumId':form.museumId}" :url="fileUrl" @update="data=>{form.thumb = data}" :thumb="form.thumb" />
    </el-form-item>


    <el-form-item label="图片：" prop="thumb">

      <div v-if="!form.museumId">请选择所属博物馆</div>
      <template v-else>

        <el-input v-model="form.thumb" class="fadeDom"></el-input>
        <ul class="tuku">
          <li v-for="(item, i) in filelist" :key="i">
            <div class="imgcon">
              <el-image class="img" :src="item.filePath" :fit="'cover'" :preview-src-list="[item.filePath]">
              </el-image>
              <div class="cover" v-if="item.isIndex">封面图片</div>
              <el-button @click="del(item, i)" size="mini" type="primary" icon="el-icon-delete" circle></el-button>
            </div>
          </li>
          <li>
            <uploadImg id="uli" :url="fileUrl" :data="{'type':'img','museumId':form.museumId}" @update="updateImg"
              :thumb="form.thumb" />
          </li>
        </ul>
      </template>

    </el-form-item>
  </div>
</template>

<script>
import uploadImg from "./plugin/uploadimg.vue";
import uploadThumb from "@/components/uploadThumb";

import { delImg } from "@/configue/api";

export default {
  props: ["data", "rule", "filelist", 'ageList', 'textureList', 'museumList'],
  components: { uploadImg, uploadThumb },
  data() {
    return {
      form: this.data,
      radio: "选中且禁用",
      fileUrl: this.$serverName + "cms/goods/upload",
    };
  },
  watch: {
  },
  methods: {
    del(item, i) {
      console.log(item);
      this.$confirm("删除后，图片将无法恢复，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delImg(
          {
            fileId: item.id,
          },
          () => {
            this.filelist.splice(i, 1);
          }
        );
      });
    },

    reupload(data, item) {
      try {

        if (item.isIndex == 1) {
          this.form.thumb = data.filePath;
        }

        this.filelist = this.filelist.map((sub) => {
          //匹配置为0
          if (item.id == sub.id) {
            sub = {
              ...sub,
              ...data,
            };
          }
          return sub;
        });
      } catch (e) {
        !e;
      }
      console.log(this.filelist);
    },

    updateImg(data) {
      console.log(data);

      this.filelist.push({
        ...data,
        isIndex: 0,
      });
    },
  },
};
</script>

<style lang="less" scoped>
@gap: 20px;

.tuku {
  display: flex;
  margin-bottom: @gap;
  flex-wrap: wrap;

  li {
    width: 180px;
    margin-right: @gap;
    margin-bottom: @gap;

    .imgcon {
      cursor: pointer;
      width: 100%;
      height: 180px;
      margin-bottom: @gap / 2;
      position: relative;
      background: #f0f0f0;
      overflow: hidden;

      .img {
        width: 100%;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
      }

      .cover {
        position: absolute;
        width: 100%;
        height: 40px;
        line-height: 40px;
        text-align: center;
        background: rgba(0, 0, 0, 0.8);
        color: #fff;
        top: 50%;
        left: 0;
        transform: translateY(-50%);
        pointer-events: none;
      }

      button {
        display: none;
        position: absolute;
        top: 10px;
        right: 10px;
      }

      &:hover {
        button {
          display: block;
        }
      }
    }

    .btn {
      display: flex;
      justify-content: center;
      width: 100%;
    }
  }
}
</style>
