<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="三方skuID" prop="thirdSkuId">
        <el-input v-model="dataForm.thirdSkuId" style="width: 185px"/>
      </el-form-item>
      <el-form-item label="三方sku名称" prop="thirdSkuName">
        <el-input v-model="dataForm.thirdSkuName" style="width: 185px"/>
      </el-form-item>
      <!-- 产品类型 -->
      <el-form-item label="产品类型" prop="prodType">
        <el-select v-model="dataForm.prodType" clearable filterable >
          <el-option key="smallcard" label="小卡" value="smallcard"></el-option>
          <el-option key="squarecard" label="方卡" value="squarecard"></el-option>
          <el-option key="badge" label="马口铁徽章" value="badge"></el-option>
          <el-option key="plushbadge" label="毛绒徽章" value="plushbadge"></el-option>
          <el-option key="postcard" label="明信片" value="postcard"></el-option>
          <el-option key="bookmark" label="书签" value="bookmark"></el-option>
          <el-option key="tipscard" label="迷你手幅" value="tipscard"></el-option>
          <el-option key="polaroid" label="花式拍立得" value="polaroid"></el-option>
          <el-option key="covercard" label="直拍封面" value="covercard"></el-option>
          <el-option key="stub" label="票根" value="stub"></el-option>
          <el-option key="transformcard" label="变换卡" value="transformcard"></el-option>
          <el-option key="transpolaroid" label="透明胶片拍立得" value="transpolaroid"></el-option>
          <el-option key="rectanglesticker" label="长形贴纸" value="rectanglesticker"></el-option>
          <el-option key="squaresticker" label="方形贴纸" value="squaresticker"></el-option>
          <el-option key="bookmark3" label="三宫格透明胶片书签" value="bookmark3"></el-option>
        </el-select>
      </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-item label="产品SKU" prop="skuId">
        <el-select v-model="dataForm.skuId" clearable filterable>
          <el-option v-for="(item, index) in skuList"
               :key="index"
               :label="item.skuName"
               :value="item.skuId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三方产品类型编号" prop="prodTypeNo">
        <el-input v-model="dataForm.prodTypeNo" style="width: 185px"/>
      </el-form-item>
      <el-form-item label="三方SKU编号" prop="skuNo">
        <el-input v-model="dataForm.skuNo" style="width: 185px"/>
      </el-form-item>
      <el-form-item label="产品角型" prop="prodAngle">
        <el-select v-model="dataForm.prodAngle" clearable filterable>
          <el-option key="1" lable="圆角" value="圆角"/>
          <el-option key="2" lable="直角" value="直角"/>
        </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>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: null,
        prodId: null,
        prodType: null,
        thirdSkuName: null,
        skuId: null,
        thirdSkuId: null,
        prodTypeNo: null,
        prodAngle: null,
        skuNo: null,
        prodName: null,
      },
      prodList: [],
      skuList: [],
      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('/prod/thirdPrintSku/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.getSkuList()
    },
    getSkuList(){
      this.$http({
        url: this.$http.adornUrl('/sku/getEnableSkuList'),
        method: 'GET',
        params: this.$http.adornParams({prodId:this.dataForm.prodId})
      }).then(({data}) => {
        this.skuList = data
      })
    },
    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('/prod/thirdPrintSku'),
            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: 200,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
