<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog :visible.sync="showDialog" :title="title" :modal-append-to-body="false" style="text-align: left" @close="closeDialog" :close-on-click-modal="false">
    <div class="user-panel" v-loading="loading">
      <el-form ref="form" :model="formModel" :rules="ruleValidate" :label-width="'100px'">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="formModel.userName" placeholder="请输入用户名" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="真实名称" prop="realName">
          <el-input v-model="formModel.realName" placeholder="请输入真实名称" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="formModel.phone" placeholder="请输入电话号码" maxlength="11" show-word-limit oninput="value=value.replace(/[^\d]/g,'')" style="width: 300px"></el-input>
        </el-form-item>
        <el-form-item label="所属区域" prop="areaId">
          <el-select-tree size="mediumn" style="width: 300px" :props="props" :options="queryAreaResult" v-model="formModel.areaId" height="200"></el-select-tree>
        </el-form-item>
        <el-form-item label="所属角色" prop="roleId">
          <el-select v-model="formModel.roleId" placeholder="请选择" style="width:300px">
            <el-option v-for="role in roleResult" :key="role.id" :label="role.name" :value="role.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import staffApi from "@/api/base/staff";
import areaApi from "@/api/base/area";
import SelectTree from "@/components/SelectTree";

export default {
  props: ["businessKey", "title"],
  data() {
    return {
      formModel: {},
      ruleValidate: {
        userName: [{ required: true, message: "用户名不能为空", trigger: "blur" },],
        realName: [{ required: true, message: "真实名称不能为空", trigger: "blur" },],
        phone: [{ required: true, message: "电话号码不能为空", trigger: "blur" },],
        areaId: [{ required: true, message: "区域不能为空", trigger: "blur" },],
        roleId: [{ required: true, message: "角色不能为空", trigger: "blur" },],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      roleResult:[],
      props: {
        value: "id",
        label: "name",
        children: "children"
      },
      queryAreaResult: [],
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return staffApi.add(self.formModel);
            } else {
              return staffApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success",
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning",
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
  },
  mounted: function () {
    var self = this;

    staffApi.queryRole().then(function(response) {
      var jsonData = response.data;
      if (jsonData.result) {
        self.roleResult = jsonData.data;
      }
    });

    var formData = new FormData();
    areaApi.queryList(formData).then(response=>{
      var jsonData = response.data;
      this.queryAreaResult = jsonData;
    });

    (function () {
      if (self.businessKey.length == 0) {
        return staffApi.create();
      } else {
        return staffApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
  components: {
    "el-select-tree": SelectTree,
  },
};
</script>