<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.caseTitle"/>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="案例封面图">
        <el-upload
          :action= "uploadImgUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          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-input v-model="form.caseSummary"/>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="案例类型">
        <el-select v-model="form.taCaseTypeId" placeholder="请选择案例类型">
          <el-option v-for="item in caseTypeList" :key="item.id" :value="item.id" :label="item.typeName"/>
        </el-select>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="案例客户方logo">
        <el-upload
          :action= "uploadImgUrl"
          :show-file-list="false"
          :on-success="handleLogoSuccess"
          class="avatar-uploader"
          name="uploadFiles">
          <img v-if="logoUrl" :src="logoUrl" 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-input v-model="form.caseIndustry"/>
      </el-form-item>
      <el-form-item>
        <p class="title">案例内容</p>
        <div id="websiteEditorElem" style="height: 400px"/>
      </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-switch
          v-model="form.topping"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="1"
          inactive-value="0"/>
      </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
import E from 'wangeditor'
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: '', // 图片预览
      logoUrl: '', // 图片预览
      importanceOptions: [1, 2, 3],
      calendarTypeOptions,
      communityQuery: {
        pageNum: 1,
        pageSize: 300,
        communityId: undefined,
        communityName: undefined,
        provinceId: undefined,
        cityId: undefined,
        districtId: undefined
      },
      listQuery: [],
      form: {
        id: '',
        caseTitle: '',
        sort: '',
        caseCoverImg: '',
        caseLogoImg: '',
        caseSummary: '',
        caseIndustry: '',
        topping: 0,
        content: '',
        type: 'case'
      },
      uploadImgUrl: process.env.BASE_API + '/uploadimage',
      dialogStatus: '',
      downloadLoading: false,
      formLabelWidth: '120px',
      showContentVisible: false,
      showURLVisible: true,
      caseTypeList: [],
      bannerPositionArr: [
        { id: 1, value: '首页banner' },
        { id: 2, value: '服务banner' }
      ],
      dynamicTags: [],
      inputVisible: false,
      inputValue: ''
    }
  },
  mounted() {
    const _that = this
    const phoneEditor = new E('#websiteEditorElem')
    phoneEditor.customConfig.zIndex = 1
    phoneEditor.customConfig.onchange = function(html) {
      _that.form.content = html
      console.log('that.dynamic.url', _that.form.content)
    }
    // phoneEditor.customConfig.uploadImgServer = this.upFileUrl
    // phoneEditor.customConfig.uploadFileName = 'uploadFiles'

    phoneEditor.customConfig.customUploadImg = function(files, insert) {
      _that.uploadImg(files[0]).then(data => {
        insert(data)
      })
    }
    phoneEditor.customConfig.menus = [
      'head', // 标题
      'bold', // 粗体
      'fontSize', // 字号
      'fontName', // 字体
      'italic', // 斜体
      'underline', // 下划线
      'strikeThrough', // 删除线
      'foreColor', // 文字颜色
      'backColor', // 背景颜色
      'justify', // 对齐方式
      'image' // 插入图片
    ]
    phoneEditor.create()
  },
  created() {
    this.getCaseTypeList()
  },
  updated() {
  },
  methods: {
    handleAvatarSuccess(res, file) { // 上传成功回调
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.caseCoverImg = res.data[0]
    },
    handleLogoSuccess(res, file) { // 上传成功回调
      this.logoUrl = URL.createObjectURL(file.raw)
      this.form.caseLogoImg = res.data[0]
    },
    dialogAddForm() {
      this.dialogFormVisible = true
      this.form.serviceName = ''
      this.form.sort = ''
      this.form.serviceImageUrl = ''
    },
    dialogForm(isVaule) {
      if (isVaule === '0') {
        this.$router.push({ name: 'case-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/createCase', this.form).then((res) => {
            if (res.data.code === '0') {
              this.$message({
                message: res.data.message,
                type: 'success'
              })
              this.$router.push({ name: 'case-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 = ''
    },
    getCaseTypeList() {
      this.$store.dispatch('servicecase/getCaseTypeList', this.form).then((res) => {
        if (res.data.code === '0') {
          this.caseTypeList = res.data.data
        }
      })
    }
  }
}
</script>

<style scoped>
.title{
    font-size: 14px;
    color: #606266;
    font-weight: 600;
}
.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>

