<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="curBannerIndex - 1 === 0 ? 'xl-form-btn t3' : 'xl-form-btn t2'" @click="bannerHandle(1)">banner1</el-button>
          <el-button :class="curBannerIndex - 1 === 1 ? 'xl-form-btn t3' : 'xl-form-btn t2'" @click="bannerHandle(2)">banner2</el-button>
          <el-button :class="curBannerIndex - 1 === 2 ? 'xl-form-btn t3' : 'xl-form-btn t2'" @click="bannerHandle(3)">banner3</el-button>
          <el-button :class="curBannerIndex - 1 === 3 ? 'xl-form-btn t3' : 'xl-form-btn t2'" @click="bannerHandle(4)">banner3</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
        </div>
      </base-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      bannerArr: [{},{},{},{}],
      curBannerIndex: 1
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.bannerArr = this.curObj.banner ? JSON.parse(this.curObj.banner) : [{},{},{}]
        let curBannerObj = this.bannerArr[0]
        this.cObj = {...this.curObj,
          ad_link_img: curBannerObj.ad_link_img,
          ad_link_type: curBannerObj.ad_link_type,
          link_type_value: Number(curBannerObj.link_type_value),
          link_type_name: curBannerObj.link_type_name,
        }
        this.getDef()
      }
    },
  },
  methods: {
    bannerHandle (index) {
      const oldform = this.$refs.ruleForm.baseForm
      let bannerArr = [...this.bannerArr]
      bannerArr[this.curBannerIndex - 1] = {
        ...bannerArr[this.curBannerIndex - 1],
        ad_link_img: oldform.ad_link_img,
        ad_link_type: oldform.ad_link_type,
        link_type_value: Number(oldform.link_type_value),
      }
      this.bannerArr = [...bannerArr]
      this.curBannerIndex = index
      this.getDef('bannerChange')
    },
    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 (fieldStr === 'bannerChange') {
        let curBannerObj = this.bannerArr[this.curBannerIndex - 1]
        const oldform = this.$refs.ruleForm.baseForm
        params = {...oldform}
        if (curBannerObj.ad_link_img) params.ad_link_img = curBannerObj.ad_link_img
        if (curBannerObj.ad_link_type) params.ad_link_type = curBannerObj.ad_link_type
        if (curBannerObj.link_type_value) params.link_type_value = Number(curBannerObj.link_type_value)
        if (curBannerObj.link_type_name) params.link_type_name = curBannerObj.link_type_name
      }
      if (params.ad_link_type === 'estate') {
        this.formData = [
          { label: '品牌馆标题', key: 'brand_name', rules: 1 },
          { label: '品牌地图', key: 'pri_image', type: 'uploads' },
          { label: `banner${this.curBannerIndex}图`, key: 'ad_link_img', class: 'c-2', type: 'uploads' },
          { label: `banner${this.curBannerIndex}类型`, 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: 'scoped-two-add', 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 }]
          },
        ]
      } else if (params.ad_link_type === 'news') {
        this.formData = [
          { label: '品牌馆标题', key: 'brand_name', rules: 1 },
          { label: '品牌地图', key: 'pri_image', type: 'uploads' },
          { label: `banner${this.curBannerIndex}图`, key: 'ad_link_img', class: 'c-2', type: 'uploads' },
          { label: `banner${this.curBannerIndex}类型`, 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: 'scoped-two-add', changeHandle: this.ltValHandle,
            remoteParams: { skey: 'title', api: `house.adminformationlist`, opKey: 'title', opVal: 'id' },
            remoteOptions: [{ keyRO: params.link_type_name, valRO: params.link_type_value }]
          },
        ]
      } else if (params.ad_link_type === 'page') {
        this.formData = [
          { label: '品牌馆标题', key: 'brand_name', rules: 1 },
          { label: '品牌地图', key: 'pri_image', type: 'uploads' },
          { label: `banner${this.curBannerIndex}图`, key: 'ad_link_img', class: 'c-2', type: 'uploads' },
          { label: `banner${this.curBannerIndex}类型`, key: 'ad_link_type', type: 'select', class: 'c-2', options: this.$dictData.ad_link_type, changeHandle: this.linkTypeHandle, },
          { label: '关联页面', key: 'link_type_value', class: 'scoped-two-add',},
        ]
      } else {
        this.formData = [
          { label: '品牌馆标题', key: 'brand_name', rules: 1 },
          { label: '品牌地图', key: 'pri_image', type: 'uploads' },
          { label: `banner${this.curBannerIndex}图`, key: 'ad_link_img', class: 'c-2', type: 'uploads' },
          { label: `banner${this.curBannerIndex}类型`, key: 'ad_link_type', type: 'select', class: 'c-2', options: this.$dictData.ad_link_type, changeHandle: this.linkTypeHandle, },
        ]
      }
      this.setDefaultValue(params)
    },
    ltValHandle (val, item, op) {
      let bannerArr = [...this.bannerArr]
      bannerArr[this.curBannerIndex - 1].link_type_name = op.keyRO
      this.bannerArr = [...bannerArr]
    },
    linkTypeHandle (val) {
      this.getDef('linkType')
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const banner1Obj = this.bannerArr[0]
            if (this.curBannerIndex === 1 && oldform.ad_link_img && !banner1Obj.ad_link_img) {
              this.bannerArr[0].ad_link_img = oldform.ad_link_img
              this.bannerArr[0].ad_link_type = oldform.ad_link_type
              this.bannerArr[0].link_type_value = oldform.link_type_value
            }
            const newForm = {
              brand_name: oldform.brand_name,
              pri_image: oldform.pri_image,
              banner: JSON.stringify(this.bannerArr)
            }
            if (this.curObj.id) newForm.id = this.curObj.id
            let apiStr = 'admbrandadd'
            if (newForm.id) apiStr = 'admbrandedit'
            this.$api.other[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.curBannerIndex = 1
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.curBannerIndex = 1
        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: 100px;
  overflow: hidden;
  .icon {
    width: 180px;
    height: 98px;
    line-height: 98px;
  }
  .img {
    width: 180px;
    height: 98px;
  }
}
::v-deep .scoped-two-add {
  position: absolute;
  top: 266px;
  right: 74px;
  width: 292px!important;
}



</style>
