<template>
  <div class="dialog-h-content scroll-h pd0">
    <el-form label-width="120px" :model="form" :inline="true" 
      :disabled="disabled" :rules="rules" ref="ruleForm" 
      class="form-wr">
      <div class="flex-div">
        <el-form-item label="所属党组织" prop="sszb" class="wd50">
          <el-cascader
                v-model="form.sszb"
                :options="partyList"
                :props="partyProps"
                clearable
                 class="input-width"
                @change="handlePartyChange"></el-cascader>
        </el-form-item>
        <el-form-item label="党员中心户" prop="isDyzxh" class="wd50">
          <el-radio-group v-model="form.isDyzxh">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
        </el-form-item>
      </div>
      <div class="flex-div">
        <el-form-item label="姓名" prop="name" class="wd50">
          <el-input v-model="form.name" placeholder="请输入" :disabled="disabled" class="input-width" clearable></el-input>
        </el-form-item>
        <el-form-item label="免学习" prop="isMxx" class="wd50">
          <el-radio-group v-model="form.isMxx">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </div>
      <div class="flex-div">
        <el-form-item label="手机号" prop="mobile" class="wd50">
          <el-input v-model="form.mobile" placeholder="请输入" :disabled="disabled" class="input-width" clearable />
        </el-form-item>
        <el-form-item label="职务" prop="partyZw" class="wd50">
          <el-select v-model="form.partyZw" filterable :disabled="disabled" placeholder="请选择" class="input-width" clearable>
            <el-option
              v-for="item in zwList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div class="flex-div">
        <el-form-item label="身份证" prop="idCard" class="wd50">
          <el-input v-model="form.idCard" placeholder="请输入" :disabled="disabled" 
          class="input-width" clearable @blur="handleValidBlur" />
        </el-form-item>
        <el-form-item label="文化程度" prop="culture" class="wd50">
          <el-select v-model="form.culture" filterable :disabled="disabled" placeholder="请选择" class="input-width" clearable>
            <el-option
              v-for="item in eduList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </div>
      <div class="flex-div">
        <el-form-item label="地址" prop="address" class="wd50">
          <el-input v-model="form.address" placeholder="请输入" :disabled="disabled || isAuto" class="input-width" clearable></el-input>
        </el-form-item>
        <el-form-item label="入党时间" prop="rdsj" class="wd50">
          <el-date-picker
            v-model="form.rdsj"
             :disabled="disabled"
            type="date"
            class="input-width"
            clearable
            placeholder="选择日期"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </div>
      <div class="flex-div">
        <el-form-item label="流动党员" prop="isLd" class="wd50">
          <el-radio-group v-model="form.isLd" class="input-width">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="流动党员号" prop="ldzh" class="wd50">
          <el-input v-model="form.ldzh" placeholder="请输入" :disabled="disabled" class="input-width" clearable></el-input>
        </el-form-item>
      </div>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" :autosize="{ minRows: 4, maxRows: 10}" :disabled="disabled" type="textarea" clearable class="input-width-textarea" placeholder="请输入内容"></el-input>
      </el-form-item>
      <div >
        <div style="margin-top: 20px; text-align: center;">
            <el-button size="small" @click="handleCancle">取消</el-button>
            <el-button
              v-if="form.icResiUser != null && form.icResiUser != ''"
              type="primary"
              :disabled="false"
              size="small"
              @click="jump(form.icResiUser)">查看更多</el-button>
          </div>
      </div>
      
    </el-form>
  </div>
</template>

<script>
import { isCard, isMobile } from '@/utils/validate'
import { computedCard } from '@/utils/index'

