<template>
  <div style="width:100%;height:100%;overflow:auto;padding:0px 23px; background: rgba(0, 9, 34, 0.6);">
    <el-form ref="form" size="mini" :model="addForm" :rules="rules" label-width="140px">
      <el-form-item label="姓名: " prop="userName">
        <el-input v-model="addForm.userName" style="width:300px ;"></el-input>
      </el-form-item>

      <el-form-item label="身份证号: " prop="cardNo" >
        <el-input v-model="addForm.cardNo" style="width:300px ;"></el-input>
      </el-form-item>

      <el-form-item label="电话: " prop="phone">
        <el-input v-model="addForm.phone" style="width:300px ;"></el-input>
      </el-form-item>


      <el-form-item label="性别: " prop="sex">
        <el-radio-group v-model="addForm.sex">
          <el-radio label="0">男</el-radio>
          <el-radio label="1">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="政治面貌：" prop="politicalOutlook">
        <el-select v-model="addForm.politicalOutlook" placeholder="请选择" style="width:300px ;">
          <el-option label="群众" value="群众"></el-option>
          <el-option label="中共党员" value="中共党员"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学历：" prop="education">
        <el-select v-model="addForm.education" placeholder="请选择" style="width:300px ;">
          <el-option label="小学" value="小学"></el-option>
          <el-option label="初中" value="初中"></el-option>
          <el-option label="高中" value="高中"></el-option>
          <el-option label="专科" value="专科"></el-option>
          <el-option label="本科" value="本科"></el-option>
          <el-option label="研究生" value="研究生"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="close()">取消</el-button>
        <el-button type="primary" @click="submitForm('form')" v-if="detailInfo.length == 0">保存</el-button>
        <el-button type="primary" @click="createForm('form')" v-else>编辑</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import { postMethodCommon, getMethodCommon } from '../../../../api/common';
  import { validateMobile, validID, validateStock } from './validate.js'
  import form from '../../../../store/modules/form';
  let BASE_IMAGE_URL = process.env.VUE_APP_BASE_IMG_URL;
  // let BASE_IMAGE_URL = BASE_IMAGE_API_URL;
  export default {
    name: "AddForm",
    props:['detailInfo'],
    data() {
      return {
        BASE_IMAGE_URL: BASE_IMAGE_URL,
        addForm: {
          sex: "0"
        },
        rules: {
          userName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' },
          ],
          phone: [
            { required: true, message: '电话不能为空', trigger: 'blur' },
            { validator: validateMobile.bind(this), trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' },
          ],
          cardNo: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' },
            { validator: validID.bind(this), trigger: 'blur' }
          ],
          politicalOutlook: [
            { required: true, message: '政治面貌不能为空', trigger: 'blur' },
          ],
          education: [
            { required: true, message: '学历不能为空', trigger: 'blur' },
          ],
        },

      }
    },
    created() {
      console.log(this.detailInfo)
      if(this.detailInfo.length == 1){
        this.addForm=this.detailInfo[0]
        this.addForm.sex = this.addForm.sex.toString()
      }else{
        this.addForm={
          sex:"0"
        }
      }
    },
    methods: {
     
      submitForm() {
        var _this =this 
        this.$refs['form'].validate((valid) => {
          if (valid) {
            console.log(_this.addForm)
            _this.addForm.sex = Number(_this.addForm.sex)
            postMethodCommon("/Grid/AddKeyUser", _this.addForm).then(response => {
              if (response.code == 200) {
                // 上传shp
                this.$emit("addSuccess");
                this.$message({
                  type:"success",
                  message:"添加成功"
                })
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      createForm(){
        var _this =this 
        this.$refs['form'].validate((valid) => {
          if (valid) {
            console.log(_this.addForm)
            _this.addForm.sex = Number(_this.addForm.sex)
            postMethodCommon("/Grid/EditKeyUser", _this.addForm).then(response => {
              if (response.code == 200) {
                // 上传shp
                this.$emit("addSuccess");
                this.$message({
                  type:"success",
                  message:"编辑成功"
                })
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      close() {
        this.$emit("close");
      }
    }
  }
</script>
<style scoped>
  ::v-deep .el-form-item__label {
    color: #fff;
  }

  ::v-deep .el-radio {
    color: #fff;

  }

  .service-type-btn {
    width: 100%;
    height: 180px;
    text-align: center;
    margin-top: 100px;
  }
</style>