<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    @close="handleClose">
    <el-form
      inline
      :model="form"
      ref="form"
      class="form-wrap"
      label-width="120px">
      <div class="dialog-info">
        <el-form-item
          style="width:100%"
          label="销售方名称"
          prop="invoiceSellerName">
          <el-input
            v-model="form.invoiceSellerName"
            :disabled="type==='edit'"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="纳税人识别号"
          prop="invoiceSellerTaxpayerNum">
          <el-input
            v-model="form.invoiceSellerTaxpayerNum"
            style="width:100%"></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 { addInvoiceSeller, updateInvoiceSeller } from '@/api/invoice'

const defaultForm = {
  invoiceSellerName: '',
  invoiceSellerTaxpayerNum: ''
}

export default {
  data () {
    return {
      title: '',
      visible: false,
      loading: false,
      type: 'add',
      form: {
        ...defaultForm
      }
    }
  },
  methods: {
    show (data, type) {
      this.type = type
      this.title = (type === 'edit' ? '编辑销售方' : '新增销售方')
      this.visible = true

      this.$nextTick(() => {
        this.reset()

        if (type === 'edit' && data) {
          this.form = Object.assign({}, this.form, JSON.parse(JSON.stringify(data)))
        } else {
          this.form = JSON.parse(JSON.stringify(defaultForm))
        }
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    handleClose () {
      this.visible = false
    },
    handleSubmit () {
      this.$refs.form.validate(valid => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form))

          this.loading = true
          if (this.type === 'edit') {
            updateInvoiceSeller(params).then(res => {
              this.$message.success('更新成功')
              this.$emit('update')
              this.handleClose()
            }).finally(() => {
              this.loading = false
            })
          } else {
            addInvoiceSeller(params).then(res => {
              this.$message.success('添加成功')
              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;
    }
    .form-wrap {
      .priceInput {
         margin: 0 10px;
        .el-input-number{
            width: 160px;
        }
        ::v-deep .el-input {
          width: 100%;
        }
      }
    }

    </style>
