<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="invoice_seller">
          <el-input
            v-model="form.invoice_seller"
            :disabled="type==='edit'"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="商户号"
          prop="mch_no">
          <el-input
            v-model="form.mch_no"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="纳税人识别号"
          prop="mch_taxpayer_num">
          <el-input
            v-model="form.mch_taxpayer_num"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="接口密钥"
          prop="private_key">
          <el-input
            v-model="form.private_key"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="开票类型"
          prop="invoiceType">
          <el-input
            v-model="form.invoiceType"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="行业类型"
          prop="business_type">
          <el-input
            v-model="form.business_type"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="征收方式"
          prop="tax_type">
          <el-input
            v-model="form.tax_type"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="税率"
          prop="tax_num">
          <el-input
            v-model="form.tax_num"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="特殊票种"
          prop="special_ticket_type">
          <el-input
            v-model="form.special_ticket_type"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="发票行性质"
          prop="invoice_property">
          <el-input
            v-model="form.invoice_property"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="商品编码"
          prop="goods_code">
          <el-input
            v-model="form.goods_code"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="商品项目名称"
          prop="goods_code_name">
          <el-input
            v-model="form.goods_code_name"
            style="width:100%"></el-input>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="使用优惠政策"
          prop="prefer_policy_flag">
          <el-switch
            v-model="form.prefer_policy_flag"
            active-text="是"
            inactive-text="否"
            :active-value="1"
            :inactive-value="0">
          </el-switch>
        </el-form-item>
        <el-form-item
          style="width:100%"
          label="增值税特殊管理"
          prop="vat_special">
          <el-input
            v-model="form.vat_special"
            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 { addInvoiceSale, updateInvoiceSale } from '@/api/ticketType'

const defaultForm = {
  invoice_seller: '',
  mch_no: '',
  mch_taxpayer_num: '',
  private_key: '',
  invoiceType: '',
  business_type: '',
  tax_type: '',
  tax_num: null,
  special_ticket_type: '',
  invoice_property: '',
  goods_code: '',
  goods_code_name: '',
  prefer_policy_flag: 0,
  vat_special: ''
}

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') {
            updateInvoiceSale(params).then(res => {
              this.$message.success('更新成功')
              this.$emit('update')
              this.handleClose()
            }).finally(() => {
              this.loading = false
            })
          } else {
            addInvoiceSale(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>
