<template>
  <div style="width:100%;height:40vh;padding:0px 23px; z-index:10;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="name">
        <el-input v-model="addForm.name" 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="gender">
        <el-radio-group v-model="addForm.gender">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item :label="'所属乡镇'">
        <el-select v-model="addForm.street" multiple placeholder="请选择">
          <el-option v-for="item in zrcList" :key="item.id" :label="item.areaName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="margin-top: 16%;">
        <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 { listToTreeSelect } from '@/utils'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  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'],
    components: {
    },
    data() {
      return {
        BASE_IMAGE_URL: BASE_IMAGE_URL,
        addForm: {
          gender: "男"
        },
        rules: {
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' },
          ],
          phone: [
            { required: true, message: '电话不能为空', trigger: 'blur' },
            { validator: validateMobile.bind(this), trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别不能为空', trigger: 'blur' },
          ],
        },
        dialogFormVisible: false,
        zrcList:[]
      }
    },
    created() {
      console.log(this.detailInfo)
      if (this.detailInfo.length == 1) {
        this.getDetail()
      } else {
        this.addForm = {
          gender: "男"
        }
      }
    },
    mounted() {
      this.getZRC()
    },
    methods: {
      getZRC() {
        getMethodCommon("/FireCodeApp/LoadZRC", {}).then(res => {
          if (res.code == 200) {
            this.zrcList = res.data
            if (this.addForm.Sex == 0) {
              this.addForm.Sex = '男'
            } else {
              this.addForm.Sex = '女'
            }
          }
        })
      },
      getDetail() {
        getMethodCommon("/FireGrid/GetSingleFireFighter?id=" + this.detailInfo[0].id, {}).then(res => {
          if (res.code == 200) {
            console.log(res)
            this.addForm = res.result
          }
        })
      },
      traverse(arr) {
        if (arr && arr.length > 0) {
          var list = []
          arr.forEach(childelement => {
            list.push({
              id: childelement.id,
              label: childelement.areaName,
              parentId: childelement.pId || null,
              children: this.traverse(childelement.child)
            })
          });
          return list
        }
      },
      submitForm() {
        var _this = this
        this.$refs['form'].validate((valid) => {
          if (valid) {
            console.log(_this.addForm)
            postMethodCommon("/FireGrid/AddFireFighter", _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)
            postMethodCommon("/FireGrid/EditFireFighter", _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 .vue-treeselect__control {
    background: rgba(0, 9, 34, 0.6);
  }

  ::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>