<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="首页banner">
        <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" />
          <div slot="tip" class="el-upload__tip">建议上传案例为481*466、分享为769*459的图片</div>
        </el-upload>
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="详情banner">
        <el-upload :action="uploadImgUrl" :show-file-list="false" :on-success="handleDetailSuccess" class="avatar-uploader" name="uploadFiles">
          <img v-if="detailUrl" :src="detailUrl" 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-item :label-width="formLabelWidth" label="更多banner">
        <el-upload :action="uploadImgUrl" :show-file-list="false" :on-success="handlemoreSuccess" class="avatar-uploader" name="uploadFiles">
          <img v-if="moreUrl" :src="moreUrl" class="avatar" >
          <i v-else class="el-icon-plus avatar-uploader-icon" />
          <div slot="tip" class="el-upload__tip">建议上传481*466的图片</div>
        </el-upload>
      </el-form-item>

      <el-form-item :label-width="formLabelWidth" label="列表图">
        <el-upload :action="uploadImgUrl" :show-file-list="false" :on-success="handleListUrlSuccess" class="avatar-uploader" name="uploadFiles">
          <img v-if="listUrl" :src="listUrl" class="avatar" >
          <i v-else class="el-icon-plus avatar-uploader-icon" />
          <div slot="tip" class="el-upload__tip">建议上传707*464的图片</div>
        </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 :label-width="formLabelWidth" label="链接名称">
        <el-input v-model="form.linkName" />
      </el-form-item>
      <el-form-item :label-width="formLabelWidth" label="分享链接">
        <el-input v-model="form.caseLink" />
      </el-form-item>
      <el-form-item>
        <p class="title">内容</p>
        <Tinymce ref="setcontent" :content="form.content" @fun="success"/>
        <!-- <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-value="1" :inactive-value="0" active-color="#13ce66" inactive-color="#ff4949" />
      </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 Tinymce from '@/components/Tinymce' // 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, Tinymce },
  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: '', // 图片预览
      detailUrl: '',
      moreUrl: '',
      listUrl: '',
      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: '',
        linkName: '',
        caseLink: '',
        topping: 0,
        content: '',
        type: 'case',

        caseDetailCoverImg: '',
        caseMoreCoverImg: '',
        listImg: ''
      },
      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

    this.$store.dispatch('servicecase/getCaseDetail', this.$route.query.id).then((res) => {
      if (res.data.code === '0') {
        this.imageUrl = res.data.data.caseCoverImg
        this.logoUrl = res.data.data.caseLogoImg

        this.detailUrl = res.data.data.caseDetailCoverImg
        this.moreUrl = res.data.data.caseMoreCoverImg
        this.listUrl = res.data.data.listImg
        this.form = res.data.data
      } else {
        this.$message({
          message: res.data.message,
          type: 'warning'
        })
      }
    })
  },
  created() {
    this.form.type = this.$route.query.type
    this.getCaseTypeList()
    // this.getCaseDetail(this.$route.query.id)
  },
  updated() {
  },
  methods: {
    handleAvatarSuccess(res, file) { // 上传成功回调
      this.imageUrl = URL.createObjectURL(file.raw)
      this.form.caseCoverImg = res.data[0]
    },
    handleDetailSuccess(res, file) { // 上传成功回调
      this.detailUrl = URL.createObjectURL(file.raw)
      this.form.caseDetailCoverImg = res.data[0]
    },
    handleMoreSuccess(res, file) { // 上传成功回调
      this.moreUrl = URL.createObjectURL(file.raw)
      this.form.caseMoreCoverImg = res.data[0]
    },
    handleListUrlSuccess(res, file) { // 上传成功回调
      this.listUrl = URL.createObjectURL(file.raw)
      this.form.listImg = res.data[0]
    },

    handleLogoSuccess(res, file) { // 上传成功回调
      this.logoUrl = URL.createObjectURL(file.raw)
      this.form.caseLogoImg = res.data[0]
    },
    success: function(res) {
      this.form.content = res
    },
    //     success(res){
    //
    // // this.form.content = this.$refs.setcontent.contents
    //     },
    dialogAddForm() {
      this.dialogFormVisible = true
      this.form.serviceName = ''
      this.form.sort = ''
      this.form.serviceImageUrl = ''
    },
    dialogForm(isVaule) {
      if (isVaule === '0') {
        // this.$router.push({ name: 'case-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
      }
    },
    getContent() {
      this.form.content = this.$refs.setcontent.content
    },
    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' })
              // this.$router.go(-1)
            } else {
              this.$message({
                message: res.data.message,
                type: 'warning'
              })
            }
          })
        }
      })
    },
    getCaseDetail(id) {
      this.$store.dispatch('servicecase/getCaseDetail', id).then((res) => {
        if (res.data.code === '0') {
          this.imageUrl = res.data.data.caseCoverImg
          this.logoUrl = res.data.data.caseLogoImg
          this.detailUrl = res.data.data.caseDetailCoverImg
          this.moreUrl = res.data.data.caseMoreCoverImg
          this.listUrl = res.data.data.listImg
          this.form = res.data.data
        } 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 || []).filter(x => x.type === this.$route.query.type)
        }
      })
    }
  }
}
</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>

