<template>
  <div>
    <el-form :model="formData" :rules="formRules" ref="formRef" label-width="120px">
      <el-form-item label="运费模板" props="freight_template_id" :rules="formRules.freight_template_id">
        <el-select class="form-item-width" v-model="formData.freight_template_id">
          <el-option key="0" label="包邮" value="0"></el-option>
          <el-option v-for="item in freightTemplateList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import * as API_ShipTemplate from '@/api/shipTemplate'

export default {
  name: 'goodsEditFormOther',
  props: {
    propFormData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      // 表单数据
      formData: {
        freight_template_id: '0'
      },
      formRules: {
        freight_template_id: [
          this.MixinRequired('运费模板不能为空', 'change')
        ]
      },
      // 运费模板列表
      freightTemplateList: []
    }
  },
  mounted() {
    this.getFreightTemplateList()
  },
  methods: {
    /** 获取运费模板数据 */
    getFreightTemplateList() {
      API_ShipTemplate.getTplList().then(res => {
        this.freightTemplateList = res
      })
    },
    /** 表单校验 */
    async validateForm() {
      try {
        // 表单校验
        await this.$refs.formRef.validate()
        // 校验通过更新数据
        Object.assign(this.propFormData, this.formData)
      } catch (e) {
        this.$message.error('【其他设置】不完善，请填写相关信息')
        this.$emit('updateActiveTab', 'other')
        // 截断父组件后面的代码
        throw e
      }
    }
  },
  watch: {
    propFormData: {
      handler(newVal) {
        this.formData = newVal
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.form-item-width {
  width: 30%;
  min-width: 200px;
}
</style>
