<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '140px'">
          <el-form-item label="所属村庄" prop="suVillage">
          <el-input v-model="dataForm.suVillage" placeholder="所属村庄"></el-input>
      </el-form-item>
          <el-form-item label="姓名" prop="suName">
          <el-input v-model="dataForm.suName" placeholder="姓名"></el-input>
      </el-form-item>
          <el-form-item label="出生日期" prop="suBirthday">
            <el-date-picker
                v-model="dataForm.suBirthday"
                type="date"
                placeholder="选择日期">
            </el-date-picker>
      </el-form-item>
          <el-form-item label="残疾证号" prop="suDisabilityNumber">
          <el-input v-model="dataForm.suDisabilityNumber" placeholder="残疾证号"></el-input>
      </el-form-item>
          <el-form-item label="残疾类别及等级" prop="suDisabilityType">
          <el-input v-model="dataForm.suDisabilityType" placeholder="残疾类别及等级"></el-input>
      </el-form-item>
          <el-form-item label="是否低保" prop="suIsLow">
            <el-select v-model="dataForm.suIsLow" placeholder="是否低保">
              <el-option v-for="item in showFlagArr" :key="item.dictValue" :label="item.dictName" :value="item.dictValue">
              </el-option>
            </el-select>
      </el-form-item>
          <el-form-item label="低保类型" prop="suLowType">
          <el-input v-model="dataForm.suLowType" placeholder="低保类型"></el-input>
      </el-form-item>
          <el-form-item label="保障人口" prop="suGuaranteedPopulation">
          <el-input v-model="dataForm.suGuaranteedPopulation" placeholder="保障人口"></el-input>
      </el-form-item>
          <el-form-item label="关系" prop="suRelationship">
          <el-input v-model="dataForm.suRelationship" placeholder="关系"></el-input>
      </el-form-item>
          <el-form-item label="低保金额" prop="suLowMoney">
          <el-input-number :precision="2" :step="0.1" v-model="dataForm.suLowMoney" placeholder="低保金额"></el-input-number>
      </el-form-item>
          <el-form-item label="困难残疾人生活补贴" prop="suLivingSubsidy">
          <el-input-number :precision="2" :step="0.1" v-model="dataForm.suLivingSubsidy" placeholder="困难残疾人生活补贴"></el-input-number>
      </el-form-item>
          <el-form-item label="重度残疾人护理补贴" prop="suCareSubsidy">
          <el-input-number :precision="2" :step="0.1" v-model="dataForm.suCareSubsidy" placeholder="重度残疾人护理补贴"></el-input-number>
      </el-form-item>
          <el-form-item label="经济困难老年人补贴" prop="suElderlySubsidy">
          <el-input-number :precision="2" :step="0.1" v-model="dataForm.suElderlySubsidy" placeholder="经济困难老年人补贴"></el-input-number>
      </el-form-item>
          <el-form-item label="孤困儿童补贴" prop="suChildSubsidy">
            <el-input-number :precision="2" :step="0.1" v-model="dataForm.suChildSubsidy" placeholder="孤困儿童补贴"></el-input-number>
      </el-form-item>
          <el-form-item label="分散五保补贴" prop="suFiveGuaranteesSubsidy">
            <el-input-number v-model="dataForm.suFiveGuaranteesSubsidy" :precision="2" :step="0.1" placeholder="分散五保补贴"></el-input-number>
      </el-form-item>
          <el-form-item label="是否贫困户" prop="suIsPoor">
            <el-select v-model="dataForm.suIsPoor" placeholder="是否贫困户">
              <el-option v-for="item in showFlagArr" :key="item.dictValue" :label="item.dictName" :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        suVillage: '',
        suName: '',
        suBirthday: '',
        suDisabilityNumber: '',
        suDisabilityType: '',
        suIsLow: '',
        suGuaranteedPopulation: '',
        suRelationship: '',
        suLowMoney: '',
        suLivingSubsidy: '',
        suCareSubsidy: '',
        suElderlySubsidy: '',
        suChildSubsidy: '',
        suFiveGuaranteesSubsidy: '',
        suIsPoor: '',
        suLowType: ''
      },
      showFlagArr: [
        {
          dictValue: '1',
          dictName: '是'
        },
        {
          dictValue: '0',
          dictName: '否'
        }
      ]
    }
  },
  computed: {
    dataRule () {
      return {
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/custom/specialuser/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/custom/specialuser/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
