<template>
  <div class="dialog-h-content scroll-h pd0">
    <el-form label-width="120px" :model="form" :inline="true" :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="optionsG"
                :props="partyProps"
                clearable
                @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="是" value="1"></el-radio>
              <el-radio label="否" value="0"></el-radio>
            </el-radio-group>
          <!-- <el-select v-model="form.isDyzxh" filterable
          :disabled="disabled" placeholder="请选择" class="input-width" clearable>
              <el-option
                v-for="item in optionsEditG"
                :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="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="是" value="1"></el-radio>
            <el-radio label="否" value="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="是" value="1"></el-radio>
            <el-radio label="否" value="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 :style="'width:' + formItemWd" >
        <div style="margin-top: 20px; text-align: center;">
            <el-button size="small">取消</el-button>
            <el-button
              v-if="!disabled"
              type="primary"
              size="small"
              >提交</el-button
            >
          </div>
      </div>
      
    </el-form>
    <div class="pdl40">
      <el-tabs v-model="activesName">
        <el-tab-pane label="量化积分" name="first">
          <pointsInfo />
        </el-tab-pane>
        <el-tab-pane label="缴费记录" name="second">
          <record />
        </el-tab-pane>
      </el-tabs>
      <!-- <el-button-group>
        <el-button class="diy-button--add" size="small">量化积分</el-button>
        <el-button size="small">缴费记录</el-button>
      </el-button-group> -->
    </div>
    <!-- <div class="resi-btns">
      <el-button size="small" @click="handlerCancle">取消</el-button>
      <el-button
        v-if="!disabled"
        type="primary"
        size="small"
        :loading="btnLoading"
        @click="handleSUbmit"
        >提交</el-button
      >
    </div> -->
  </div>
</template>

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

import pointsInfo from './cpts/points.vue'
import record from './cpts/record.vue'

export default {
  components: {
    pointsInfo,
    record
  },
  props: {
    partyList: {
      type: Array,
      default: () => []
    }
  },
  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',
      disabled: false,
      isAuto: false,
      formItemWd: '100%',
      partyProps: {
        label: 'partyOrgName',
        value: 'partyOrgCode'
      },
      zwList: [
        {
          label: '普通党员',
          value: '0'
        },
        {
          label: '支部书记',
          value: '1'
        },
        {
          label: '支部委员',
          value: '2'
        },
        {
          label: '党委委员',
          value: '3'
        },
      ],
      form: {
        icResiUser: '',
        name: '',
        mobile: '',
        idCard: '',
        address: '',
        rdsj: '',
        sszb: '',
        isLd: '',
        ldzh: '',
        partyZw: '',
        isDyzxh: '',
        isMxx: '',
        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' }],
      }
    }
  },
  created() {
    this.getEduList()
  },
  mounted() {
    const w = document.getElementsByClassName('dialog-h-content')[0]
    this.formItemWd = w.clientWidth + 'px'
  },
  methods: {
    handlePartyChange(val) {
      console.log('val------pp', val)
    },
    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
            }
            
          } 
        })
        .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>