<template>
  <el-dialog
    title="修改"
    :visible.sync="visible"
    width="600px"
    append-to-body
    @close="close"
  >
    <el-form
      ref="addForm"
      :model="addForm"
      label-position="left"
      size="mini"
      label-width="80px"
      :rules="rules"
    >
      <el-row :gutter="36">
        <el-col :span="24">
          <el-form-item label="机构号" prop="orgId">
            <el-input v-model="addForm.orgId" placeholder="请输入" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="层级" prop="level">
            <el-select style="width: 100%" v-model="addForm.level" disabled>
              <el-option
                v-for="item in orgLevelArr"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="行员号" prop="userId">
            <el-input
              v-model="addForm.userId"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="行员姓名" prop="userName">
            <el-input
              v-model="addForm.userName"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { updateModel } from '@/api/configure/levelConfig.js'
import { Message } from 'element-ui'
export default {
  props: {
    dialogVisible: Boolean,
    orgLevelArr: Array,
    rowInfo: Object
  },
  data() {
    return {
      addForm: {
      },
      rules: {
        orgId: [
          {
            required: true,
            message: "请输入机构号",
            trigger: "blur",
          },
        ],
        level: [
          {
            required: true,
            message: "请选择层级",
            trigger: "change",
          },
        ],
        userId: [
          {
            required: true,
            message: "请输入行员号",
            trigger: "blur",
          },
        ],
        userName: [
          {
            required: true,
            message: "请输入行员姓名",
            trigger: "blur",
          },
        ],
      },
      visible: false,
    };
  },
  computed: {
  },
  watch: {
    dialogVisible(newVal, olaVal) {
      this.visible = newVal
    },
    rowInfo: {
      handler(newVal) {
        this.addForm = {
          orgId: newVal.orgId,
          level: newVal.level,
          userId: newVal.userId,
          userName: newVal.userName,
        }
      },
      deep: true
    }
  },
  methods: {
    close() {
      this.visible = false
      this.$refs.addForm.resetFields()
      this.$emit("close")
    },
    submit() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
            updateModel({
              id: this.rowInfo.id,
              ...this.addForm
            }).then(res => {
                if (res.code === 200) {
                Message.success('修改成功!')
                this.close()
                this.$emit("fresh")
                }
            })
        }
      });
    },
  },
};
</script>
