<template>
  <div class="clearfix">
    <a-row>
      <a-col>商品大图</a-col>
      <a-col>
        <a-upload
          action="/rooms/cesGoods/modify"
          list-type="picture-card"
          :file-list="fileList"
          @preview="handlePreview"
          @change="handleChange"
        >
          <div v-if="fileList.length < 1">
            <a-icon type="plus" />
            <!-- <div class="ant-upload-text">Upload</div> -->
          </div>
        </a-upload>
      </a-col>
    </a-row>
    <a-row>
      <a-col>图片</a-col>
      <a-col>
        <a-upload
          action="#"
          list-type="picture-card"
          :file-list="fileListImgs"
          @preview="handlePreview"
          @change="handleChangeImgs"
        >
          <div v-if="fileList.length < 8">
            <a-icon type="plus" />
            <!-- <div class="ant-upload-text">Upload</div> -->
          </div>
        </a-upload>
      </a-col>
    </a-row>
    <a-button @click="submitImg">确认</a-button>
    <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>
  </div>
</template>

<script>
function getBase64(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}
import { httpAction, getAction } from "@/api/manage";
export default {
  inject:['modelData'],
  data() {
    return {
      previewVisible: false,
      previewImage: "",
      fileList: [],
      fileListImgs: [],
      model: {
        id: "",
        hotelId: 0,
        goodType: "",
        goodUnit: "",
        barCode: "",
        bid: null,
        name: "",
        sellingPrice: "",
        purchases: null,
        salesVolume: null,
        inventory: null,
        appState: false,
        canStoreCard: false,
        canIntegralPay: false,
      },
      url: {
        edit: "/rooms/cesGoods/modify",
      },
    };
  },
  created(){
    console.log(this.model);
  },
  methods: {
    getStartData(){
      this.model = this.modelData
      if (this.model.cover) {
        this.fileList.push(this.model.cover);
      }
      if (this.model.images) {
        this.fileListImgs = this.model.images.split(",");
      }
    },
    handleCancel() {
      this.previewVisible = false;
    },
    async handlePreview(file) {
      if (!file.url && !file.preview) {
        file.preview = await getBase64(file.originFileObj);
      }
      this.previewImage = file.url || file.preview;
      this.previewVisible = true;
    },
    handleChange({ fileList }) {
      console.log(fileList);
      this.fileList = fileList;
    },
    handleChangeImgs({ fileList }) {
      console.log(fileList);
      this.fileListImgs = fileList;
    },
    edit(record) {
      console.log(22222222, record);
      this.model = JSON.parse(JSON.stringify(record));
      if (this.model.cover) {
        this.fileList.push(this.model.cover);
      }
      if (this.model.images) {
        this.fileListImgs = this.model.images.split(",");
      }
      // console.log(this.filterType(this.treeData, record.goodType));
      // console.log(this.arr);
      this.visible = true;
      // getSelectList({
      //   id: this.model.id,
      // }).then((res) => {
      //   if (res.success) {
      //     this.members = res.result;
      //   }
      // });
    },
    //修改图片
    submitImg() {
      const that = this;
      that.confirmLoading = true;
      let httpurl = "";
      let method = "";
      httpurl += this.url.edit;
      method = "put";
      if (this.model.payFlag == 0) {
        this.model.payAmount = 0;
      }
      this.model.cover = this.fileList[0].thumbUrl;
      let arr=[] 
      this.fileListImgs.forEach(ele=>{
        arr.push(ele.thumbUrl)
      })
      this.model.images = arr.toString()
      httpAction(httpurl, this.model, method)
        .then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            that.$emit("ok");
          } else {
            that.$message.warning(res.message);
          }
        })
        .finally(() => {
          that.confirmLoading = false;
        });
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      debugger;
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
          } else {
            httpurl += this.url.edit;
            method = "put";
          }
          if (this.model.payFlag == 0) {
            this.model.payAmount = 0;
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
            });
        }
      });
    },
  },
};
</script>

<style>
/* you can make up upload button and sample style by using stylesheets */
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>
