<template>
  <el-dialog
    :title="!dataForm.id ? this.$i18n.t('crud.addTitle') : this.$i18n.t('temp.modify')"
    :close-on-click-modal="false"
    top="5vh"
    width="40%"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="关注二维码" prop="officialQrCode">
            <img-upload v-model="dataForm.officialQrCode" :limit="1"></img-upload>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="官方Logo" prop="officialLogo">
            <img-upload v-model="dataForm.officialLogo" :limit="1"></img-upload>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="渠道Logo" prop="channelLogo">
            <img-upload v-model="dataForm.channelLogo" :limit="1"></img-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="官方名称" prop="officialName">
        <el-input v-model="dataForm.officialName"></el-input>
      </el-form-item>
      <el-form-item label="官方ID" prop="officialNo">
        <el-input v-model="dataForm.officialNo"></el-input>
      </el-form-item>
      <el-form-item label="渠道名称" prop="channelName">
        <el-input v-model="dataForm.channelName"></el-input>
      </el-form-item>
      <el-form-item label="官方介绍" prop="officialDesc">
        <el-input v-model="dataForm.officialDesc" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="关联店铺" prop="shopId">
        <el-select v-model="dataForm.shopId" clearable filterable>
          <el-option v-for="(item, index) in shopList"
                     :key="index"
                     :label="item.shopName"
                     :value="item.shopId">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button class="default-btn" @click="visible = false">{{$t("crud.filter.cancelBtn")}}</el-button>
      <el-button class="default-btn primary-btn" type="primary" @click="dataFormSubmit()">{{$t("crud.filter.submitBtn")}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import ImgUpload from '@/components/imgs-upload'
export default {
  components: {
    ImgUpload
  },
  data () {
    return {
      visible: false,
      dataForm: {
        officialNo: null,
        officialName: null,
        officialLogo: null,
        officialQrCode: null,
        channelName: null,
        channelLogo: null,
        shopId: this.$store.state.user.shopId,
        status:1
      },
      dataRule: {
      },
      shopList: []
    }
  },
  created() {
    this.getShopList();
  },
  methods: {

    getShopList() {
      this.$http({
        url: this.$http.adornUrl('/shop/shopDetail/getShopList'),
        method: 'get',
        params: this.$http.adornParams({shopId: this.shopId})
      }).then(({ data }) => {
        if (data) {
          this.shopList = data
        }
      })
    },
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/promotion/officialChannel/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.dataForm = data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/promotion/officialChannel'),
            method: this.dataForm.id ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({data}) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
