<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="接收对象" prop="userName">
        <el-input
          v-model="form.userName"
          placeholder="选择人员"
          readonly
          suffix-icon="el-icon-user-solid"
          @focus="handleSelectUser" />
      </el-form-item>
      <el-form-item label="转移原因" prop="remark">
        <el-input
          v-model="form.remark"
          maxlength="300"
          placeholder="请输入转移原因"
          rows="5"
          show-word-limit
          type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
    <!-- 选择负责人弹窗 -->
    <select-user ref="selectUser" @save="selectUser" />
  </el-dialog>
</template>

<script>
  import businessApi from '@/api/proj/business'
  import SelectUser from '@/components/select/SelectUser'

  export default {
    name: 'Transfer',
    components: { SelectUser },
    data() {
      return {
        form: {
          id: undefined,
          userId: undefined,
          userName: undefined,
          remark: undefined,
        },
        rules: {
          userName: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
          remark: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
        },
        title: '转移项目',
        dialogFormVisible: false,
      }
    },
    methods: {
      handleSelectUser() {
        this.$refs.selectUser.open()
      },
      selectUser(val) {
        if (val && val.length > 0) {
          this.form.userId = val[0].id
        }
        this.form.userName = val.map((item) => item.nickName).join()
      },
      open(row) {
        this.form.id = row.id
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await businessApi.businessTransfer(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
