<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.id ? '编辑短视频' : '新增短视频'"
      :fullscreen="false"
      width="760px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" class="lib-edit" :data="formData" :is-inline="false" label-width="110px">
        <div slot="footer">
          <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>
import { arrToObj } from '@/utils'
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      loading: false,
      formData: [],
      cObj: {},
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (this.curObj.id) {
          this.$api.other.admchannelsdetail({id: this.curObj.id}).then(res => {
            this.cObj = res || {}
            this.getDef()
          })
        } else {
          this.getDef()
        }
      }
    },
  },
  methods: {
    getDef (str) {
      let params = {}
      params = { ...this.cObj }
      if (str === 'edit') {
        params = {...this.$refs.ruleForm.baseForm, ...params}
      }
      if (!params.hide_status) params.hide_status = '1'
      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 = []
      }
      this.formData = [
        { label: '发布时间', key: 'publish_at', type: 'datePicker', type2: 'date', rules: 1},
        { label: '标题', key: 'title', rules: 1},
        { label: '状态', rules: 1, key: 'hide_status', class: 'c-2', type: 'select', options: this.$dictData.hide_status},
        { label: '视频分类', key: 'channels_type', class: 'c-2', type: 'select', options: this.$dictData.channels_type},
        { label: '封面图', rules: 1, key: 'cover', class: 'c-2', type: 'cuImg',
          options: {
            w: 175,
            h: 225,
          }
        },
        { label: '自定义标签', class: 'c-2', key: 'custom_tag', type: 'textarea' },
        { label: '视频ID', rules: 1, key: 'feed_id', type: 'textarea' },
        { label: '关联楼盘', key: 'estate_id_list', type: 'selectRemote', multiple: true, changeHandle: this.deviceChange,
          remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
          remoteOptions: remoteOptionsIds
        },
      ]
      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
            newForm.custom_tag = newForm.custom_tag.replace(/，|、|\/|\\/g, ',')
            let apiStr = 'admchannelsadd'
            if (this.curObj.id) apiStr = 'admchannelsedit'
            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 = ''
            }
            this.$api.other[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.cObj = {}
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.cObj = {}
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/libEdit.scss';
.lib-edit {
  padding-top: 0;
  padding-left: 0;
  padding-bottom: 40px;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}
</style>
