<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="110px">
        <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>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      isShowMap: false
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (val) {
          if (this.curObj.id) {
            this.$api.house.adminformationdetail({id: this.curObj.id}).then(res => {
              let curData = res || {}
              this.cObj = curData || {}
              this.getDef()
            })
          } else {
            this.cObj = this.curObj
            this.getDef()
          }
        }
      }
    },
  },
  methods: {
    getDef() {
      const params = { ...this.cObj }
      const disabled = false
      const remoteOptionsIds = []
      if (params.estate_list) {
        params.estate_id_list = params.estate_list.map(item => {
          remoteOptionsIds.push({ keyRO: item.estate_name, valRO: item.id })
          return item.id
        })
      } else {
        params.estate_id_list = []
      }
      // if (params.id) disabled = true
      this.formData = [
        { label: '标题', key: 'title' },
        { label: '链接', key: 'link' },
        { label: '分类', key: 'information_category', type: 'select', class: 'c-2', options: this.$dictData.information_category, changeHandle: this.cateChange },
        { label: '作者', key: 'author', class: 'c-2' },
        { label: '状态', key: 'hide_status', type: 'select', class: 'c-2', options: this.$dictData.hide_status },
        { label: '发布时间', key: 'create_at', class: 'c-2', type: 'datePicker', type2: 'date'},
        { label: '主图', key: 'pri_image', type: 'upload', class: 'c-2' },
        { label: '关联楼盘', key: 'estate_id_list', type: 'selectRemote', multiple: true, changeHandle: this.deviceChange,
          remoteParams: { skey: 'estate_name', api: `house.admestatelist?search_EQ_status=1`, opKey: 'estate_name', opVal: 'id' },
          remoteOptions: remoteOptionsIds
        },
        { label: '关联区域', key: 'area_type', type: 'select', multiple: true, options: this.$dictData.area_type },
      ]
      this.setDefaultValue(params)
    },
    cateChange (val) {
      let params = {...this.$refs.ruleForm.baseForm}
      let imgURL= ''
      if (val === 'hlzt') imgURL= 'http://icon.honglounews.com/news_hlzt.jpg'
      if (val === 'hljy' || val === 'xqsc') imgURL= 'http://icon.honglounews.com/news_hljy.jpg'
      if (val === 'bkfx') imgURL= 'http://icon.honglounews.com/news_bkfx.jpg'
      if (val === 'lppc') imgURL= 'http://icon.honglounews.com/news_lppc.jpg'
      if (val === 'lpwd') imgURL= 'http://icon.honglounews.com/news_lpwd.jpg'

      if (val === 'hlpd') imgURL= 'http://icon.honglounews.com/news_hlpd.jpg'
      if (val === 'lszt') imgURL= 'http://icon.honglounews.com/news_lszt.jpg'
      if (val === 'zcjd') imgURL= 'http://icon.honglounews.com/news_zcjd.jpg'
      if (val === 'esf') imgURL= 'http://icon.honglounews.com/news_esf.jpg'
      if (val === 'lsdt') imgURL= 'http://icon.honglounews.com/news_lsdt.jpg'
      params.pri_image = imgURL
      this.setDefaultValue(params)
    },
    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
            if (newForm.estate_id_list && newForm.estate_id_list.length > 0) {
              newForm.estate_id_list = newForm.estate_id_list.join(',')
            } else {
              newForm.estate_id_list = ''
            }
            if (newForm.area_type) newForm.area_type = newForm.area_type.join(',')
            let apiStr = 'adminformationadd'
            newForm.news_type = 'gzh'
            if (!newForm.create_at) delete newForm.create_at
            if (newForm.id) apiStr = 'adminformationedit'
            this.$api.house[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%;
  }
}
</style>
