<template>
  <div class="app-container">
    <el-form :model="form" label-width="110px">
      <el-form-item label="Banner位置：">
        <el-select v-model="form.type" placeholder="请选择Banner位置">
          <el-option v-for="item in dict.type.show_the_location" :key="item.value" :label="item.label"
            :value="Number(item.value)" />
        </el-select>
      </el-form-item>
      <el-form-item label="Banner名称：">
        <el-input v-model="form.name" placeholder="请输入Banner名称" />
      </el-form-item>
      <el-form-item label="展示顺序：">
        <el-input-number v-model="form.sort" :min="1" />
      </el-form-item>
      <el-form-item label="跳转地址：">
        <el-radio-group v-model="form.forwardType">
          <el-radio :label="0">内部</el-radio>
          <el-radio :label="1">外部</el-radio>
        </el-radio-group>
        <el-select v-model="form.forwardLocation" v-if="form.forwardType === 0" placeholder="请选择跳转位置"
          style="margin: 15px 0 20px 0">
          <el-option v-for="item in dict.type.nav_address_url" :key="item.value" :label="item.label"
            :value="Number(item.value)" />
        </el-select>
        <el-input v-model="form.forwardUrl" placeholder="请输入跳转链接地址"></el-input>
      </el-form-item>
      <el-form-item label="时间：">
        <el-date-picker v-model="date" type="datetimerange" start-placeholder="开始时间" end-placeholder="结束时间"
          value-format="yyyy-MM-dd HH:mm:ss" />
      </el-form-item>
      <el-form-item label="图片上传：">
        <Upload listType="picture-card" :url="form.pic" @upload="getUpload" />
      </el-form-item>
    </el-form>
    <div class="form-btn">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="getSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import Upload from '@/components/Upload/index'
import { submit, detail, edit } from '@/api/content/banner'
export default {
  dicts: ['nav_address_url', 'show_the_location'],
  components: {
    Upload
  },
  data() {
    return {
      date: '',
      form: {
        sort: 1,
        forwardType: 0
      }
    }
  },
  watch: {
    date(val) {
      this.form.startTime = val[0]
      this.form.endTime = val[1]
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.getList()
    }
  },
  methods: {
    // 详情
    getList() {
      detail({
        id: this.$route.query.id
      }).then(res => {
        if (res.code === 0) {
          this.form = res.data
          this.date = [res.data.startTime, res.data.endTime]
        }
      })
    },
    // 上传
    getUpload(e) {
      this.form.pic = e.file
    },
    // 取消
    cancel() {
      this.$tab.closeOpenPage("/content/banner");
    },
    // 提交
    getSubmit() {
      if (this.$route.query.id) {
        edit(this.form).then(res => {
          if (res.code === 0) {
            this.$message.success('修改成功！')
            this.cancel()
          }
        })
      } else {
        submit(this.form).then(res => {
          if (res.code === 0) {
            this.$message.success('新增成功！')
            this.cancel()
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 500px;
}
.form-btn {
  margin-left: 110px;
}
</style>