<template>
  <div>
    <el-button  size="mini" :type="btnType?btnType:'text'" @click="onShow">编 辑</el-button>
    <el-dialog title="新 增" :visible.sync="dialogFormVisible" width="500px">
      <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="left" size="mini">
          <el-col :span="20">
            <el-form-item label="合作企业" prop="sys_enterpriseid">
              <el-select v-model="form.sys_enterpriseid" placeholder="请选择合作企业">
                <el-option
                    v-for="item in enterprises"
                    :key="item.sys_enterpriseid"
                    :label="item.enterprisename"
                    :value="item.sys_enterpriseid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20" v-show="form.sys_enterpriseid !== ''">
            <el-form-item label="营销账户类型" prop="sa_accountclassid">
              <el-select v-model="form.sa_accountclassid" placeholder="请选择营销账户类型">
                <el-option
                    v-for="item in accountclass"
                    :key="item.sa_accountclassid"
                    :label="item.accountname"
                    :value="item.sa_accountclassid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="支出金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入支出金额"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="凭证来源" >
              <el-input v-model="form.source" placeholder="请输入凭证来源"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="凭证来源说明" >
              <el-input v-model="form.sourcenote" placeholder="请输入凭证来源说明"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="备注" >
              <el-input 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: "edit",
  props:["data","btnType"],
  data(){
    return {
      dialogFormVisible:false,
      form:{
        sa_cashbillid:'',
        sys_enterpriseid:'',
        sa_accountclassid:'',
        type:0,
        amount:'',
        source:'',
        sourcenote:'',
        remarks:''
      },
      enterprises:[],
      accountclass:[],
      rules:{
        sys_enterpriseid:[
          { required: true, message: '未选择合作企业', trigger: 'change'},
        ],
        sa_accountclassid:[
          { required: true, message: '未选择营销账户类型', trigger: 'change'},
        ],
        amount:[
          { required: true, message: '支出金额不可为空', trigger: 'blur'},
        ],
      }
    }
  },
  methods:{
    onShow(){

      this.dialogFormVisible = true
      this.$store.dispatch('queryArealist')
      this.form = Object.assign({},this.form,this.data)
      console.log(this.form)
      this.enterprisesList()
      this.accountList(this.form.sa_accountclassid)
      console.log(this.form.sa_accountclassid)
    },
    onSubmit(){
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20221009102803",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields()
          this.dialogFormVisible = false
        })
      })
    },
    async enterprisesList(){
      const res = await this.$api.requested({
        "id": "20221008164103",
        "version":1,
        "content": {
          "where":{
            "condition":""
          }
        }
      })
      console.log(res)
      this.enterprises = res.data
      console.log(this.enterprises)
    },
    async accountList(id){
      const res = await this.$api.requested({
        "id": "20221008164203",
        "version":1,
        "content": {
          "sys_enterpriseid":id
        }
      })
      console.log(res)
      this.accountclass = res.data
    }
  },
  watch:{
    "form.sys_enterpriseid":function (val){
      console.log(val)
      this.accountList(val)
    }
  }
}
</script>

<style scoped>

</style>