<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/treeUserDataAllOffice?type=2`"
              :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="年份" prop="year" :rules="[{required: true, message: '年份不能为空', trigger: 'blur'}]">
            <el-select v-model="inputForm.year" @change="selectYear(inputForm.year)" style="width:100%" placeholder="请选择">
              <el-option
                v-for="year in yearList"
                :key="year.id"
                :label="year.year"
                :value="year.year">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12"  v-if="method !== 'read'">
          <el-form-item label="报销类型" prop="reimbursementType"
                        :rules="[
                  {required: true, message:'报销类型不能为空', trigger:'blur'}
                 ]">
            <el-radio-group v-model="inputForm.reimbursementType">
              <el-radio v-for="item in $dictUtils.getDictList('reimbursement_type')" @change="selectReimbursementType(inputForm.reimbursementType)" :label="item.value" :key="item.id">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12"  v-if="method !== 'read'">
          <el-form-item label="实发类型" prop="reimbursementFsalary"
                        :rules="[
                  {required: true, message:'请选择实发类型', trigger:'blur'}
                 ]">
            <el-radio-group v-model="inputForm.reimbursementFsalary">
              <el-radio v-for="item in $dictUtils.getDictList('reimbursement_fsalary')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="this.show">
          <el-form-item label="报销天数" prop="reimbursementDay" :rules="[{required: true, max: 10, message:'报销天数不能为空且只可为整数', trigger:'blur'}]">
            <el-input v-model="inputForm.reimbursementDay" class="bg-grey" size="small" placeholder="报销天数" style="width: 100%;" @keyup.native="inputForm.reimbursementDay = checkInputs(inputForm.reimbursementDay)"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="this.show">
          <el-form-item label="剩余报销天数" prop="surplusReimbursementDay" :rules="[{ max: 10, trigger:'blur'}]">
            <el-input v-model="inputForm.surplusReimbursementDay" class="bg-grey" size="small" placeholder="剩余报销天数" style="width: 100%;" :disabled="true"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="报销金额" prop="reimbursementAmount":rules="[{required: true, max: 10, message:'报销金额不可为空', max: 10,trigger:'blur'}]">
            <el-input id="reimbursementAmount" v-model="inputForm.reimbursementAmount" class="bg-grey" size="small" placeholder="报销金额" style="width: 100%;" v-bind:disabled="this.show" @keyup.native="inputForm.reimbursementAmount = checkReimbursementAmount(inputForm.reimbursementAmount)">
                </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="this.show">
          <el-form-item label="日报销额度" prop="reimbursementQuotaDay":rules="[{max: 10,trigger:'blur'}]">
            <el-input  v-model="inputForm.reimbursementQuotaDay" class="bg-grey" size="small" placeholder="日报销额度" style="width: 100%;" :disabled="true">
                </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="年报销额度" prop="reimbursementQuota":rules="[{max: 10,trigger:'blur'}]">
            <el-input v-model="inputForm.reimbursementQuota" class="bg-grey" size="small" placeholder="年报销额度" style="width: 100%;" :disabled="true">
                </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="剩余可报额度" prop="remainReimbursementAmount" :rules="[{max: 10, trigger:'blur'}]">
            <el-input v-model="inputForm.remainReimbursementAmount" class="bg-grey" size="small" placeholder="剩余可报额度" style="width: 100%;" :disabled="true">
                </el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="备注" prop="remarks" :rules="[{max: 255, trigger:'blur'}]">
            <el-input type="textarea" v-model="inputForm.remarks" oninput="if(value.length>255)value=value.slice(0,254)" 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" 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 './treeReimbursementBuinessUserSelect'
  import ReimbursementBusiness from '@/api/reimbursementSys/accountant/reimbursementBusinessTwoService'
  export default {
    data () {
      return {
        show: false,
        visible: false,
        loading: false,
        title: '',
        method: '',
        yearList: [],
        inputForm: {
          id: '',
          userId: '', // 人员id
          year: '', // 年份
          reimbursementType: '', // 报销类型
          reimbursementFsalary: '', // 实发类型
          reimbursementQuota: '', // 年报销额度
          reimbursementAmount: '', // 报销金额
          reimbursementQuotaDay: '', // 日报销额度
          canReimbursementAmount: '', // 业务可报销额度
          surplusReimbursementAmount: '', // 业务剩余可报销额度
          reimbursementDay: '', // 报销天数
          surplusReimbursementDay: '', // 剩余报销天数
          oldSurplusReimbursementDay: '', // 剩余报销天数(比较数据)
          remarks: '' // 备注

        }
      }
    },
    ReimbursementBusiness: null,
    created () {
      this.reimbursementBusiness = new ReimbursementBusiness()
    },
    mounted () {
    },
    components: {
      SelectUserTree
    },
    provide () {
      return {
        say: this.say
      }
    },
    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 no-useless-escape
        str = str.replace(/[^\d^]+/g, '') // 保留数字
        // 获取日报销额度
        let reimbursementQuotaDayNumber = this.inputForm.reimbursementQuotaDay
        if (reimbursementQuotaDayNumber) {
          var oldSurplusReimbursementDay = this.inputForm.oldSurplusReimbursementDay
          this.inputForm.reimbursementAmount = (str * reimbursementQuotaDayNumber).toString()
          this.inputForm.surplusReimbursementDay = (oldSurplusReimbursementDay - str).toString()
        }
        return str
      },
      checkReimbursementAmount: 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
      },
      say (userId) {
        // 根据用户id查询年份信息
        this.reimbursementBusiness.yearList({userId: userId, current: 1, size: -1}).then(({data}) => {
          this.yearList = data.records
          if (this.yearList.length > 0) {
            this.inputForm.year = this.yearList[0].year
            this.inputForm.reimbursementQuotaDay = this.yearList[0].reimbursementQuotaDay
            this.inputForm.reimbursementQuota = this.yearList[0].reimbursementQuota
            this.inputForm.remainReimbursementAmount = this.yearList[0].remainReimbursementAmount
            this.inputForm.surplusReimbursementDay = this.yearList[0].surplusReimbursementDay
            this.inputForm.oldSurplusReimbursementDay = this.yearList[0].surplusReimbursementDay

            // 获取日报销额度
            let reimbursementQuotaDayNumber = this.inputForm.reimbursementQuotaDay
            let reimbursementDayNumber = this.inputForm.reimbursementDay
            if (reimbursementQuotaDayNumber && reimbursementDayNumber) {
              var oldSurplusReimbursementDay = this.inputForm.oldSurplusReimbursementDay
              this.inputForm.reimbursementAmount = (reimbursementDayNumber * reimbursementQuotaDayNumber).toString()
              this.inputForm.surplusReimbursementDay = (oldSurplusReimbursementDay - reimbursementDayNumber).toString()
            }
          } else {
            this.inputForm.year = ''
            this.inputForm.reimbursementQuotaDay = ''
            this.inputForm.reimbursementQuota = ''
            this.inputForm.remainReimbursementAmount = ''
            this.inputForm.surplusReimbursementDay = ''
            this.inputForm.oldSurplusReimbursementDay = ''
          }
        })
      },
      selectReimbursementType (reimbursementType) {
        if (reimbursementType === '1') {
          // reimbursementAmount.isdisabled = true
          this.show = true
        } else {
          this.show = false
        }
      },
      selectYear (year) {
        for (var yearKey in this.yearList) {
          if (this.yearList[yearKey].year === year) {
            this.inputForm.reimbursementQuotaDay = this.yearList[yearKey].reimbursementQuotaDay
            this.inputForm.reimbursementQuota = this.yearList[yearKey].reimbursementQuota
            this.inputForm.remainReimbursementAmount = this.yearList[yearKey].remainReimbursementAmount

            // 获取日报销额度
            let reimbursementQuotaDayNumber = this.inputForm.reimbursementQuotaDay
            let reimbursementDayNumber = this.inputForm.reimbursementDay
            if (reimbursementQuotaDayNumber && reimbursementDayNumber) {
              this.inputForm.reimbursementAmount = (reimbursementDayNumber * reimbursementQuotaDayNumber).toString()
            }
          }
        }
      },
      init (method, obj) {
        this.method = method
        this.inputForm.businessCodeId = obj.businessCodeId
        this.inputForm.businessCode = obj.businessCode
        if (method === 'addBusinessInfo') {
          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.reimbursementBusiness.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.selectReimbursementType(this.inputForm.reimbursementType)
              this.loading = false
            })
          }
          if (method === 'addBusinessInfo') { // 修改或者查看
            this.inputForm.businessCodeId = obj.businessCodeId
            this.loading = true
            this.reimbursementBusiness.getBusinessById(this.inputForm.businessCodeId).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.selectReimbursementType(this.inputForm.reimbursementType)
              this.loading = false
            })
          }
        })
      },

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