<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="640px"
    @close="handleClose">
    <el-form
      inline
      :model="form"
      ref="form"
      class="form-wrap"
      label-width="100px">
      <div class="dialog-info">
        <el-form-item
          style="width:100%"
          verify
          label="回仓对象"
          prop="to_warehouse_id">
          <el-select
            v-model="form.to_warehouse_id"
            placeholder="请选择">
            <el-option
              v-for="item in tableData"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          label="数量"
          prop="number">
          <el-input-number
            v-model="form.number"
            :min="1"
            label="分仓数量"></el-input-number>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          label="票的税印起始"
          prop="ticketpaper_num_begin">
          <el-input v-model="form.ticketpaper_num_begin"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          label="票的税印结束"
          prop="ticketpaper_num_end">
          <el-input v-model="form.ticketpaper_num_end"></el-input>
        </el-form-item>
      </div>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">
        取 消
      </el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="handleSubmit">
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { transferTicketInventory } from '@/api/ticketInventory'

const defaultForm = {
  from_warehouse_id: 0,
  number: 1,
  remark: '',
  to_managerId: null,
  to_warehouse_id: null,
  type: '',
  ticketpaper_num_begin: '',
  ticketpaper_num_end: ''
}

export default {
  computed: {
  },
  props: {
    tableData: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      title: '',
      visible: false,
      loading: false,
      type: '',
      id: '',
      form: {
        ...defaultForm
      }
    }
  },
  methods: {
    show (id) {
      this.id = id
      this.visible = true
      this.$nextTick(() => {
        this.reset()
        this.form = JSON.parse(JSON.stringify(defaultForm))
        // this.form.to_warehouse_id = id
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleClose () {
      this.visible = false
    },
    handleSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          let params = JSON.parse(JSON.stringify(this.form))
          params.to_managerId = this.id
          params.type = 'MANAGERTOBRANCH'

          transferTicketInventory(params).then(res => {
            this.$message.success('修改成功')
            this.$emit('updateAccountList')
            this.handleClose()
          }).finally(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.periodItem {
  display: flex;  margin: 0 10px 10px 0; align-items: center;
}
::v-deep .el-form-item__label{
  width: 200px!important;
}
.form-wrap {
  .priceInput {
     margin: 0 10px;
    .el-input-number{
        width: 160px;
    }
    ::v-deep .el-input {
      width: 100%;
    }
  }
}

</style>
