<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>图片：
          </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" :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:[]
      },
      uploadFail(err) {
        console.log(err)
      },
      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 = ''
        }
        // if(this.imgList && this.imgList.length>0){
        //   this.imgList.map(item=>{
        //     if(item.response){
        //       this.form.image_id = item.response.id
        //     }else{
        //        this.form.image_id = item.id
        //     }
        //   })
        // }else{
        //   this.$message({
        //     type: 'warning',
        //     message: '请上传图片'
        //   })
        // }
        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>
