<template>
  <el-dialog
    :title="!dataForm.assistId ? 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="assistImg">
            <img-upload v-model="dataForm.assistImg" :limit="1"></img-upload>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="邀请海报" prop="assistPoster">
            <img-upload v-model="dataForm.assistPoster" :limit="1"></img-upload>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="邀请二维码" prop="inviteQrCode">
            <img-upload v-if="dataForm.inviteQrCode" v-model="dataForm.inviteQrCode" :limit="1"></img-upload>
            <el-button v-else size="small" type="success" @click="createInviteQrCode(dataForm.assistId)">生成邀请二维码</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="助力名称" prop="assistName">
        <el-input v-model="dataForm.assistName"></el-input>
      </el-form-item>
      <el-form-item label="助力人数" prop="assistNum">
        <el-input v-model="dataForm.assistNum" type="number"></el-input>
      </el-form-item>
      <el-form-item label="已助力电量" prop="assistPower">
        <el-input v-model="dataForm.assistPower" type="number"></el-input>
      </el-form-item>
      <el-form-item label="解锁电量" prop="unlockPower">
        <el-input v-model="dataForm.unlockPower" type="number"></el-input>
      </el-form-item>
      <el-form-item label="单位发电量" prop="unlockPower">
        <el-input v-model="dataForm.unitPower" type="number"></el-input>
      </el-form-item>
      <el-form-item label="助力描述" prop="assistDesc">
        <el-input v-model="dataForm.assistDesc" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="关联商品" prop="prodId">
        <el-select v-model="dataForm.prodId" clearable filterable @change="prodChange">
          <el-option v-for="(item, index) in prodList"
                     :key="index"
                     :label="item.prodName"
                     :value="item.prodId">
          </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: {
        assistId: null,
        assistName: null,
        assistNum: null,
        assistImg: null,
        assistPower: null,
        assistDesc: null,
        unlockPower: null,
        shopId: this.$store.state.user.shopId,
        prodId: null,
        inviteQrCode: null
      },
      dataRule: {
      },
      prodList: []
    }
  },
  created() {
    this.getProdList();
  },
  methods: {
    createInviteQrCode(assistId){
      this.$http({
        url: this.$http.adornUrl('/assist/prodAssist/createInviteQrCode'),
        method: 'GET',
        params: this.$http.adornParams({assistId: assistId})
      }).then(({data}) => {
        if(data){
          this.dataForm.inviteQrCode = data;
        }
      })
    },
    prodChange(prodId){
      this.prodList.map(item =>{
        if(item.prodId === prodId){
          this.dataForm.prodName = item.prodName;
        }
      });
    },
    getProdList(){
      this.$http({
        url: this.$http.adornUrl('/prod/prod/simpleList'),
        method: 'GET',
        params: this.$http.adornParams({customized:0, shopId: this.$store.state.user.shopId})
      }).then(({data}) => {
        this.prodList = data
      })
    },
    init (assistId) {
      this.dataForm.assistId = assistId || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.assistId) {
          this.$http({
            url: this.$http.adornUrl('/assist/prodAssist/info/' + this.dataForm.assistId),
            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('/assist/prodAssist'),
            method: this.dataForm.assistId ? '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>
