<template>
  <div>
    <el-dialog :title="exData.id ? '编辑': '新增'"
               :visible.sync="dialog"
               width="600px"
               :close-on-click-modal="false"
               top="50px">
      <el-form ref="form"
               :model="form"
               :rules="formRules"
               label-width="100px">
        <el-form-item prop="attach_name"
                      :rules="formRules.required"
                      label="规格名称：">
          <el-input v-model="form.attach_name"
                    placeholder="请输入规格名称"
                    clearable></el-input>
        </el-form-item>
        <el-form-item v-for="(option, index) in form.attach_content"
                      :label="(index + 1).toString()"
                      :key="index"
                      :prop="'attach_content.' + index + '.name'"
                      :rules="formRules.required">
          <el-input style="width: 75%;"
                    v-model="option.name"
                    placeholder="请输入附加内容"
                    clearable></el-input>
          <p style="display:inline-block;width: 25%; text-align: right">
            <el-button type="warning"
                       :disabled="form.attach_content.length <= 1"
                       @click.prevent="removeAttachContent(option)">删除
            </el-button>
          </p>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer text-center">
        <el-button @click="dialog = false">取 消</el-button>
        <el-button @click="addAttachContent">新增选项名</el-button>
        <el-button type="primary" :disabled="booLock" @click="handleSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    value: {
      type: Boolean,
      default: true
    },
    exData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      dialog: !!this.value,
      form: {
        attach_name: '', // 附加名称
        attach_content: [
          {
            name: ''
          }
        ]
      },
      booLock: false
    }
  },
  methods: {
    removeAttachContent (item) {
      var index = this.form.attach_content.indexOf(item)
      if (index !== -1) {
        this.form.attach_content.splice(index, 1)
      }
    },
    addAttachContent () {
      this.form.attach_content.push({
        name: ''
      })
    },
    handleSubmit () {
      const url = this.exData.id ? '/v1/bar/product/attach/modify' : '/v1/bar/product/attach/add'
      this.$refs.form.validate(async valid => {
        if (valid) {
          const postData = {
            ...this.form,
            attach_content: this.form.attach_content.map(item => item.name).join(',')
          }
          this.booLock = true
          const data = await this.$fetch(url, postData)
          this.booLock = false
          if (data.code === 200) {
            this.$message.success('提交成功')
            this.$emit('success')
            this.$store.dispatch('common/fetchGoodsAttr')
            this.dialog = false
          }
        }
      })
    }
  },
  mounted () {
    if (this.exData.id) {
      this.$set(this.form, 'id', this.exData.id)
      for (const key in this.exData) {
        if (this.form.hasOwnProperty(key)) {
          let value = this.exData[key]
          if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
            if (key === 'attach_content') {
              value = value.split(',').map(item => ({
                name: item
              }))
            }
            this.$set(this.form, key, value)
          }
        }
      }
    }
  },
  watch: {
    dialog (val) {
      if (!val) this.$emit('input', val)
    }
  }
}
</script>

<style lang="scss" scoped>
.top-tip {
  margin-top: -20px;
  margin-bottom: 20px;
}
</style>