export default {
  components: {

  },
  props: {
    partyList: {
      type: Array,
      default: () => []
    },
    info: {
      type: Object,
      default: () => {}
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    let checkMObile = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else {
        if (!isMobile(value)) {
          callback(new Error('手机号格式不正确'))
        }
        callback()
      }
    }
    let checkIdCard = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入身份证'))
      } else {
        if (!isCard(value)) {
          callback(new Error('身份证号格式不正确'))
        }
        callback()
      }
    }
    return {
      activesName: 'first',
      isAuto: false,
      formItemWd: '100%',
      partymenberid: '',
      partyProps: {
        label: 'partyOrgName',
        value: 'id',
        emitPath: false
      },
      zwList: [
        {
          label: '普通党员',
          value: '0'
        },
        {
          label: '支部书记',
          value: '1'
        },
        {
          label: '支部委员',
          value: '2'
        },
        {
          label: '党委委员',
          value: '3'
        },
      ],
      form: {
        icResiUser: '',
        name: '',
        mobile: '',
        idCard: '',
        address: '',
        rdsj: '',
        sszb: '',
        isLd: '0',
        ldzh: '',
        partyZw: '0',
        isDyzxh: '0',
        isMxx: '0',
        culture: '',
        remark: ''
      },
      eduList: [],
      rules: {
        sszb: [{ required: true, message: '所属党组织不能为空', trigger: 'blur' }],
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        mobile: [{ required: true, validator: checkMObile, trigger: 'blur' }],
        idCard: [{ required: true, validator: checkIdCard, trigger: 'blur' }],
      },
      partyOrgs: []
    }
  },
  watch: {
    info: {
      handler(val) {
        if (Object.keys(val).length > 0) {
          this.form = { ...val }
          if (val.icResiUserId || val.icResiUser) this.isAuto = true
          console.log('val----------in', val)
          console.log('isAuto----------in', this.isAuto)
          this.partyOrgs = val.orgPids.split(':')
          console.log('partyOrgs-----', this.partyOrgs)
          this.partymenberid = val.id
        }
      },
      immediate: true
    }
  },
  created() {
    this.getEduList()
    console.log('partyList', this.partyList)
  },
  mounted() {
    const w = document.getElementsByClassName('dialog-h-content')[0]
    this.formItemWd = w.clientWidth + 'px'
  },
  destroyed() {
    // this.$refs['ruleForm'].resetFields()
  },
  methods: {
    handleCancle() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('cancle')
    },
    handlePartyChange(val) {
      console.log('val------pp', val)
      // if (val.length > 0) {
      //   const i = val.length - 1
      //   this.form.sszb = val[i]
      // } else this.form.sszb = ''
    },
    handleValidBlur(n) {
      if (!isCard(this.form.idCard)) return
      const { age } = computedCard(this.form.idCard)
      this.form.isMxx = age >= 70 ? '1' : '0'
      this.validateResi()
    },
    async getEduList() {
      await this.$http
        .post('/sys/dict/data/education')
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.eduList = res.data
          } 
        })
        .catch(err => {
          console.log('err', err)
          return this.$message.error('网络错误')
        })
    },
    async validateResi() {
      const params = {
        idCard: this.form.idCard,
        agencyId: this.$store.state.user.agencyId
      }
      await this.$http
        .post('/epmetuser/icresiuser/getUserByIdCard', params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            const { address, icResiUserId } = res.data
            if (icResiUserId) {
              this.form.address = address
              this.isAuto = true
            } else {
              this.form.address = ''
              this.isAuto = false
            }
            
          } 
        })
        .catch(err => {
          console.log('err', err)
          return this.$message.error('网络错误')
        })
    },
    hadnleSubmitbase() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (Object.keys(this.info).length > 0) this.editBase()
          else this.saveBase()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleSavePoints(data) {

      const _data = { ...data }
      const params = {
        ..._data,
        baseOptions: data.baseOptions.length > 0 ? data.baseOptions.toString() : '',
        reviewOptions: data.reviewOptions.length > 0 ? data.reviewOptions.toString() : '',
        inspireOptions: data.inspireOptions.length > 0 ? data.inspireOptions.toString() : '',
        warnOptions: data.warnOptions.length > 0 ? data.warnOptions.toString() : ''
      }
      this.savePoints(params)
    },
    async jump(icResiUserId) {
      this.$emit('saveBase', icResiUserId)
    },
    async saveBase() {
      const params = {
        ...this.form
      }
      await this.$http
        .post('/resi/partymember/icPartyMember/save', params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.$message.success('保存成功')
            this.partymenberid = res.data
            this.$emit('saveBase', true)
            
          } 
        })
        .catch(err => {
          console.log('err', err)
          return this.$message.error('网络错误')
        })
    },
    async editBase() {
      const params = {
        ...this.form
      }
      await this.$http
        .post('/resi/partymember/icPartyMember/update', params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.$message.success('保存成功')
            this.partymenberid = res.data
            this.$emit('saveBase', true)
            
          } 
        })
        .catch(err => {
          console.log('err', err)
          return this.$message.error('网络错误')
        })
    },
    async savePoints(form) {
      const params = {
        ...form,
        partyMemberId: this.partymenberid
      }
      let url = ''
      if (form.id) url = '/resi/partymember/icPartyMemberPoint/update'
      else url = '/resi/partymember/icPartyMemberPoint/save'
      await this.$http
        .post(url, params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.$message.success('保存成功')
            
          } 
        })
        .catch(err => {
          console.log('err', err)
          return this.$message.error('网络错误')
        })
    },
    async saveRecord(form) {
      const params = {
        ...form,
        partyMemberId: this.partymenberid
      }
      await this.$http
        .post('/resi/partymember/icPartyMemberPoint/save', params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            this.$message.success('保存成功')
            
          } 
        })
        .catch(err => {
          console.log('err', err)
          return this.$message.error('网络错误')
        })
    }
  }
}
</script>

<style lang="scss" scoped>
.input-width-textarea {
  width: 400px;
}
.input-width {
  width: 220px;
}
.flex-div {
  display: flex;
  // justify-content: space-between;
}
.wd50 {
  width: 50%;
}
.pdl40 {
  padding-left: 40px;
}
.pd0 {
  padding: 0 !important;
}
</style>