<template>
  <div>
    <xy-dialog ref="dialog" :width="70" :is-show.sync="isShow" :type="'form'"
      :title="type === 'add' ? '新增轮播图' : '编辑轮播图'" :form="form" :rules='rules' @submit="submit">
      <template v-slot:name>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>标题：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.name" placeholder="请输入标题" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:position>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>显示位置：
          </div>
          <div class="xy-table-item-content">
            <el-select v-model="form.position" placeholder="请选择显示位置" clearable style="width: 100%;">
              <el-option v-for="item in position_options" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:jump_type>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>跳转类型：
          </div>
          <div class="xy-table-item-content">
            <el-select v-model="form.jump_type" placeholder="请选择跳转类型" clearable style="width: 100%;">
              <el-option v-for="item in type_options" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:jump_url>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>跳转链接：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.jump_url" placeholder="请输入跳转链接" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:sort>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>排序：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.sort" placeholder="请输入排序" clearable style="width: 100%;"></el-input>

          </div>
        </div>
      </template>
      <template v-slot:image_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold;">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>图片（750*400）：
          </div>
          <div class="xy-table-item-content">
            <el-upload :action="action" class='upload-demo' :limit="1" list-type="picture-card" :file-list="imgList"
              ref="pictureUpload" :before-upload="beforeUpload" :auto-upload="true" :on-success="uploadSuccess"
              :on-remove="uploadRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </div>
      </template>
    </xy-dialog>

  </div>
</template>

<script>
  import {
    show,
    save
  } from "@/api/info/banners.js"
  export default {
    components: {

    },
    data() {
      return {
        isShow: false,
        type: 'add',
        action: `${process.env.VUE_APP_UPLOAD_API}`,
        id: '',
        type_options: [],
        position_options: [],
        form: {
          name: '',
          position: '',
          jump_type: '',
          jump_url: '',
          sort: 0,
          image_id: '',
        },
        imgList: [],
        rules: {
          name: [{
            required: true,
            message: '请输入标题'
          }],
          position: [{
            required: true,
            message: '请选择显示位置'
          }],
          image_id: [{
            required: true,
            message: '请上传图片'
          }]
        }
      }
    },
    created() {},
    methods: {
      setOptions(position, type) {
        this.position_options = position ? position : []
        this.type_options = type ? type : []
      },
      beforeUpload(file) {
        console.log(file)
        const isImage = file.type.includes('image');
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isImage) {
          this.$message.error('请上传正确的图片格式文件');
        }
        if (!isLt2M) {
          this.$message.error('上传文件大小不能超过2MB');
        }
        return isImage && isLt2M;
      },
      uploadSuccess(response, file, fileList) {
        console.log(response, file, fileList)
        this.form.image_id = response.id
        this.imgList = fileList
      },
      uploadRemove(file, fileList) {
        this.imgList = fileList
        this.form.image_id = ''
      },
      submit() {
        if (this.id) {
          this.form.id = this.id
        }
        if (this.type == 'add') {
          this.form.id = ''
        }
        save({
          ...this.form
        }).then(res => {
          this.$message({
            type: 'success',
            message: this.type === 'add' ? '新增轮播图成功' : '编辑轮播图成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },

      getDetail() {
        show({
          id: this.id,
          show_relation: ['image']
        }).then(res => {
          this.form = this.base.requestToForm(res, this.form)
          this.imgList.push(res.image)
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.imgList = []
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .image_id {
    flex-basis: 100%;
  }
</style>
