<template>
  <div style="width:100%;height:50vh;padding:0px 23px; z-index:10;">
    <el-form ref="form" size="mini" :model="addForm" :rules="rules" label-width="140px">

      <el-form-item label="队伍名称: " prop="unitName">
        <el-input v-model="addForm.unitName" style="width:300px ;"></el-input>
      </el-form-item>

      <el-form-item label="队伍类型: " prop="unitType">
        <el-input v-model="addForm.unitType" style="width:300px ;"></el-input>
      </el-form-item>

      <el-form-item label="队伍描述：" prop="description">
        <el-input v-model="addForm.description" style="width:300px ;"></el-input>
      </el-form-item>

      <el-form-item label="队伍图标：" prop="iocn">

        <el-upload
          class="avatar-uploader"
          name="files"
          :action="upload_url"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="addForm.imaUrl" :src="imaUrl_base+'/'+addForm.imaUrl" class="">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>


      <el-form-item style="margin-top: 16%;">
        <el-button @click="close()">取消</el-button>
        <el-button type="primary" @click="submitForm('form')" >保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import { listToTreeSelect } from '@/utils'
  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_API;
  let VUE_APP_BASE_IMG_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 {
        imaUrl_base:VUE_APP_BASE_IMG_URL,
        upload_url:BASE_IMAGE_URL+"/Files/Upload",
        BASE_IMAGE_URL: BASE_IMAGE_URL,
        addForm: {
          unitName:null,
          unitType:null,
          description:null,
          imaUrl:null,
          createTime:new Date(),
          id:0,
          createUserId:"1",

        },
        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,
        orgsTree: [], // 用户可访问到的所有机构组成的树
        selectRoles: [], // 用户分配的角色
        selectRoleNames: '',
        level:0,
      }
    },
    created() {
      this.addForm = this.detailInfo;
      
    },
    computed: {
      selectOrgs: {
        get: function () {
          if (this.detailInfo.length == 1) {
            return this.addForm.areaId
          } else {
            return []
          }
        },
        set: function (v) {
          var _this = this
          _this.addForm.areaId = v
        }
      }
    },
    mounted() {

     
    },
    methods: {
      handleAvatarSuccess(e){
        this.addForm.imaUrl = e.result[0].filePath
      },
      beforeAvatarUpload(e){
        console.log(e);
        return;
      },
      getDetail() {
        getMethodCommon("/FireCodeApp/GetForestryUserById?id=" + this.detailInfo[0].Id, {}).then(res => {
          if (res.code == 200) {
            let detail  = {...res.data[0]};
            this.addForm.id = detail.Id;
            this.addForm.name = detail.Name;
            this.addForm.account = detail.Account;
            this.addForm.sex = detail.Sex;
            this.addForm.userRole = detail.UserRole;
            this.addForm.areaId = detail.areaId;
            // if(this.addForm.areaId){
            //   var areaId = this.addForm.areaId.split(',')
            //   this.addForm.areaId = turnNum(areaId)
            // }
            // var turnNum = function (nums) {
            //   return nums.map(Number);
            // }
          }
        })
      },
      traverse(arr) {
        this.level++;
        if(this.level<3){
          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) {
            // 添加
            postMethodCommon("/FireCodePC/AddOrUpdateUserUnit", _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) {

            // delete this.addForm.zrclist
            postMethodCommon("/FireCodeApp/EditForestryUser ", _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);
  }

  ::v-deep .el-form-item__label {
    color: #333;
  }

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

  }

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

</style>