<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules" v-if="!ids">
       <template>
  <el-row>
  <el-col :span="12">
   <el-form-item label="角色名称" prop="角色名称">
        <el-input style="width:300px;" :disabled="ids" 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  style="width:300px;" type="textarea" v-model="form.券包价格"></el-input>
        <!-- <el-input :disabled="ids" v-model.trim="form.券包价格" autocomplete="off" /> -->
      </el-form-item>
  </el-col>
  </el-row>
  
    <el-row>
   <el-col :span="12">
  <el-form-item label="状态">
    <el-radio-group  style="width:300px;"  v-model="form.resource">
      <el-radio label="1">启用</el-radio>
      <el-radio label="2">禁用</el-radio>
    </el-radio-group>
  </el-form-item>
   </el-col>
  </el-row>
       </template>
    </el-form>

  <div class="block" v-else>
  <span class="demonstration">权限设置</span>
  <el-cascader
  style="width:300px;margin-left:20px;"
    :options="options"
    :props="props"
    collapse-tags
    clearable>
    </el-cascader>
   </div>
     
    <div slot="footer" class="dialog-footer" >
      <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: '已支付'
        // },
        // ],
         props: { multiple: true },
          options: [{
          value: 1,
          label: '东南',
          children: [{
            value: 2,
            label: '上海',
            children: [
              { value: 3, label: '普陀' },
              { value: 4, label: '黄埔' },
              { value: 5, label: '徐汇' }
            ]
          }, {
            value: 7,
            label: '江苏',
            children: [
              { value: 8, label: '南京' },
              { value: 9, label: '苏州' },
              { value: 10, label: '无锡' }
            ]
          }, {
            value: 12,
            label: '浙江',
            children: [
              { value: 13, label: '杭州' },
              { value: 14, label: '宁波' },
              { value: 15, label: '嘉兴' }
            ]
          }]
        }, {
          value: 17,
          label: '西北',
          children: [{
            value: 18,
            label: '陕西',
            children: [
              { value: 19, label: '西安' },
              { value: 20, label: '延安' }
            ]
          }, {
            value: 21,
            label: '新疆维吾尔族自治区',
            children: [
              { value: 22, label: '乌鲁木齐' },
              { value: 23, label: '克拉玛依' }
            ]
          }]
        }],
        form: {
          券包名称:'',
          券包价格:'',
          package1:{
             券ID:'',
          券的有效期限:'',
          },
           package2:{
             券ID:'',
          券的有效期限:'',
          },
           package3:{
             券ID:'',
          券的有效期限:'',
          },
          resource:'1'
         
        
        },
        rules: {
          // 券包ID: [{ required: true, trigger: 'blur', message: '请输入券ID' }],
          券包名称: [{ 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.package1 = {}
      this.form.package2 = {}
      this.form.package3 = {}
        // 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>

