<template>
  <div>
    <el-button size="mini" type="primary" @click="dialogFormVisible = true">新 建</el-button>
    <el-drawer title="新建返退单" append-to-body :visible.sync="dialogFormVisible" size="40%" :show-close="false">
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules" ref="form" label-width="100px" label-position="right" size="mini">
            <!-- <el-col :span="24">
              <el-form-item label="经销商:" prop="enterprisename">
                  <selectAgent ref="agent" @selectRow="agentChange">
                    <el-input readonly type="text" slot="input" size="small" @focus="$refs.agent.visible=true" v-model="form.enterprisename"></el-input>
                  </selectAgent>
              </el-form-item>
            </el-col> -->
            <el-col :span="24">
              <el-form-item label="订单号:" prop="sonum">
                <selectOrder ref="order" @selectRow="orderChange">
                    <el-input readonly type="text" slot="input" size="small" @focus="$refs.order.listData(),$refs.order.visible=true" v-model="form.sonum"></el-input>
                </selectOrder>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="24">
              <el-form-item label="返退单类型:" prop="type">
                <el-select style="width:100%" v-model="form.type" placeholder="请选择返退单类型">
                  <el-option label="借用归还单" value="借用归还单"></el-option>
                  <el-option label="退货单" value="退货单"></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
            <el-col :span="24">
              <el-form-item label="单据日期:" prop="billdate">
                <el-date-picker
                  style="width:100%"
                  v-model="form.billdate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择开始日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="返退原因:" prop="reason">
                <el-input type="textarea" size="small" v-model="form.reason"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="是否实物退回" prop="isitemreturn">
                <el-checkbox :true-label="1" :false-label="0" v-model="form.isitemreturn"></el-checkbox>
              </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 selectAgent from '@/template/selectAgent/index'
import selectOrder from './selectOrder'
export default {
  name: "add",
  components:{selectAgent,selectOrder},
  data(){
    return {
      visible:false,
      dialogFormVisible:false,
      form:{
        "sa_aftersalesmagid":0,
        "sa_orderid":0,
        "reason":"",
        "isitemreturn":true,
        "billdate":"",
        "enterprisename":'',
        "sonum":"",
        "type":'借用归还单'
      },
      rules:{
        enterprisename:[
          { required: true, message: '请选择经销商', trigger: 'change'},
        ],
        sonum:[
          { required: true, message: '请选择订单', trigger: 'change'}
        ],
        billdate:[
          { required: true, message: '请选择单据日期', trigger: 'change'}
        ],
        type:[
          { required: true, message: '请选择返退单类型', trigger: 'change'}
        ],
      }
    }
  },
  watch: {
    dialogFormVisible (val) {
      if (!val) {
        this.$refs['form'].resetFields()
        
      }
    }
  },
  created () {
    this.form.billdate = new Date().getFullYear() + '-' + parseInt(new Date().getMonth() + 1) + '-' + parseInt(new Date().getDate())
  },
  methods:{
    onSubmit(){
      console.log(this.form)
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20230104160503",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.dialogFormVisible = false
        })
      })
    },
    agentChange (data) {
      console.log(data);
      this.form.sys_enterpriseid = data.sys_enterpriseid
      this.form.enterprisename = data.enterprisename
      this.$refs.order.param.content.sys_enterpriseid = data.sys_enterpriseid
      this.$refs.agent.visible = false
    },
    orderChange (data) {
      this.form.sa_orderid = data.sa_orderid
      this.form.sonum = data.sonum
      this.$refs.order.visible = false
    }
  }
}
</script>

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