<template>
  <div>
    <el-button size="mini" type="primary" :disabled="disabled" @click="editBtn">编 辑</el-button>
    <el-drawer title="新 建" append-to-body :visible.sync="dialogFormVisible" size="30%">
      <div slot="title" style="font-size: 15px">
        编辑核销单
      </div>
      <div class="drawer__panel">
        <el-row>
          <el-form :model="form" :rules="rules" ref="form" label-width="90px" label-position="right" size="mini">
            <el-col :span="24">
              <el-form-item label="经销商:" prop="sys_enterpriseid">
                <selectEnterprise ref="ent" @rowClick="entRowClick"></selectEnterprise>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="账户类型" prop="sa_accountclassid">
                <el-select v-model="form.sa_accountclassid" placeholder="请选择账户类型" @change="accountChange" size="small">
                  <el-option
                    v-for="item in accountType"
                    :key="item.sa_accountclassid"
                    :label="item.accountname"
                    :value="item.sa_accountclassid">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="凭证" prop="sa_cashbillid">
                <selectVoucher ref="voucher" :sys_enterpriseid="form.sys_enterpriseid" :sa_accountclassid="form.sa_accountclassid" @rowClick="voucherRowClick"></selectVoucher>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="核销金额" prop="writeoffamount">
                <el-input size="small" placeholder="请输入核销金额" v-model.number="form.writeoffamount"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="核销日期" prop="billnodate">
                <el-date-picker
                  v-model="form.billnodate"
                  type="date"
                  value-format="yy-mm-dd"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import selectEnterprise from './selectEnterprise'
import selectVoucher from './selectVoucher'
export default {
  name: "add",
  props:['data','disabled'],
  components:{selectEnterprise,selectVoucher},
  data(){
    return {
      dialogFormVisible:false,
      accountType:[],
      form:{
        "sa_writeoffbillid":0,
        "sys_enterpriseid": "",
        "sa_accountclassid": "",
        "sa_cashbillid":"",
        "remarks":"",
        "writeoffamount":'',  //新增时可不传
        "billnodate":''
      },
      rules:{
        sys_enterpriseid:[
          { required: true, message: '请选择经销商', trigger: 'blur'},
        ],
        sa_accountclassid:[
          { required: true, message: '请选择账户类型', trigger: 'blur'}
        ],
        sa_cashbillid:[
          { required: true, message: '请选择支出或收入凭证', trigger: 'blur'}
        ],
        writeoffamount:[
          { message: '请填写正确金额', trigger: 'change',type:'number'}
        ],
      }
    }
  },
  created () {
    this.getAccountType()
  },
  watch: {
    dialogFormVisible (val) {
      if(!val) {
        this.$refs.ent.form.enterprisename = ''
        this.$refs.form.resetFields()
      }
    }
  },
  methods:{
    editBtn () {
      this.form = Object.assign({},this.form,this.data)
      console.log(this.form);
      
      this.$nextTick(() => {
        this.$refs.ent.form.enterprisename = this.form.enterprisename
        this.$refs.voucher.form.billno = this.form.cashbillidbillno
        console.log(this.$refs.ent.form);
        
      })
      this.dialogFormVisible = true
    },
    onSubmit(){
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20221124090204",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.dialogFormVisible = false
        })
      })
    },
    entRowClick (data) {
      this.$refs.voucher.form.billno = ''
      this.form.sa_cashbillid = ''
      this.form.sys_enterpriseid = data.sys_enterpriseid
    },
    voucherRowClick (data) {
      this.form.sa_cashbillid = data.sa_cashbillid
    },
    async getAccountType () {
      let res = await this.$api.requested({
        "id":20221124090604,
        "content": {

        }
      })
      this.accountType = res.data
      console.log(this.accountType);
      
    },
    accountChange (val) {
      this.$refs.voucher.form.billno = ''
      this.form.sa_cashbillid = ''
    }
  }
}
</script>

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