<template>
  <div class="add">
    <el-button type="primary" size="small" style="margin-bottom:16px" @click="dialogVisible=true" v-if="type=='add'">新增</el-button>
    <el-button type="text" size="small" @click="editBtn" v-else>编辑</el-button>
    <el-dialog
      :title="type == 'add' ? '新增计量单位组' : '编辑计量单位组'"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      append-to-body>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
        <el-form-item label="计量单位组名称" prop="unitgroupname">
          <el-input v-model="ruleForm.unitgroupname" size="small" placeholder="请输入计量单位组名称"></el-input>
        </el-form-item>
        <el-form-item label="主计量单位" prop="unitid">
          <el-select v-model="ruleForm.unitid" placeholder="请选择">
            <el-option
              v-for="item in allUnit"
              :key="item.value"
              :label="item.unitname"
              :value="item.unitid"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="辅助计量单位" prop="auxunitid">
          <el-select v-model="ruleForm.auxunitid" placeholder="请选择">
            <el-option
              v-for="item in allUnit"
              :key="item.value"
              :label="item.unitname"
              :value="item.unitid"
              size="small">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="换算率" prop="conversionrate">
          <el-input v-model.number="ruleForm.conversionrate" size="small" placeholder="换算率=辅助单位数量/主单位数量"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <div>
          <el-button @click="dialogVisible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="submit" size="small">确 定</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
  
</template>

<script>
import { log } from '@antv/g2plot/lib/utils';
export default {
  name: '',
  data() {
    return {
      currentData:'',
      dialogVisible:false,
      ruleForm: {
        unitgroupname:'',
        unitid:'',
        auxunitid:'',
        conversionrate:''
      },
      rules:{
        unitgroupname: [
          { required: true, message: '请输入计量单位组名称', trigger: 'blur' },
        ],
        unitid: [
          { required: true, message: '请输入主计量单位', trigger: 'blur' },
        ],
        auxunitid: [
          { required: true, message: '请输入辅助计量单位', trigger: 'blur' },
        ],
        conversionrate: [
          { required: true, message: '请输入换算率,换算率=辅助单位数量/主单位数量', trigger: 'blur' },
          { message: '换算率必须为数字', trigger: 'blur' , type:'number' },
        ],
      }
    };
  },
  props:['type','unitData','allUnit'],
  computed:{
  },
  watch:{
    currentData: {
      handler(val) {
        this.ruleForm = {
          unitgroupname:val.unitgroupname,
          unitid:val.unitid,
          auxunitid:val.auxunitid,
          conversionrate:val.conversionrate
        }
      }
    }
  },
  methods: {
    editBtn() {
      this.dialogVisible=true
      this.currentData = this.unitData
    },
    handleClose() {
      this.dialogVisible = false
    },
    submit() {
      this.$refs.ruleForm.validate(async val => {
        if(val) {
          let res = await this.$api.requested({
            "id": "20220922084403",
            "version":1,
            "content": {
              "unitgroupid":this.type == 'add' ? 0 : this.unitData.unitgroupid,     
              "unitgroupname":this.ruleForm.unitgroupname,
              "unitid":this.ruleForm.unitid,
              "auxunitid":this.ruleForm.auxunitid,
              "conversionrate":this.ruleForm.conversionrate
            }
          })
          console.log(res);
          this.tool.showMessage(res,() => {
            this.$emit('unitAddSuccess')
            this.dialogVisible = false
            this.$refs.ruleForm.resetFields()
          })
        }
      })
    }
  },
};
</script>

<style scoped>
  .dialog-footer {
    display: flex;
    justify-content: space-evenly;
  }
  /deep/.el-dialog__body {
    padding-top: 10px !important;
    padding-bottom: 0 !important;
  }
  /deep/.dialog-footer {
    margin-top: 10px !important;
  }
  /deep/.el-select {
    width: 100% !important;
  }
</style>
