<template>
  <a-modal
    centered
    class="supplierInfoEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTitle"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 表单 -->
      <div v-if="!isView">
        <a-form-model
          id="supplierInfoEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="姓名" prop="name">
            <a-input
              id="supplierInfoEdit-name"
              :maxLength="10"
              v-model.trim="form.name"
              @change="filterEmpty"
              placeholder="请输入姓名(最多10个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="身份证号">
            <a-input
              id="supplierInfoEdit-idCard"
              :maxLength="18"
              v-model.trim="form.idCard"
              placeholder="请输入身份证号"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="手机号码" prop="mobile">
            <a-input
              id="supplierInfoEdit-mobile"
              :maxLength="11"
              v-model.trim="form.mobile"
              placeholder="请输入手机号码(最多11个字符)"
              allowClear />
          </a-form-model-item>
          <a-form-model-item label="入职时间" prop="entryDate">
            <a-date-picker
              v-model="form.entryDate"
              type="date"
              :disabled-date="disabledDate"
              :defaultValue="null"
              placeholder="请选择入职时间"
              style="width: 100%;"
            />
          </a-form-model-item>
          <a-form-model-item label="性别" prop="sex">
            <a-radio-group
              name="radioGroup"
              v-model="form.sex"
              id="supplierInfoEdit-enabledFlag" >
              <a-radio :value="1">男</a-radio>
              <a-radio :value="0">女</a-radio>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item label="状态" prop="enableFlag">
            <a-radio-group
              name="radioGroup"
              v-model="form.enableFlag"
              id="enableFlag" >
              <a-radio :value="1">启用</a-radio>
              <a-radio :value="0">禁用</a-radio>
            </a-radio-group>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="supplierInfoEdit-btn-submit" @click="handleSubmit">保存</a-button>
          <a-button id="supplierInfoEdit-btn-back" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
        </div>
      </div>
      <!-- 详情 -->
      <a-descriptions size="small" :column="2" v-if="isView">
        <a-descriptions-item label="姓名">{{ this.form.name || '--' }}</a-descriptions-item>
        <a-descriptions-item label="身份证号">{{ this.form.idCard || '--' }}</a-descriptions-item>
        <a-descriptions-item label="联系电话">{{ this.form.mobile || '--' || '--' }}</a-descriptions-item>
        <a-descriptions-item label="入职时间">{{ this.form.entryDate || '--' || '--' }}</a-descriptions-item>
        <a-descriptions-item label="性别">{{ this.form.sexDictValue || '--' || '--' }}</a-descriptions-item>
        <a-descriptions-item label="状态">{{ this.form.enableFlagDictValue || '--' || '--' }}</a-descriptions-item>
      </a-descriptions>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { employeeSave, employeeDetail } from '@/api/salesman'
import moment from 'moment'
export default {
  name: 'SupplierInfoEditModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: [Number, String],
      default: ''
    },
    modalTitle: {
      type: String,
      default: ''
    },
    isState: { //  是否显示状态
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      isView: false, // 是否查看详情
      form: {
        name: '', // 姓名
        mobile: '', //  联系电话
        enableFlag: 1, //  启禁用
        sex: 1, // 性别
        idCard: '', // 身份证号
        entryDate: undefined // 入职时间
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }, { pattern: '^[^<|>]{1,30}$', message: '请输入不含<或>的字符，最多30个字符' }
        ],
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' }, { pattern: /^\d{11}$/, message: '请输入正确的手机号码!' }
        ],
        entryDate: [
          { required: true, message: '请选择入职时间', trigger: 'change' }

        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        enableFlag: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 不可选日期
    disabledDate (current) {
      return current && current > moment().endOf('day')
    },
    filterEmpty () {
      let str = this.form.supplierName
      str = str.replace(/\ +/g, '')
      str = str.replace(/[ ]/g, '')
      str = str.replace(/[\r\n]/g, '')
      this.form.supplierName = str
    },
    // 详情
    getDetail () {
      employeeDetail({ id: this.itemId }).then(res => {
        if (res.status == 200) {
          this.form = Object.assign(this.form, res.data)
          this.form.sex = Number(this.form.sex)
          this.form.enableFlag = Number(this.form.enableFlag)
        }
      })
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = _this.form
          form.entryDate = moment(form.entryDate).format('YYYY-MM-DD')
          form.id = this.itemId ? this.itemId : null
          _this.spinning = true
          employeeSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.isShow = false
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cancel () {
      this.form = {
        name: '', // 姓名
        mobile: '', //  联系电话
        enableFlag: 1, //  启禁用
        sex: 1, // 性别
        idCard: '', // 身份证号
        entryDate: undefined // 入职时间
      }
      // this.$nextTick(() => {
      //   this.$refs.ruleForm.resetFields()
      // })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
      this.cancel()
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    },
    itemId (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    },
    modalTitle (newValue, oldValue) {
      console.log(newValue)
      if (newValue) {
        if (newValue == '编辑' || newValue == '新增') {
          this.isView = false
        } else {
          this.isView = true
        }
      }
    }

  }
}
</script>

<style lang="less">
  .supplierInfoEdit-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
