<template>
  <div class="container">
    <el-row >
      <el-col :xl="8" :lg="8" :md="8" :sm="12" :xs="24">
        <el-form
          :model="typedata"
          :rules="rules"
          ref="form"
          label-width="120px"
          class="demo-ruleForm"
        >
          <el-form-item label="类型" prop="typeName">
            <el-input v-model="typedata.typeName" ></el-input>
          </el-form-item>
          <el-form-item label="类别" prop="typeCategory">
            <el-select v-model="typedata.typeCategory"  disabled >
              <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-item>
            <el-button type="success" icon="el-icon-upload2" @click="updateType()">提交</el-button>
            <el-button type="info" icon="el-icon-refresh-left" @click="cancel()">取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      
    </el-row>
  </div>
</template>

<script>
 
import {selectById,updata} from "@/api/type"

export default {
  data() {
    return {
      typedata:{
        typeName:null,
        typeCategory:null,
      },
      options: [
        {
          value: "0",
          label: "收入类别",
        },
        {
          value: "1",
          label: "支出类别",
        },
      ],
      rules: {
        typeName: [
          { required: true, message: "类型不能为空", trigger: "blur" },
        ],
        typeCategory: [
          { required: true, message: "类别不能为空", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    typeId() {
      return this.$route.query.id;
    },
  },
  created(){
    this.getTypeData()
  },
  methods:{
    //获取数据
    getTypeData(){
      selectById({typeId:this.typeId}).then((res)=>{
        if(res.success){
          this.typedata = res.data
        }else{
          this.$message.error(res.msg)
        }
      }).catch((error)=>{
        this.$message.error("查询数据失败")
      })
    },
    //提交
    updateType(){
      updata(this.typedata).then((res)=>{
        if(res.success){
          this.$message.success("修改成功")
          this.$store.dispatch('tagsView/delView', this.$route)
          this.$router.push("/typeList")
        }else{
          this.$message.error(res.msg)
        }
      }).catch((error)=>{
        this.$message.error("修改失败")
      })
    },
    //返回
    cancel(){
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.push("/typeList")
    }
  }
};
</script>

<style scoped>
.container {
  padding: 10px;
}
</style>