<template>
  <el-dialog title="更新成员信息" :visible.sync="dialogFormVisible" @close="close">
    <el-form ref="form" label-position="top" :model="form" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目经理" prop="projectManId">
            <el-input
              v-model="form.projectManName"
              readonly
              suffix-icon="el-icon-search"
              @focus="handleSelectUser('projectManId', 'projectManName')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交付经理" prop="deliverManId">
            <el-input
              v-model="form.deliverManName"
              readonly
              suffix-icon="el-icon-search"
              @focus="handleSelectUser('deliverManId', 'deliverManName')" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button :disabled="!form.deliverManId && !form.projectManId" type="primary" @click="save">确 定</el-button>
    </template>

    <!-- 选择支持人员弹窗 -->
    <select-user ref="selectUser" @save="selectUser" />
  </el-dialog>
</template>

<script>
  import deliverApi from '@/api/work/deliver'
  import SelectUser from '@/components/select/SelectUser'

  export default {
    name: 'WorkOrderFeedback',
    components: {
      SelectUser,
    },
    props: {},
    data() {
      return {
        form: {
          deliverManId: undefined,
          deliverManName: undefined,
          projectManId: undefined,
          projectManName: undefined,
          id: 0,
        },
        rules: {
          // deliverManId: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
          // deliverManName: [{ required: true, message: '不能为空', trigger: ['blur', 'change'] }],
        },
        dialogFormVisible: false,
        orderTypeList: [],
        dingtalkForm: undefined,
        curKey: '',
        curName: '',
      }
    },
    mounted() {},
    methods: {
      handleSelectUser(key, name) {
        this.curKey = key
        this.curName = name
        this.$refs.selectUser.open()
      },
      selectUser(val) {
        if (val && val.length > 0) {
          this.form[this.curKey] = val[0].id
          this.form[this.curName] = val.map((item) => item.nickName).join()
        }
      },
      open(row) {
        this.form = {
          deliverManId: row.deliverManId,
          deliverManName: row.deliverManName,
          projectManId: row.projectManId,
          projectManName: row.projectManName,
          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 deliverApi.updateDeliverOrder(this.form)
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        })
      },
    },
  }
</script>
