<template>
  <el-dialog
    title="Banner详情"
    width="50%"
    :visible.sync="modal"
    :close-on-click-modal="false"
    @close="$emit('cancel')"
  >
    <divider-container title="基本信息" />

    <el-form ref="ruleForm" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="banner名称" prop="name"><el-input v-model="form.name" style="max-width: 300px;" autocomplete="off" /></el-form-item>
      <el-form-item label="绑定链接"><el-input v-model="form.url" style="max-width: 600px;" autocomplete="off" /></el-form-item>
      <el-form-item label="banner图片" prop="imgFile"><upload v-model="form.imgFile" params="prefix=/banner" /></el-form-item>
      <el-form-item label="置顶状态"><el-checkbox v-model="form.isTop">置顶</el-checkbox></el-form-item>
      <el-form-item label="展示状态"><el-checkbox v-model="form.isShow">展示</el-checkbox></el-form-item>
      <el-form-item label="排序（倒序）" prop="sort"><el-input-number v-model="form.sort" controls-position="right" :min="0" :max="10000" /></el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit('ruleForm')">保存</el-button>
      <!-- <el-button v-if="modalType === 'edit'" type="danger" @click="handleDelete">删除</el-button> -->
      <el-button @click="modal = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import { setStatus } from '@/utils'
import EMITKEY from '@/const/emitNameKey'
import { UPLOAD_URL_DEMO } from '@/const/urlKey'
import { getItem, saveItem } from '@/model/banner'
import { cutUrl } from '@/utils'

export default {
  name: 'BannerItem',

  props: {
    id: {
      type: String,
      default: ''
    },
    modalType: {
      type: String,
      default: ''
    }
  },

  data() {
    const validateImg = (rule, value, callback) => {
      if (!Array.isArray(value)) return callback(new Error('数据出错，请重新上传'))
      if (value.length < 1) return callback(new Error('请先上传图片'))

      return callback()
    }

    return {
      modal: true,

      form: {
        name: '',
        url: '',
        imgFile: [{ url: UPLOAD_URL_DEMO }],
        isTop: false,
        isShow: false,
        sort: null
      },
      rules: {
        name: [{ required: true, message: '请输入banner标题', trigger: 'blur' }],
        imgFile: [{ required: true, validator: validateImg, trigger: 'change' }],
        sort: [{ required: true, message: '请输入优先度(1~9)', trigger: 'blur' }]
      },

      val: []
    }
  },

  watch: {
    'form.imgFile': {
      handler(val) {
        this.$refs['ruleForm'].validateField('imgFile')
      },
      deep: true
    }
  },

  mounted() {
    this.id && this.loadData()
  },

  methods: {

    async loadData() {
      const { success, data, msg } = await getItem({
        id: this.id
      })
      if (success) {
        Object.assign(this.form, data)
      } else {
        this.$message.error(msg)
      }
    },

    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveData()
        }
      })
    },

    async saveData() {
      const { success, msg } = await saveItem({
        id: this.id,
        data: {
          title: this.form.name,
          url: this.form.url,
          imgUrl: cutUrl(this.form.imgFile[0].url),
          isTop: this.form.isTop,
          isShow: this.form.isShow,
          sort: this.form.sort
        }
      })
      if (success) {
        this.$message.success('操作成功~')
        this.bus.$emit(EMITKEY.BANNERRELOAD)
        this.modal = false
      } else {
        this.$message.error(msg)
      }
    }
  }
}
</script>

<style scoped>
.mt-60 {
  margin-top: 60px;
}
.btn-right {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
.mcol-20 .el-row {
  font-size: 14px;
  line-height: 1.7;
  padding-top: 14px;
}
.search-box {
  border: 1px #DCDEE2 solid;
  padding: 5px;
}
.search-box .search-input {
  margin: 0 5px;
  max-width: 200px;
}
</style>
