<template>
  <div id="app" class="app-container">
    <el-form ref="form" :model="form">
      <el-form-item :label-width="formLabelWidth" label="案例页面封面图">
        <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"/>
          <div slot="tip" class="el-upload__tip">建议上传1920*768的图片</div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <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: '',
        coverImg: '',
        labelList: [],
        type: 'case'
      },
      uploadImgUrl: process.env.BASE_API + 'uploadimage',
      dialogStatus: '',
      downloadLoading: false,
      formLabelWidth: '120px',
      showContentVisible: false,
      showURLVisible: true,
      bannerPositionArr: [
        { id: 1, value: '首页banner' },
        { id: 2, value: '服务banner' }
      ],
      dynamicTags: [],
      inputVisible: false,
      inputValue: ''
    }
  },
  computed: {
  },
  created() {
    this.getCoverImg()
  },
  updated() {
  },
  methods: {
    handleAvatarSuccess(res, file) { // 上传成功回调
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.coverImg = res.data[0]
    },
    dialogAddForm() {
      this.dialogFormVisible = true
      this.form.serviceName = ''
      this.form.sort = ''
      this.form.coverImg = ''
    },
    dialogForm(isVaule) {
      if (isVaule === '0') {
        // this.$router.push({ name: 'service-index' })
        this.$router.go(-1)
      } 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.form.labelList = this.dynamicTags
          this.$store.dispatch('servicecase/createCaseCover', this.form).then((res) => {
            if (res.data.code === '0') {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
              // this.$router.push({ name: 'case-cover' })
            } else {
              this.$message({
                message: res.data.message,
                type: 'warning'
              })
            }
          })
        }
      })
    },
    getCoverImg() {
      this.$store.dispatch('servicecase/getCaseCover', this.form).then((res) => {
        if (res.data.code === '0') {
          if (res.data.data.length > 0) {
            this.imageUrl = res.data.data[0].coverImg
            this.form.coverImg = res.data.data[0].coverImg
          }
        } else {
          this.$message({
            message: res.data.message,
            type: 'warning'
          })
        }
      })
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>

<style scoped>
.app-container {
  width: 50%;
  margin: auto;
}
.dialog-footer {
  text-align: center;
}
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</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>

