<template>
  <el-dialog
    :title="!dataForm.templateId ? this.$i18n.t('crud.addTitle') : this.$i18n.t('temp.modify')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <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-row>
        <el-col :span="6">
            <el-form-item label="正面" prop="frontUrl">
              <img-upload v-model="dataForm.frontUrl" :limit="1"></img-upload>
            </el-form-item>
          </el-col>
        <el-col :span="6">
          <el-form-item label="背面" prop="backUrl">
            <img-upload v-model="dataForm.backUrl" :limit="1"></img-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="是否禁用" prop="isDelete">
        <el-select v-model="dataForm.isDelete" clearable filterable>
          <el-option key="1" label="启用" :value=0 ></el-option>
          <el-option key="0" label="禁用" :value=1 ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="seq">
        <el-input v-model="dataForm.seq" style="width: 185px" type="number"/>
      </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: {
        id: null,
        frontUrl: null,
        backUrl: null,
        prodId: null,
        prodName: null,
        seq: null,
        isDelete: 0,
      },
      prodList: [],
      dataRule: {
      }
    }
  },
  created(){
    this.getProdList();
  },
  methods: {
    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('/print/printPicLib/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.dataForm = data
          })
        }
      })
    },
    prodChange(prodId){
      this.prodList.map(item =>{
        if(item.prodId === prodId){
          this.dataForm.prodName = item.prodName;
        }
      })
      // this.dataForm.prodId = prod.value;
      // this.dataForm.prodName = prod.label;
    },
    getProdList(){
      this.$http({
        url: this.$http.adornUrl('/prod/prod/simpleList'),
        method: 'GET',
        params: this.$http.adornParams({customized:0})
      }).then(({data}) => {
        this.prodList = data
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl('/print/printPicLib'),
            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: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
