<template>
  <div class="data-info">
    <el-card>
      <el-form :model="dataForm" label-width="120px">

        <el-form-item label="编码:">
          <el-input v-model="dataForm.code"></el-input>
        </el-form-item>
        <el-form-item label="名称 :">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>
        <el-form-item label="取值类型:">
          <el-select v-model="dataForm.value_type"  placeholder="请选择取值类型" size="large" style="width:100%;">
              <el-option
                v-for="item in valueTypeList"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              />
            </el-select>
        </el-form-item>
        <el-form-item label="取值:">
          <el-input   v-model="dataForm.value"></el-input>
        </el-form-item>
        <el-form-item label="取值范围:" v-if="dataForm.value_type==2">
          <el-input type="textarea" v-model="dataForm.value_item"></el-input>
        </el-form-item>
        <el-form-item label="介绍:">
            <el-input type="textarea" v-model="dataForm.intro"></el-input>
        </el-form-item>

        <el-form-item label="状态:">
          <el-select v-model="dataForm.status"  placeholder="请选择状态" size="large" style="width:100%;">
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
        </el-form-item>
      </el-form>
    </el-card>
    <div style="text-align:right; margin-top:20px;">
        <el-button type="warning" @click="btnSave">确认保存</el-button>
    </div>
  </div>
</template>

<script>
  import {createParams,updateParams,getParamsInfo} from'@/api/params';
  export default{
    emits:['onClose'],
    props:{
      id:{
        type:Number,
        default:0
      },
      type:{
        type:Number,
        default:1,
      },
    },
    watch:{
      type:{
        handler(val){
          if(val==null && val<=0)return;
          this.dataForm.type=val;
        },
        immediate: true,
        deep: true
      },
      id:{
        handler(v){
            let _this=this;
            if(v==null || v<=0)return;
            _this.getInfo(v);
        },
        immediate: true,
        deep: true
      }
    },
    data(){
      return{
        activeName:'base',
        currentCategory:[],
        valueTypeList:[
          {
            value:1,
            name:'文本内容',
          },
          {
            value:2,
            name:'下拉选择',
          }
        ],
        allCategories:[],
        typeList:[
          {
            value:1,
            name:'变量',
          },
          {
            value:2,
            name:'常量',
          }
        ],
        dataForm:{
           id:0,
           type:1,
           name:'',
           value:'',
           value_type:1,
           value_item:' ',
           intro:"",
           status:'',
        },
        statusOptions:[
         {
           value:'',
           label:"请选择状态"
         },
         {
           value:5,
           label:'使用中'
         },
         {
           value:6,
           label:"已停用"
         },
        ],
      }
    },

    mounted() {
      let _this=this;
    },
    methods:{
      //保存更新
      btnSave(e){
          let _this=this;

          if(_this.dataForm.value_type==1){
            _this.dataForm.value_item=_this.dataForm.value;
          }
          if(_this.dataForm.id>0){
            updateParams(_this.dataForm).then(res=>{
              if(res.status!=200)return;
              _this.$alert("信息更新成功");
              _this.$emit("onClose");
            })
          }else{
            createParams(_this.dataForm).then(res=>{
              if(res.status!=200)return;
              _this.$alert("信息创建成功");
              _this.$emit("onClose");
            })
          }
          _this.$emit("onClose")
      },

      //产品详情
      getInfo(id){
          let _this=this;
          let par={
            id:id,
          };
          getParamsInfo(par).then(res=>{
              if(!res)return;
              if(res.status!=200)return;
              _this.dataForm=res.data;
          })
      },

      onChangeStatus(e){
          this.dataForm.status=e;
      },
      //修改分类信息
      onChangeCategory(e){
         let _this=this;
         _this.dataForm.categoryId=e;
      },

    }
  }
</script>

<style lang="scss">
 @import './dataInfo.scss'
</style>
