<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
          v-if="type ==='distribute' || type ==='backToBranch'"
          style="width:100%"
          verify
          label="售票员"
          prop="to_managerId">
          <el-select
            v-model="form.to_managerId"
            placeholder="请选择">
            <el-option
              v-for="item in accoutList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select> <span v-if="currentManager"> ( 当前库存 {{ currentManager.ticket_stock_num }} )</span>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          v-if="type ==='backToTotal' || type ==='distribute'"
          label="票仓名称"
          prop="from_warehouse_id">
          <el-select
            v-model="form.from_warehouse_id"
            placeholder="请选择">
            <el-option
              :value="null"
              label="请选择">
            </el-option>
            <el-option
              v-for="item in tableData.filter(i=>i.id!==1)"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          style="width:100%"
          verify
          v-if="type ==='totaltobranch' || type ==='backToBranch'"
          :label="type ==='totaltobranch'?'分仓至':'回仓至'"
          prop="to_warehouse_id">
          <el-select
            v-model="form.to_warehouse_id"
            placeholder="请选择">
            <el-option
              v-for="item in (type ==='totaltobranch'?tableData.filter(i=>i.id!==1):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 { getTicketInventoryList, transferTicketInventory } from '@/api/ticketInventory'
import { getAccountList } from '@/api/systemSetting/account'
const defaultForm = {
  from_warehouse_id: null,
  number: 1,
  remark: '',
  to_managerId: null,
  to_warehouse_id: null,
  type: '',
  ticketpaper_num_begin: '',
  ticketpaper_num_end: ''
}

const titleMap = {
  backToTotal: '回总仓',
  add: '增加库存',
  distribute: '分仓至售票员',
  backToBranch: '售票员回仓',
  totaltobranch: '分仓'
}

export default {
  data () {
    return {
      visible: false,
      loading: false,
      type: '',
      id: '',
      tableData: [],
      accoutList: [],
      form: {
        ...defaultForm
      },
      isPart: false // 是否是分仓
    }
  },
  computed: {
    title () {
      return titleMap[this.type]
    },
    currentManager () {
      return this.accoutList.find(i => i.id === this.form.to_managerId)
    }
  },
  methods: {
    show (id, type, isPart) {
      this.id = id
      this.type = type
      this.visible = true
      this.getList()

      this.getAccountList()

      this.$nextTick(() => {
        this.reset()
        this.form = JSON.parse(JSON.stringify(defaultForm))
        if (type === 'backToBranch') {
          this.form.to_warehouse_id = id
        }

        if (type === 'distribute') {
          this.form.from_warehouse_id = id
        }
      })
    },
    getList () {
      this.loading = true
      getTicketInventoryList({ pageSize: -1, pageNum: 0 }).then(res => {
        this.tableData = res.list
        // 更新全局节目信息
      }).finally(() => {
        this.loading = false
      })
    },
    getAccountList () {
      this.loading = true
      getAccountList({ pageSize: -1, pageNum: 0 }).then(res => {
        this.accoutList = res.list
      }).finally(() => {
        this.loading = false
      })
    },
    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))

          if (this.type === 'add') {
            params.type = 'TOTOTAL'
            params.from_warehouse_id = 0
            params.to_warehouse_id = 1
          } else if (this.type === 'totaltobranch') {
            params.from_warehouse_id = 1
            params.type = 'TOTALTOBRANCH'
          } else if (this.type === 'distribute') {
            params.from_warehouse_id = this.id
            params.to_warehouse_id = -1
            params.type = 'BRANCHTOMANAGER'
          } else if (this.type === 'backToTotal') {
            params.to_warehouse_id = 1
            params.type = 'BRANCHTOTOTAL'
          } else if (this.type === 'backToBranch') {
            params.type = 'MANAGERTOBRANCH'
          }
          transferTicketInventory(params).then(res => {
            this.$message.success('修改成功')
            if (this.type === 'backToBranch') {
              this.$emit('updateAccountList')
            }
            this.$emit('update')
            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>
