<template>
  <div>
    <el-button size="mini" :type="btnType?btnType:'text'" @click="editBtn">编 辑</el-button>
    <el-dialog title="新 建" :visible.sync="dialogFormVisible" width="50%">
      <div slot="title" style="font-size: 15px">
        编辑报价单
      </div>
      <el-row :gutter="20">
        <el-form :model="form" :rules="rules" ref="form" label-width="120px" label-position="right" size="mini">
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectname">
              <el-input v-model="form.projectname" placeholder="请输入项目名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="折扣率" prop="discountrate">
              <el-input v-model="form.discountrate" placeholder="请输入折扣率"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="有效日期" prop="invaliddate">
              <el-date-picker
                v-model="form.invaliddate"
                type="date"
                placeholder="选择日期"
                @change="timeChange">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remarks">
              <el-input type="textarea" v-model="form.remarks" placeholder="请输入备注"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="onSubmit" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "add",
  inject:['detailData'],
  props:['data','btnType'],
  components:{},
  data(){
    return {
      dialogFormVisible:false,
      form:{
        "sa_quotedpriceid":0,     //sat_notice_classid<=0时 为新增
        "sys_enterpriseid": "",
        "sa_projectid": "",
        "discountrate": "",
        "remarks": "",
        "invaliddate": "",
        "projectname": "",
        "address": ""
      },
      rules:{
        projectname:[
          { required: true, message: '输入项目名称', trigger: 'blur'},
        ],
        invaliddate:[
          { required: true, message: '请选择报价有效期', trigger: 'blur'}
        ],
        discountrate:[
          { required: true, message: '整单折扣率，保留2位小数', trigger: 'blur'}
        ],
      }
    }
  },
  methods:{
    editBtn () {
      console.log(this.data);
      
      this.dialogFormVisible = true
      this.form = Object.assign({},this.form,this.data)
    },
    onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        this.form.sys_enterpriseid = this.detailData().sys_enterpriseid
        this.form.sa_projectid = parseInt(this.$route.query.id)
        const res = await this.$api.requested({
          "id": "20221020164803",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false
        })
      })
    },
    timeChange(e) {
      if(!e) {
        this.form.invaliddate = ''
      } 
      let result = this.changeTimeFormat(e.getTime())
 
      this.form.invaliddate = result
    },
    changeTimeFormat(time) {
      var date = new Date(time);
      var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      return date.getFullYear() + "-" + month + "-" + currentDate
    }
  }
}
</script>

<style scoped>
/deep/.el-select {
  height: 28px !important;
  width: 100%;
}
/deep/.el-input__inner {
  height: 28px !important;
}
</style>