<template>
  <div v-show="dialogVisible" class="member-container">
    <el-drawer
      :title="title"
      :modal-append-to-body="false"
      :modal="false"
      :wrapper-closable="false"
      size="40%"
      :visible="true"
      @close="dialogVisible=false"
    >
      <div class="content-container">
        <el-form ref="formData" :model="formData" :rules="rules" label-position="top" label-width="100px">
          <el-form-item label="人员" prop="accountId">
            <user-selector :default-val="formData.accountId" @setUserInfo="handleSelectUser" />
          </el-form-item>

          <el-form-item label="分工岗位" prop="positionId">
            <lgp-selector v-model="formData.lgpId" placeholder="请选择分工岗位" style="width:300px;" />
            <!-- <el-button type="primary" style="margin-left:15px" @click="addPosition">添加岗位</el-button> -->
          </el-form-item>

          <el-form-item label="职责描述" prop="duty">
            <el-input v-model="formData.duty" :rows="10" type="textarea" maxlength="500" show-word-limit />
          </el-form-item>

          <el-form-item label="排序">
            <el-input v-model.number="formData.sortNo" type="number" />
          </el-form-item>

          <el-form-item label="备注">
            <el-input v-model="formData.remark" :rows="2" type="textarea" maxlength="300" show-word-limit />
          </el-form-item>
        </el-form>
        <div class="btn-group">
          <el-button type="primary" @click="submitForm('formData')">确 定</el-button>
          <el-button class="cancel-btn" @click="dialogVisible = false">取 消</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { ACTION_ADD, ACTION_UPDATE } from '@/utils/actionType'
import UserSelector from '@/components/UserSelector'
import LgpSelector from '@/views/system/leadingGroup/components/LgpSelector'
import { createLeadingGroupMember, updateLeadingGroupMember } from '@/api/system/leadingGroupMemberApi'
export default {
  name: 'LeadingGroupMember',
  components: {
    UserSelector,
    LgpSelector
  },
  props: {
    title: {
      type: String,
      default: '添加成员'
    }
  },
  data() {
    return {
      dialogVisible: false,
      formData: {
        lgId: undefined,
        lgpId: undefined,
        lgmId: undefined,
        duty: '',
        remark: '',
        sortNo: 0,
        groupId: undefined,
        positionId: undefined,
        accountId: undefined
      },
      rules: {
        lgId: [
          { required: true, message: '请选择用户', trigger: 'blur' }
        ],
        accountId: [
          { required: true, message: '请选择用户', trigger: 'blur' }
        ],
        duty: [
          { required: true, message: '请填写职责描述', trigger: 'blur' }
        ],
        positionId: [
          { required: true, message: '请选择分工岗位', trigger: 'blur' }
        ]
      },
      positions: [],
      viewData: {},
      actionType: ''
    }
  },
  methods: {

    // 添加
    showAddModel(lgId, viewData) {
      this.resetFormData()
      this.actionType = ACTION_ADD
      this.formData.lgId = lgId
      this.dialogVisible = true
      this.viewData = viewData
    },

    // 编辑
    showEditModel(data, viewData) {
      this.resetFormData()
      this.formData.lgId = data.lgId
      this.formData.lgpId = data.lgpId
      this.formData.lgmId = data.lgmId
      this.formData.accountId = data.accountId
      this.formData.positionId = data.positionId
      this.formData.groupId = data.groupId
      this.formData.duty = data.duty
      this.formData.remark = data.remark
      this.formData.sortNo = data.sortNo

      this.actionType = ACTION_UPDATE
      this.dialogVisible = true
      this.viewData = viewData
    },

    // 执行人选择
    handleSelectUser(item) {
      this.formData.positionId = item.positionId
      this.formData.groupId = item.groupId
      this.formData.accountId = item.accountId
    },

    resetFormData() {
      this.formData = {
        lgId: undefined,
        lgpId: undefined,
        lgmId: '',
        duty: '',
        remark: '',
        sortNo: 0,
        groupId: undefined,
        positionId: undefined,
        accountId: undefined
      }
    },

    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          switch (this.actionType) {
            case ACTION_ADD:
              createLeadingGroupMember(this.formData).then((resp) => {
                const { code, msg } = resp
                if (code === 200) {
                  this.dialogVisible = false
                  this.formSuccess()
                  this.$message.success(msg)
                } else {
                  this.$message.error(msg)
                }
              }).catch((error) => {
                console.log(error)
              })
              break

            case ACTION_UPDATE:
              updateLeadingGroupMember(this.formData).then((resp) => {
                const { code, msg } = resp
                if (code === 200) {
                  this.dialogVisible = false
                  this.formSuccess()
                  this.$message.success(msg)
                } else {
                  this.$message.error(msg)
                }
              }).catch((error) => {
                console.log(error)
              })
              break
          }
        } else {
          this.$message({ type: 'error', message: 'error submit!!' })
        }
      })
    },

    formSuccess() {
      this.$emit('formSuccess')
    },

    resetForm(formName) {
      this.$refs[formName].resetFields()
    },

    // 添加分工岗位
    addPosition() {
      this.$refs['LeadingGroupPosition'].handler(null, this.viewData)
    },
    positionAddSuccess() {

    }
  }
}
</script>
<style lang="scss">
</style>
<style lang="scss" scoped>
</style>
