<template>
  <ai-detail>
    <template slot="title">
      <ai-title :title="params.id ? '编辑风险人员' : '添加风险人员'" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <el-form ref="form" class="ai-form" :model="form" label-width="110px" label-position="right">
            <el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '请输入姓名', trigger: 'blur' }]">
              <el-input v-model="form.name" size="small" placeholder="请输入姓名"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" prop="phone" :rules="[{ required: true, validator: validatorPhone, trigger: 'blur' }]">
              <el-input v-model="form.phone" :maxlength="11" size="small" placeholder="请输入..."></el-input>
            </el-form-item>
            <el-form-item label="风险类型" prop="riskType" :rules="[{ required: true, message: '请选择风险类型', trigger: 'blur' }]">
              <ai-select
                v-model="form.riskType"
                clearable
                placeholder="请选择风险类型"
                :selectList="dict.getDict('fpRiskType')">
              </ai-select>
            </el-form-item>
            <el-form-item label="身份证号" prop="idNumber" :rules="[{ required: true, validator: validatorId, trigger: 'blur' }]">
              <el-input v-model="form.idNumber" :maxlength="20" size="small" placeholder="请输入..."></el-input>
            </el-form-item>
            <el-form-item
              style="width: 100%!important;"
              label="所在地区"
              prop="areaId"
              :rules="[{ required: true, pattern: /([^0]\d{2}|0[^0]\d|0\d[^0])$/, message: '请选择到村', trigger: 'change' }]">
              <ai-area-select
                v-model="form.areaId"
                always-show
                :instance="instance"
                clearable
                @fullname="v=>form.areaName = v"
                :disabledLevel="disabledLevel"/>
            </el-form-item>
            <el-form-item label="联系地址" prop="address" style="width: 100%;">
              <el-input v-model="form.address" :maxlength="30" size="small" placeholder="请输入联系地址"></el-input>
            </el-form-item>
            <el-form-item label="备注说明" prop="remarks" style="width: 100%;">
              <el-input v-model="form.remarks" :rows="5" style="width: 100%;" type="textarea" :maxlength="500" size="small" placeholder="请简要说明风险情况"></el-input>
            </el-form-item>
          </el-form>
        </template>
      </ai-card>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="isLoading">提交</el-button>
    </template>
  </ai-detail>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'Add',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      const validatorId = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入身份证号'))
        } else if (!this.idCardNoUtil.checkIdCardNo(value)) {
          callback(new Error('身份证号格式错误'))
        } else {
          callback()
        }
      }

      const validatorPhone = function (rule, value, callback) {
        if (value === '') {
          callback(new Error('请输入联系方式'))
        } else if (!/^1\d{10}$/.test(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        info: {},
        validatorId,
        validatorPhone,
        form: {
          name: '',
          idNumber: '',
          address: '',
          areaId: '',
          areaName: '',
          remarks: '',
          phone: '',
          riskType: ''
        },
        id: '',
        isLoading: false,
        disabledLevel: 3
      }
    },

    computed: {
      ...mapState(['user'])
    },

    created () {
      this.form.areaName = this.user.info.areaName
      this.form.areaId = this.user.info.areaId
      this.disabledLevel = this.user.info.areaList.length
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/apppreventionreturntopovertyriskperson/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = {
              ...res.data,
              images: res.data.images ? JSON.parse(res.data.images) : []
            }
          }
        })
      },

      onClose () {
        this.form.explain = ''
      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.isLoading = true
            this.instance.post(`/app/apppreventionreturntopovertyriskperson/addOrUpdate`, {
              ...this.form
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('提交成功')
                setTimeout(() => {
                  this.cancel(true)
                  this.isLoading = false
                }, 600)
              } else {
                this.isLoading = false
              }
            }).catch(() => {
              this.isLoading = false
            })
          }
        })
      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'List',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
