<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.id ? '编辑广告' : '添加广告'"
      :fullscreen="false"
      width="700px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" class="lib-edit" :data="formData" :is-inline="false" label-width="100px">
        <div slot="footer" style="padding-top: 20px;">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
        </div>
      </base-form>
      <div v-if="posTips" class="scoped-tips">当前投放位置的尺寸建议：
        <span class="s">{{posTips}}</span>
      </div>
      <div class="scoped-tips2">注:当前时间超过结束时间，则投放结束</div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      posTips: '',
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (val) {
          if (this.curObj.id) {
            this.$api.other.admadvertsdetail({id: this.curObj.id}).then(res => {
              let curData = res || {}
              curData.startEndTime = [curData.start_at, curData.end_at]
              this.cObj = curData || {}
              this.getDef()
            })
          } else {
            this.cObj = this.curObj
            this.getDef()
          }
        }
      }
    },
  },
  methods: {
    getDef(fieldStr) {
      let params = { ...this.cObj }
      if (fieldStr === 'linkType') {
        const oldform = this.$refs.ruleForm.baseForm
        params = {...this.cObj, ...oldform}
        delete params.link_type_value
      }
      if (params.ad_link_type === 'estate') {
        this.formData = [
          { label: '广告标题', key: 'title', class: 'c-2', rules: 1 },
          { label: '投放位置', key: 'ad_position', type: 'select', class: 'c-2', options: this.$dictData.ad_position, rules: 1, changeHandle: this.posHandle },
          { label: '链接类型', key: 'ad_link_type', type: 'select', class: 'c-2', options: this.$dictData.ad_link_type, changeHandle: this.linkTypeHandle, },
          { label: '关联楼盘', key: 'link_type_value', type: 'selectRemote', class: 'c-2', changeHandle: this.ltValHandle,
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: [{ keyRO: params.link_type_name, valRO: params.link_type_value }]
          },
          { label: '广告图片', key: 'images', type: 'uploads' },
          { label: '投放时间', label2: '开始时间', label3: '结束时间', key: 'startEndTime', type: 'datePicker', rules: 1},
          { label: '备注', key: 'remarked', type: 'textarea'},
        ]
      } else if (params.ad_link_type === 'news') {
        this.formData = [
          { label: '广告标题', key: 'title', class: 'c-2', rules: 1 },
          { label: '投放位置', key: 'ad_position', type: 'select', class: 'c-2', options: this.$dictData.ad_position, rules: 1, changeHandle: this.posHandle },
          { label: '链接类型', key: 'ad_link_type', type: 'select', class: 'c-2', options: this.$dictData.ad_link_type, changeHandle: this.linkTypeHandle, },
          { label: '关联文章', key: 'link_type_value', type: 'selectRemote', class: 'c-2', changeHandle: this.ltValHandle,
            remoteParams: { skey: 'title', api: `house.adminformationlist`, opKey: 'title', opVal: 'id' },
            remoteOptions: [{ keyRO: params.link_type_name, valRO: params.link_type_value }]
          },
          { label: '广告图片', key: 'images', type: 'uploads' },
          { label: '投放时间', label2: '开始时间', label3: '结束时间', key: 'startEndTime', type: 'datePicker', rules: 1},
          { label: '备注', key: 'remarked', type: 'textarea'},
        ]
      } else {
        this.formData = [
          { label: '广告标题', key: 'title', class: 'c-2', rules: 1 },
          { label: '投放位置', key: 'ad_position', type: 'select', class: 'c-2', options: this.$dictData.ad_position, rules: 1, changeHandle: this.posHandle },
          { label: '链接类型', key: 'ad_link_type', type: 'select', class: 'c-2', options: this.$dictData.ad_link_type, changeHandle: this.linkTypeHandle, },
          { label: '广告图片', key: 'images', type: 'uploads' },
          { label: '投放时间', label2: '开始时间', label3: '结束时间', key: 'startEndTime', type: 'datePicker', rules: 1},
          { label: '备注', key: 'remarked', type: 'textarea'},
        ]
      }
      this.setDefaultValue(params)
    },
    ltValHandle (val, item, op) {
      this.cObj.link_type_name = op.keyRO
    },
    linkTypeHandle (val) {
      this.getDef('linkType')
    },
    posHandle (val, item, op) {
      this.posTips = op.option1
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const newForm = { ...oldform }
            if (this.curObj.id) newForm.id = this.curObj.id
            let apiStr = 'admadvertsadd'
            if (newForm.id) apiStr = 'admadvertsedit'
            newForm.start_at = newForm.startEndTime[0]
            newForm.end_at = newForm.startEndTime[1]
            delete newForm.startEndTime
            newForm.link_type_name = this.cObj.link_type_name
            this.$api.other[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/libEdit.scss';
.lib-edit {
  padding-top: 0;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}
::v-deep .img-upload {
  height: 180px;
  overflow: hidden;
  .icon {
    width: 278px;
  }
  .img {
    width: 278px;
  }
}
.scoped-tips {
  position: absolute;
  width: 110px;
  top: 160px;
  right: 150px;
  color: #666;
  font-size: 14px;
  .s {
    display: block;
    color: #f15264;
    font-weight: bold;
  }
}
.scoped-tips2 {
  position: absolute;
  width: 260px;
  top: 350px;
  right: 0;
  color: #f15264;
  font-size: 14px;
  font-weight: bold;
}
</style>
