<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
       <template>
  <el-row>
  <el-col :span="12">
   <el-form-item label="券名称" prop="券名称">
        <el-input :disabled="ids"  style="width:300px;" v-model.trim="form.券名称" autocomplete="off" />
      </el-form-item>
  </el-col>
  </el-row>
   <el-row>
   <el-col :span="12">
   <el-form-item label="券规则" prop="券规则">
        <el-input :disabled="ids" style="width:300px;" v-model.trim="form.券规则" autocomplete="off" />
      </el-form-item>
  </el-col>
  </el-row>
    <el-row>
   <el-col :span="12">
   <el-form-item label="满多少" prop="满多少">
        <el-input :disabled="ids" style="width:300px;" v-model.trim="form.满多少" autocomplete="off" />
      </el-form-item>
  </el-col>
  </el-row>
    <el-row>
   <el-col :span="12">
   <el-form-item label="减多少" prop="减多少">
        <el-input :disabled="ids" style="width:300px;" v-model.trim="form.减多少" autocomplete="off" />
      </el-form-item>
  </el-col>
  </el-row>
    <el-form-item label="起始时间" prop="起始时间">
      <el-date-picker
      v-model="form.起始时间"
     style="width:300px;"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="选择日期">
    </el-date-picker>
      </el-form-item>
       <el-form-item label="结束时间" prop="结束时间">
      <el-date-picker
      v-model="form.结束时间"
      style="width:300px;"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="选择日期">
    </el-date-picker>
      </el-form-item>
 
   <!-- <el-row>
 
  </el-row> -->
       </template>
         <!-- <el-form-item v-else label="订单状态" prop="订单状态">
             <el-select v-model="form.订单状态" placeholder="请选择">
             <el-option
               v-for="item in options"
               :key="item.value"
               :label="item.label"
               :value="item.value">
             </el-option>
           </el-select>
      </el-form-item> -->
    </el-form>
     
    <div slot="footer" class="dialog-footer" v-if="!ids">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit,addPackage,editPackage } from '@/api/table'

  export default {
    name: 'TableEdit',
    data() {
      return {
        ids:true,
        status:'',
          options: [{
          value: '已支付',
          label: '已支付'
        },
        ],
        form: {
          券包名称:'',
          券包价格:''
        },
        rules: {
          // 券包ID: [{ required: true, trigger: 'blur', message: '请输入券ID' }],
          券名称: [{ required: true, trigger: 'blur', message: '请输入券名称' }],
          券规则: [{ required: true, trigger: 'blur', message: '请输入券包价格' }],
          满多少: [{ required: true, trigger: 'blur', message: '请输入满多少' }],
          减多少: [{ required: true, trigger: 'blur', message: '请输入减多少' }],
          起始时间: [{ required: true, trigger: 'blur', message: '请输入起始时间' }],
          结束时间: [{ required: true, trigger: 'blur', message: '请输入结束时间' }]
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row,ids) {
        console.log(row,ids,'???')
        this.status = row
        if (!row) {
          this.title = '添加'
          this.ids = false
          this.form.券包名称='',
          this.form. 券包价格=''
        //  this.form.package1 = {}
        //    this.form.package2 = {}
        //      this.form.package3 = {}
           
        } else if (ids) {
          this.ids = ids
           this.title = '查看'
             this.form = Object.assign({}, row)
        }else{
           this.title = '编辑'
            this.ids = false
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        // this.$refs['form'].resetFields()
        // this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.form.券名称 ='',
        this.form.券规则 ='',
        this.form.满多少 ='',
        this.form.减多少 ='',
        this.form.起始时间 ='',
        this.form.结束时间 =''
        // this.$emit('fetch-data')
      },
      save() {
        if(this.status){
           this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await editPackage(this.form)
            this.$message({
              message:msg,
              duration:2000,
              type:'success'
            })
            // this.$baseMessage(msg, 'success')
            this.$refs['form'].resetFields()
            this.$parent.fetchData();
            this.dialogFormVisible = false
            this.form = this.$options.data().form
          } else {
            return false
          }
        })
        }else{
            this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await addPackage(this.form)
            this.$message({
              message:msg,
              duration:2000,
              type:'success'
            })
            // this.$baseMessage(msg, 'success')
            this.$refs['form'].resetFields()
            this.$parent.fetchData();
            this.dialogFormVisible = false
            this.form = this.$options.data().form
          } else {
            return false
          }
        })

        }
        // console.log(this.status,'--=')
      },
    },
  }
</script>

