<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 prop="userId" :rules=" [{required: true, message: '人员不能为空', trigger: 'blur'}]" label="人员">
            <SelectUserTree
              ref="companyTree"
              :props="{
                  value: 'id',             // ID字段名
                  label: 'name',         // 显示名称
                  children: 'children'    // 子级字段名
                }"
              :url="`/sys/user/treeUserDataByOfficeName?type=2&officeName=中审众环`"
              :value="inputForm.userId"
              :clearable="true"
              :accordion="true"
              :disabled="method==='edit'"
              @getValue="(value) => {inputForm.userId=value}"/>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="年份" id="year" prop="year"
                        :rules="[
                  {required: true, message:'年份不能为空', trigger:'blur'}
                 ]">
            <el-date-picker
              v-model="inputForm.year"
              type="year"
              value-format="yyyy"
              style="width: 100%;"
              placeholder="选择年份">
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="日报销额度"  id="reimbursementQuotaDay" prop="reimbursementQuotaDay":rules="[{required: true, max: 10, message:'日报销额度不能为空', trigger:'blur'}]">
            <el-input v-model="inputForm.reimbursementQuotaDay" @input="calculateReimbursementQuota(inputForm.reimbursementQuotaDay,inputForm.reimbursementAllDay)" class="bg-grey" size="small" placeholder="日报销额度" style="width: 100%;" @keyup.native="inputForm.reimbursementQuotaDay = checkInputs(inputForm.reimbursementQuotaDay)">
                </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="可报销天数" prop="reimbursementAllDay" :rules="[
                  {required: true, message:'可报销天数不能为空', trigger:'blur'},
                  {validator: validator.isNumber, trigger:'blur'}
                 ]">
            <el-input-number v-model="inputForm.reimbursementAllDay" placeholder="可报销天数" @input="calculateReimbursementQuota(inputForm.reimbursementQuotaDay,inputForm.reimbursementAllDay)" :step="1"  :min="1" :max="366" style="width: 100%;"  ></el-input-number>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="年报销额度" prop="reimbursementQuota":rules="[{required: true, max: 10, message:'年报销额度不能为空', trigger:'blur'}]">
            <el-input v-model="inputForm.reimbursementQuota" class="bg-grey" size="small" placeholder="年报销额度" style="width: 100%;" disabled>
                </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" type="primary" v-if="method != 'view'" @click="doSubmit()" icon="el-icon-circle-check" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import SelectUserTree from '@/views/modules/reimbursementSys/utils/treeReimbursementBuinessUserSelect.vue'
  import ReimbursementUser from '@/api/reimbursementSys/user/reimbursementUserService'
  export default {
    data () {
      return {
        visible: false,
        loading: false,
        title: '',
        method: '',
        roleList: [],
        postList: [],
        inputForm: {
          id: '',
          userId: '', // 人员id
          year: '', // 年份
          reimbursementQuota: '', // 年报销额度
          reimbursementQuotaDay: '', // 日报销额度
          reimbursementAllDay: '' // 报销总天数

        }
      }
    },
    reimbursementUser: null,
    created () {
      this.reimbursementUser = new ReimbursementUser()
    },
    mounted () {
    },
    components: {
      SelectUserTree
    },
    methods: {
      checkInputs: function (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
      },
      calculateReimbursementQuota (reimbursementQuotaDay, reimbursementAllDay) {
        if (reimbursementQuotaDay && reimbursementAllDay) {
          if (!isNaN(reimbursementQuotaDay) && !isNaN(reimbursementAllDay)) {
            this.inputForm.reimbursementQuota = (reimbursementQuotaDay * reimbursementAllDay).toString()
          } else {
            this.inputForm.reimbursementQuota = ''
          }
        } else {
          this.inputForm.reimbursementQuota = ''
        }
      },
      init (method, obj) {
        this.method = method
        this.inputForm.id = obj
        if (method === 'add') {
          this.inputForm.id = obj.id
          this.title = `新建人员信息`
        } else if (method === 'edit') {
          this.title = '修改人员信息'
        } else if (method === 'view') {
          this.title = '查看人员信息'
        }
        this.visible = true
        this.$nextTick(() => {
          this.inputForm.id = obj.id
          this.$refs['inputForm'].resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.inputForm.id = obj
            this.loading = true
            this.reimbursementUser.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          } else if (method === 'add') {
            // 获取字典表中每日报销额度
            this.reimbursementUser.getReimbursementQuotaDay().then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },

      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.inputForm.officeType = '1'
            this.reimbursementUser.save(this.inputForm).then(({data}) => {
              this.loading = false
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
