<template>
  <div class="商城首页弹窗配置-set gray-box top-redius border-bottom-gray">
    <div class="title">{{$t('sysManagement.homePagePopupConfig')}}</div>
    <el-form
      ref="dataForm"
      label-width="180px"
      size="mini"
      class="set-form"
      label-position="left"
      @keyup.enter.native="dataFormSubmit()"
      :rules="dataRule"
      :model="dataForm"
    >
      <el-form-item :label="`${$t('sysManagement.popupName')}:`" style="width:640px" prop="popupName">
        <el-input v-model="dataForm.popupName" :placeholder="`${$t('sysManagement.popupName')}`" controls-position="right"></el-input>
      </el-form-item>
      <el-form-item :label="`${$t('sysManagement.popupPic')}:`" style="width:640px" prop="popupPic">
        <el-input v-model="dataForm.popupPic" :placeholder="`${$t('sysManagement.popupPic')}`" controls-position="right"></el-input>
      </el-form-item>
      <el-form-item :label="`${$t('sysManagement.popupPicToPage')}:`" style="width:640px" prop="popupPicToPage">
        <el-input v-model="dataForm.popupPicToPage" :placeholder="`${$t('sysManagement.popupPicToPage')}`" controls-position="right"></el-input>
      </el-form-item>
      <el-form-item :label="$t('sysManagement.isActive')" style="width:640px" prop="isOpen">
        <el-switch v-model="dataForm.isOpen" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
      </el-form-item>

      <div class="default-btn" @click="dataFormSubmit()">{{$t('sysManagement.save')}}</div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <!-- <el-button @click="visible = false">取消</el-button> -->
    </span>
  </div>
</template>

<script>
export default {
  props: {
    saveUrl: {
      type: String,
      default: '/sys/pconfig/save'
    },
    changeFlag: {
      type: Boolean,
      default: true
    }
  },
  watch: {
    changeFlag: function (newVal, oldVal) {
      if (!this.activationEventOriginate) {
        this.dataForm.isOpen = false
      }
      this.activationEventOriginate = false
    }
  },
  data () {
    return {
      dataForm: {
        // id: null,
        // paramKey: 'HOME_PAGE_POPUP',
        popupName: null,
        popupPic: null,
        popupPicToPage: null,
        isOpen: true
        // paramValue: null
      },
      activationEventOriginate: false,
      homePagePopup: {
        // eBusinessID: null,
        // appKey: null,
        // reqUrl: null
      },
      dataRule: {
        popupPic: [
          { required: true, message: `${this.$i18n.t('sysManagement.popupPic')} ${this.$i18n.t('sysManagement.nullTips')}`, trigger: 'blur' }
        ],
        popupPicToPage: [
          { required: true, message: `${this.$i18n.t('sysManagement.popupPicToPage')} ${this.$i18n.t('sysManagement.nullTips')}`, trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      // this.$refs['dataForm'].resetFields()
      this.$nextTick(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/pconfig/info/' + 'HOME_PAGE_POPUP'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          if (data) {
            this.dataForm = data
          }
        })
        this.$refs['dataForm'].resetFields()
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.getHomePagePopupParam()
          this.$http({
            url: this.$http.adornUrl(this.saveUrl),
            method: 'post',
            data: this.$http.adornData({
              'paramKey': 'HOME_PAGE_POPUP',
              'paramValue': JSON.stringify(this.dataForm),
              'remark': this.$i18n.t('sysManagement.homePagePopupConfig')
            })
          }).then(({ data }) => {
            if (data) {
              this.init()
            }
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                if (this.dataForm.isOpen) {
                  this.activationEventOriginate = true
                  this.$emit('activationConfig')
                }
              }
            })
          })
        }
      })
    },
    /**
     * dataform -> homePagePopup
     */
    getHomePagePopupParam () {
      this.homePagePopup.popupName = this.dataForm.popupName
      this.homePagePopup.popupPic = this.dataForm.popupPic
      this.homePagePopup.popupPicToPage = this.dataForm.popupPicToPage
    },
    /**
     *  homePagePopup -> dataform
     */

    getDataParam (data) {
      // let obj = JSON.parse(data.paramValue)
      this.dataForm.paramKey = data.paramKey
      this.dataForm.paramValue = data.paramValue
      this.dataForm.popupName = data.homePagePopup.popupName
      this.dataForm.popupPic = data.homePagePopup.popupPic
      this.dataForm.popupPicToPage = data.homePagePopup.popupPicToPage
    }
  }
}
</script>
