<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="姓名" prop="name">
          <el-input v-model="dataForm.name" placeholder="姓名" style="width: 260px"></el-input>
      </el-form-item>
<!--        <el-form-item label="党员标识" prop="partyFlag">-->
<!--            <el-select v-model="dataForm.partyFlag" clearable placeholder="党员标识" style="width: 260px">-->
<!--                <el-option v-for="item in partyFlagList" :key="item.dictValue" :label="item.dictName" :value="item.dictValue">-->
<!--                </el-option>-->
<!--            </el-select>-->
<!--        </el-form-item>-->
        <el-form-item label="头像"
                      v-loading="loading"
                      prop="imgUrl">
            <el-upload class="avatar-uploader"
                       :action="uploadUrl"
                       :show-file-list="false"
                       :on-success="handleAvatarSuccess"
                       :before-upload="beforeAvatarUpload">
                <img v-if="dataForm.imgUrl"
                     :src="dataForm.imgUrl"
                     class="avatar">
                <i v-else
                   class="el-icon-plus avatar-uploader-icon"></i>
                <div slot="tip"
                     class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
        </el-form-item>
        <el-form-item label="队伍分类" prop="categoryCode">
            <el-select v-model="dataForm.categoryCode" clearable placeholder="队伍分类" style="width: 260px">
                <el-option v-for="item in gridMemberCategoryList" :key="item.typeCode" :label="item.typeName" :value="item.typeCode">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="所属组织"
                      prop="allDeptIdsShow">
            <el-cascader v-model="dataForm.allDeptIdsShow"
                         :options="options"
                         :props="{  multiple: false, emitPath: true }"
                         clearable
                         @change="changeHandle"
                         collapse-tags
                         style="width: 500px"></el-cascader>
        </el-form-item>
          <el-form-item label="格言" prop="motto">
          <el-input v-model="dataForm.motto" placeholder="格言"></el-input>
      </el-form-item>
          <el-form-item label="职责" prop="duty">
          <el-input v-model="dataForm.duty" placeholder="职责"></el-input>
      </el-form-item>
          <el-form-item label="负责区域" prop="territory">
          <el-input v-model="dataForm.territory" placeholder="负责区域"></el-input>
      </el-form-item>
          <el-form-item label="电话" prop="mobile">
          <el-input v-model="dataForm.mobile" placeholder="电话"></el-input>
      </el-form-item>
<!--          <el-form-item label="所属网格名称" prop="dept">-->
<!--          <el-input v-model="dataForm.dept" placeholder="所属网格名称"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="所属网格" prop="deptId">-->
<!--          <el-input v-model="dataForm.deptId" placeholder="所属网格"></el-input>-->
<!--      </el-form-item>-->

<!--          <el-form-item label="父所有部门" prop="parentDeptIds">-->
<!--          <el-input v-model="dataForm.parentDeptIds" placeholder="父所有部门"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="父所有部门" prop="parentDeptNames">-->
<!--          <el-input v-model="dataForm.parentDeptNames" placeholder="父所有部门"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="所有部门ID" prop="allDeptIds">-->
<!--          <el-input v-model="dataForm.allDeptIds" placeholder="所有部门ID"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="所有部门名称" prop="allDeptNames">-->
<!--          <el-input v-model="dataForm.allDeptNames" placeholder="所有部门名称"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="乐观锁" prop="revision">-->
<!--          <el-input v-model="dataForm.revision" placeholder="乐观锁"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="创建人" prop="createdBy">-->
<!--          <el-input v-model="dataForm.createdBy" placeholder="创建人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="创建时间" prop="createdTime">-->
<!--          <el-input v-model="dataForm.createdTime" placeholder="创建时间"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="更新人" prop="updatedBy">-->
<!--          <el-input v-model="dataForm.updatedBy" placeholder="更新人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="更新时间" prop="updatedTime">-->
<!--          <el-input v-model="dataForm.updatedTime" placeholder="更新时间"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="删除标记" prop="delFlag">-->
<!--          <el-input v-model="dataForm.delFlag" placeholder="删除标记"></el-input>-->
<!--      </el-form-item>-->
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import Cookies from 'js-cookie'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        name: '',
        imgUrl: '',
        // partyFlag: '',
        motto: '',
        duty: '',
        territory: '',
        mobile: '',
        dept: '',
        deptId: '',
        parentDeptIds: '',
        parentDeptNames: '',
        allDeptIds: '',
        allDeptNames: '',
        revision: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: '',
        delFlag: '',
        allDeptIdsShow:[],
        categoryCode:''
      },
        gridMemberCategoryList:[],
        uploadUrl: '',
        loading: false,
        options: [],
        // partyFlagList: [
        //     {dictValue: '0', dictName: '否'},
        //     {dictValue: '1', dictName: '是'},
        // ]
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        imgUrl: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        // partyFlag: [
        //   { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        // ],
        motto: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        duty: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        territory: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' },
          { pattern: /^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$/, message: '请输入正确号码格式', trigger: 'blur' }
        ],
        dept: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        deptId: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        parentDeptIds: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        parentDeptNames: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        allDeptIds: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        allDeptNames: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        revision: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        createdTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedBy: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        updatedTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        delFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
    created () {
        this.getGridMemberCategoryList();
        this.$http
            .get(`/sys/user/deptOptions/getAllByLoginUser`)
            .then(({ data: res }) => {
                if (res.code !== 0) {
                    return this.$message.error(res.msg)
                }
                this.options = res.data.options
            })
            .catch(() => { })
        this.$nextTick(() => {
            this.uploadUrl = `${window.SITE_CONFIG['apiURL']}/oss/file/upload?token=${Cookies.get('token')}`
        })

    },
  methods: {
      getGridMemberCategoryList () {
          this.$http.get(`sys/gridmembercategory/getGridCategoryList`).then(({ data: res }) => {
              this.gridMemberCategoryList = res.data
          }).catch(() => {})
      },
      changeHandle(value) {
          this.dataForm.deptId = this.dataForm.allDeptIdsShow[this.dataForm.allDeptIdsShow.length - 1]
      },
      // 上传图片
      handleAvatarSuccess (res, file) {
          this.loading = false
          this.dataForm.imgUrl = res.data.url
      },
      beforeAvatarUpload (file) {
          this.loading = true
      },
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/sys/gridmember/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/gridmember/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
<style>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>