<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.typeName"/>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="排序">
        <el-input v-model="form.sort"/>
      </el-form-item>
      <el-form-item>
        <el-tag
          v-for="tag in dynamicTags"
          :key="tag"
          :disable-transitions="false"
          closable
          @close="handleClose(tag)">
          {{ tag }}
        </el-tag>
        <el-input
          v-if="inputVisible"
          ref="saveTagInput"
          v-model="inputValue"
          size="small"
          class="input-new-tag"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"/>
      </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: '',
        serviceImageUrl: '',
        labelList: []
      },
      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.listQuery = this.$route.params.listQuery
  },
  updated() {
  },
  methods: {
    handleAvatarSuccess(res, file) { // 上传成功回调
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.serviceImageUrl = res.data[0]
    },
    dialogAddForm() {
      this.dialogFormVisible = true
      this.form.typeName = ''
      this.form.sort = ''
      this.form.serviceImageUrl = ''
    },
    dialogForm(isVaule) {
      if (isVaule === '0') {
        this.$router.push({ name: 'servicecase-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.form.labelList = this.dynamicTags
          this.$store.dispatch('servicecase/addServiceCase', this.form).then((res) => {
            if (res.data.code === '0') {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
              this.$router.push({ name: 'servicecase-index' })
            } 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>

