<template>
  <div id="app" class="app-container">
    <el-form ref="form" :model="form">
      <!-- <el-form-item :label-width="formLabelWidth" label="业务名称">
        <el-input v-model="form.serviceName"/>
      </el-form-item>-->
      <el-form-item :label-width="formLabelWidth" label="权重">
        <el-input v-model="form.sort" />
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="链接">
        <el-input v-model="form.link" />
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="banner图">
        <el-upload :action="uploadImgUrl" :show-file-list="false" :on-success="handleAvatarSuccess" :with-credentials="true" class="avatar-uploader" name="uploadFiles">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" >
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="链接类型">
        <el-select v-model="form.type" placeholder="请选择发布类型">
          <el-option key="case" value="case" label="案例" />
          <el-option key="movement" value="movement" label="动态" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogForm('0')">取 消</el-button>
      <el-button type="primary" @click="dialogForm('1')">确 定</el-button>
    </div>
  </div>
</template>

<script>
// import { updateArticle, createBanner } from '@/api/banner' // getBanner
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
const calendarTypeOptions = [
  { key: 'CN', display_name: 'China' },
  { key: 'US', display_name: 'USA' },
  { key: 'JP', display_name: 'Japan' },
  { key: 'EU', display_name: 'Eurozone' }
]
// arr to obj ,such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})
export default {
  name: 'UpdateBanner',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      imageUrl: '', // 图片预览
      importanceOptions: [1, 2, 3],
      calendarTypeOptions,
      communityQuery: {
        pageNum: 1,
        pageSize: 300,
        communityId: undefined,
        communityName: undefined,
        provinceId: undefined,
        cityId: undefined,
        districtId: undefined
      },
      listQuery: [],
      form: {
        id: '',
        serviceName: '',
        sort: '',
        link: '',
        type: 'case',
        imageUrl: ''
      },
      uploadImgUrl: process.env.BASE_API + 'uploadimage',
      dialogStatus: '',
      downloadLoading: false,
      formLabelWidth: '120px',
      showContentVisible: false,
      showURLVisible: true,
      bannerPositionArr: [
        { id: 1, value: '首页banner' },
        { id: 2, value: '服务banner' }
      ]
    }
  },
  computed: {
  },
  created() {
    this.listQuery = this.$route.params.listQuery
  },
  updated() {
  },
  mounted() {
    this.$store.dispatch('carousel/selectCarousel', this.$route.query.id).then((res) => {
      this.imageUrl = res.data.data.imageUrl

      this.form = res.data.data
    })
  },
  methods: {
    handleAvatarSuccess(res, file) { // 上传成功回调
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.imageUrl = res.data[0]
    },
    dialogAddForm() {
      this.dialogFormVisible = true
      this.form.serviceName = ''
      this.form.sort = ''
      this.form.link = ''
      this.form.type = ''
      this.form.imageUrl = ''
    },
    dialogForm(isVaule) {
      if (isVaule === '0') {
        this.$router.push({ name: 'carousel-index' })
      } else {
        this.createData()
      }
    },
    showContent() {
      if (this.form.bannerType === '1') {
        this.showContentVisible = false
        this.showURLVisible = true
      } else {
        this.showContentVisible = true
        this.showURLVisible = false
      }
    },
    createData() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$store.dispatch('carousel/updateCarousel', { id: this.$route.query.id, data: this.form }).then((res) => {
            if (res.data.code === '0') {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
            } else {
              this.$message({
                message: res.data.message,
                type: 'warning'
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.app-container {
  width: 50%;
  margin: auto;
}
.dialog-footer {
  text-align: center;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

