<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="80px" 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="businessCode" :rules=" [{required: true,max: 50, message: '业务编号不能为空', trigger: 'blur'}]">
            <el-input v-model="inputForm.businessCode" placeholder="业务编码" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报销比例(%)" prop="reimbursementRatio":rules="[{required: true, max: 10, message:'报销比例不能为空', trigger:'blur'}]">
            <el-input v-model="inputForm.reimbursementRatio" class="bg-grey" size="small" placeholder="报销比例(%)" style="width: 100%;" @keyup.native="inputForm.reimbursementRatio = checkInputs(inputForm.reimbursementRatio)">
                </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 {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          name: '',
          parent: {
            id: ''
          },
          businessCode: '', // 业务编码
          reimbursementRatio: '' // 报销比例
        }
      }
    },
    reimbursementSys: null,
    created () {
      this.reimbursementSys = new ReimbursementSys()
    },
    methods: {
      checkInputs (num) {
        let str = num.toString()
        var len1 = str.substr(0, 1)
        var len2 = str.substr(1, 1)
        // eslint-disable-next-line eqeqeq
        if (str.length > 1 && len1 == 0 && len2 != '.') {
          str = str.substr(1, 1)
        }
        // eslint-disable-next-line eqeqeq
        if (len1 == '.') {
          str = ''
        }
        // eslint-disable-next-line eqeqeq
        if (str.indexOf('.') != -1) {
          var str_ = str.substr(str.indexOf('.') + 1)
          // eslint-disable-next-line eqeqeq
          if (str_.indexOf('.') != -1) {
            str = str.substr(0, str.indexOf('.') + str_.indexOf('.') + 1)
          }
          if (str_.length > 2) {
            this.$message.warning(`金额小数点后只能输入两位，请正确输入！`)
            return (str = '')
          }
        }
        // eslint-disable-next-line no-useless-escape
        str = str.replace(/[^\d^\.]+/g, '') // 保留数字和小数点
        return str
      },
      init (method, obj) {
        this.method = method
        if (method === 'editBusinessRatio') {
          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 === 'editBusinessRatio' || method === 'view') { // 修改或者查看
            this.loading = true
            this.reimbursementSys.queryBusinessById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.reimbursementSys.updateReimbursementRatio(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>
