<template>
  <div>
    <el-button size="mini" type="primary" @click="editBtn">编 辑</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="itemname">
                <selectProduct ref="product" @selectRow="productChange">
                  <el-input readonly type="text" slot="input" size="small" @focus="$refs.product.listData(),$refs.product.visible=true" v-model="form.itemname"></el-input>
                </selectProduct>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="所在仓库:" prop="stockname">
                <selectStore ref="store" @selectRow="storeChange">
                  <el-input readonly type="text" slot="input" size="small" @focus="$refs.store.listData(),$refs.store.visible=true" v-model="form.stockname"></el-input>
                </selectStore>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="序列号:" prop="sku">
                <el-input type="text" size="small" v-model="form.sku"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="限制使用" prop="islimit">
                <el-checkbox :true-label="1" :false-label="0" v-model="form.islimit"></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 './selectEnterprise'
import selectProduct from '@/template/selectProduct/index'
import selectStore from '@/template/selectStore/index'
export default {
  name: "add",
  components:{selectAgent,selectProduct,selectStore},
  props:['data'],
  data(){
    return {
      visible:false,
      dialogFormVisible:false,
      form:{
        "sa_itemskuid":0,     //sa_brandid<=0时 为新增
        "sys_enterpriseid":"",
        "enterprisename":'',
        "itemid":"",
        "itemname":'',
        "sku":"",
        "stockid":1,
        "islimit":1,
        "stockname":''
      },
      rules:{
        enterprisename:[
          { required: true, message: '请选择经企业', trigger: 'change'},
        ],
        itemname:[
          { required: true, message: '请选择商品', trigger: 'change'}
        ],
        sku:[
          { required: true, message: '请填写序列号', trigger: 'blur'}
        ],
        islimit:[
          { required: true, message: '请选择', trigger: 'blur'}
        ],
      }
    }
  },
  watch: {
    dialogFormVisible (val) {
      if (!val) {
        this.$refs['form'].resetFields()
      }
    }
  },
  created () {
  },
  methods:{
    editBtn () {
      this.dialogFormVisible = true
      this.form = Object.assign({},this.form,this.data)
    },
    onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20221215100803",
          "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.agent.visible = false

      this.form.sa_orderid = ''
      this.form.sonum = ''
    },
    productChange (data) {
      this.form.itemid = data.itemid
      this.form.itemname = data.itemname
      this.$refs.product.visible = false
    },
    storeChange (data) {
      this.form.stockid = data.stockid
      this.form.stockname = data.stockname
      this.$refs.store.visible = false
    }
  }
}
</script>

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