<template>
  <Modal v-model="showModalUserInfo" :mask-closable="false" :closable="false" :title="modalTitle" :value="value"
         width="820" v-draggable="options">

    <i-form label-position="right" :label-width="100" ref="userInfoForm" :model="bean" :rules="rules" inline>
      <Row>
        <i-col :span="12">
          <Form-item label="姓名" prop="name" class="ivu-form-item-search">
            <Input v-model="bean.name" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="身份证" prop="idCard" class="ivu-form-item-search">
            <Input v-model="bean.idCard" style="width: 243px;" :disabled="disabled" @on-blur="parseIdCard"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="性别" class="ivu-form-item-search">
            <Select v-model="bean.sex" style="width: 243px" :disabled="disabled">
              <Option :value="'1'" :key="1">男</Option>
              <Option :value="'0'" :key="0">女</Option>
            </Select>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="生日" class="ivu-form-item-search">
            <DatePicker type="date" :value="bean.birthday" :disabled="disabled"
                        @on-change="bean.birthday=$event"
                        placeholder="yyyy.MM.dd"
                        format="yyyy.MM.dd"
                        transfer
                        style="width: 243px">
            </DatePicker>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="年龄" class="ivu-form-item-search">
            <Input v-model="bean.age" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="联系电话" class="ivu-form-item-search">
            <Input v-model="bean.phone" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="邮箱" class="ivu-form-item-search">
            <Input v-model="bean.email" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="联系地址" class="ivu-form-item-search">
            <Input v-model="bean.addr" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="工号" prop="jobNo" class="ivu-form-item-search">
            <Input v-model="bean.jobNo" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="备注" class="ivu-form-item-search">
            <Input v-model="bean.remark" style="width: 243px;" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
      </Row>
    </i-form>
    <div slot="footer">
      <Button @click="cancel">关闭</Button>
      <Button type="primary" :loading="saveLoading" @click="ok('userInfoForm')" v-if="showSaveBtn">保存</Button>
    </div>
  </Modal>
</template>

<script>
  import { addUserInfo, editUserInfo } from '../../../../api/rbac/userInfo-api'

  let $this
  export default {
    name: 'userInfoModal',
    components: {},
    props: {
      show: Boolean,
      value: Object
    },
    data () {
      return {
        showModalUserInfo: false,
        bean: {},
        initBean: {},
        flag: '',
        saveLoading: false,
        modalTitle: '',
        disabled: false,
        showSaveBtn: true,
        rules: {
          name: [
            { required: true, message: '姓名不能为空!', trigger: 'blur' },
          ],
          sex: [
            { required: true, message: '性别不能为空!', trigger: 'blur' },
          ],
          birthday: [
            { required: true, message: '生日不能为空!', trigger: 'blur' },
          ],
          age: [
            { required: true, message: '年龄不能为空!', trigger: 'blur' },
          ],
          idCard: [
            { required: true, message: '身份证不能为空!', trigger: 'blur' },
          ],
          jobNo: [
            { required: true, message: '工号不能为空!', trigger: 'blur' },
          ],
        },
        options: {
          trigger: '.ivu-modal-header',
          body: '.ivu-modal',
          recover: true
        }
      }
    }, methods: {
      ok (name) {
        this.$refs[name].validate((valid) => {
          if (!valid) {
            return
          }//表单校验失败则返回
          this.saveLoading = true
          //this.showModalUserInfo = false;
          if (this.flag == 'edit') {
            let params = this.bean
            delete params['createTime']
            delete params['modifyTime']
            //执行修改操作
            editUserInfo(params).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.closeAndRefresh()
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          } else if (this.flag == 'add') {
            delete this.bean['createTime']
            delete this.bean['modifyTime']
            //执行添加操作
            addUserInfo(this.bean).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.closeAndRefresh()
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          }
        })

      },
      cancel () {
        this.showModalUserInfo = false
        this.$emit('update:show', false)
        //重置校验结果
        this.$refs['userInfoForm'].resetFields()
      },
      closeAndRefresh () {
        this.saveLoading = false
        this.cancel()
        this.$emit('reload-data')
      },
      //--------------
      parseIdCard(){
        this.$myUtil.parseIdCard(this.bean.idCard, (sex, age, birth) =>{
          $this.bean.sex = sex;
          $this.bean.age = age;
          $this.bean.birthday = birth;
          $this.$forceUpdate();
        })
      }
    },
    mounted () {
      $this = this
      this.bean = this.initBean
    },
    watch: {
      show: {
        immediate: true,
        handler: function () {
          this.showModalUserInfo = this.show
        }
      },
      value: {
        immediate: true,
        handler: function () {
          //赋值
          this.bean = this.value.bean == null ? Object.assign({}, this.initBean) : JSON.parse(JSON.stringify(this.value.bean))
          this.flag = this.value.flag
          this.modalTitle = this.value.title
          //如果时查看的功能，要将所有input框只读，并且去掉保存按钮
          if (this.flag == 'view') {
            this.disabled = true
            this.showSaveBtn = false
          } else {
            this.disabled = false
            this.showSaveBtn = true
          }
        }
      }
    }

  }
</script>

<style scoped>
  .ivu-form-item-search {
    margin-bottom: 21px;
  }
</style>
