<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" @keyup.enter.native="doSubmit()"
             label-width="120px" v-loading="loading" :class="method==='view'?'readonly':''" :disabled="method==='view'" @submit.native.prevent>
      <el-row :gutter="15">
        <el-col :span="12">
          <el-form-item label="随机业务编码" prop="randomType">
            <el-radio-group v-model="inputForm.randomType" @change="changeRadio(inputForm.randomType)">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="this.isShow">
          <el-form-item label="业务编码" prop="businessCode" :rules=" [{required: true,max: 50, message: '业务编号不能为空', trigger: 'blur'}]">
            <el-input v-model="inputForm.businessCode" placeholder="业务编码"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false" icon="el-icon-circle-close">关闭</el-button>
      <el-button size="small" v-if="method != 'view'" type="primary" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import ReimbursementSys from '@/api/reimbursementSys/accountant/reimbursementSysService'
  export default {
    data () {
      return {
        isShow: false,
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          randomType: '',
          parent: {
            id: ''
          },
          businessCode: '' // 业务编码
        }
      }
    },
    reimbursementSys: null,
    created () {
      this.reimbursementSys = new ReimbursementSys()
    },
    methods: {
      init (method, obj) {
        this.method = method
        if (method === 'editBusiness') {
          this.title = '修改业务编码'
        } else if (method === 'view') {
          this.title = '查看业务编码'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['inputForm'].resetFields()
          this.inputForm.id = obj.id
          this.inputForm.parent.id = obj.parent.id
          this.inputForm.parent.name = obj.parent.name
          if (method === 'editBusiness' || method === 'view') { // 修改或者查看
            this.loading = true
            this.reimbursementSys.queryBusinessById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              if (this.inputForm.randomType === '1') {
                this.isShow = false
              } else {
                this.isShow = true
              }
              this.loading = false
            })
          }
          if (method === 'editInvoiceBusiness') { // 修改或者查看
            this.loading = true
            this.reimbursementSys.queryBusinessByInvoiceId(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              if (this.inputForm.randomType === '1') {
                this.isShow = false
              } else {
                this.isShow = true
              }
              this.loading = false
            })
          }
        })
      },
      changeRadio (randomType) {
        if (randomType === '1') {
          this.isShow = false
        } else {
          this.isShow = true
        }
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.reimbursementSys.saveBusiness(this.inputForm).then(({data}) => {
              this.loading = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
