<template>
  <div>
    <el-button size="mini" type="primary" @click="editBtn">编 辑</el-button>
    <el-dialog title="编辑收款条件" append-to-body :visible.sync="dialogFormVisible" width="40%">
      <el-row :gutter="20">
        <el-form :model="form" :rules="rules" ref="form" label-width="140px" label-position="right" size="mini">
          <el-col :span="24">
            <el-form-item label="账期依据" prop="point">
              <el-select class="width-240" v-model="form.point" placeholder="请选择账期依据">
                <el-option label="订单" value="订单审核"></el-option>
                <el-option label="出库" value="销售出库审核"></el-option>
                <el-option label="发票" value="发票审核"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="form.point == '订单审核'">
            <el-form-item label="立账额度比例" prop="amountrate">
              <el-input v-model="form.amountrate" placeholder="请输入立账额度比例"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="后置天数" prop="days">
              <el-input v-model="form.days" placeholder="请输入后置天数"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="remarks">
              <el-input type="textarea" v-model="form.remarks" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否默认条件" prop="isdefault">
              <el-checkbox :true-label="1" :false-label="0" v-model="form.isdefault"></el-checkbox>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false;$refs.form.resetFields()" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "add",
  props:['data'],
  data(){
    var NumberSize = (rule, value, callback) => {
      if (+value >= 0 && +value <= 100) {
        callback()
      } else {
        return callback(new Error('额度比率范围(0-100)'));
      }
    }
    return {
      dialogFormVisible:false,
      form:{
        "sa_receivableruleid":0,
        "days":"10",
        "amountrate":'',
        "point":"",
        "remarks":"",
        "isdefault":1
      },
      rules:{
        point:[
          { required: false, message: '请选择账期依据', trigger: 'change'},
        ],
        amountrate:[
          { required: false, message: '请输入立账额度比率', trigger: 'blur'},
          { validator: NumberSize , trigger:'blur'}
        ],
        days:[
          { required: false, message: '请输入后置天数', trigger: 'blur'}
        ],
        isdefault:[
          { required: false, message: '请选择是否默认条件', trigger: 'blur'}
        ],
        remarks:[
          { required: false, message: '请输入描述', trigger: 'blur'}
        ],
      }
    }
  },
  methods:{
    editBtn () {
      this.dialogFormVisible = true
      this.form = Object.assign({},this.form,this.data)
      this.form.amountrate = this.form.amountrate * 100
    },
    onSubmit(){
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        this.form.amountrate = (this.form.amountrate /100).toFixed(4)
        const res = await this.$api.requested({
          "id": "20230110095903",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false
        })
      })
    }
  }
}
</script>

<style scoped>
.dialog-footer {
  margin-top: 0;
}
.el-select {
  width: 100% !important;
}
</style>