module.exports = function(compoenntName) {
  return `<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="5vh"
    width="1040px"
    @close="closeModel"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            ref="ruleForm"
            label-width="110px"
            class="demo-ruleForm"
          >
            <el-form-item
              label="登录名"
              prop="loginName"
              v-if="id === 'add' || isDetail"
            >
              <el-input
                v-model="ruleForm.loginName"
                :disabled="isDetail"
              ></el-input>
            </el-form-item>
            <el-form-item label="姓名" prop="fullName">
              <el-input
                v-model="ruleForm.fullName"
                :disabled="isDetail"
              ></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="tel">
              <el-input v-model="ruleForm.tel" :disabled="isDetail"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password" v-if="id === 'add'">
              <el-input
                type="password"
                placeholder="密码"
                :maxlength="20"
                v-model="ruleForm.password"
              ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="password2" v-if="id === 'add'">
              <el-input
                type="password"
                placeholder="再次输入密码"
                :maxlength="20"
                v-model="ruleForm.password2"
              ></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right;">
          <el-button type="primary" @click="submitForm" v-if="!isDetail"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{ isDetail ? "关 闭" : "取 消" }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
   </template>
   <script>
   import asyncRequest from "@/apis/service/${compoenntName}";

   export default {
    name: '${compoenntName}',
    props: ["showModel", "id", "isDetail"],
    data() {
      let validatePass = (rule, value, callback) => {
        if (value === "" || value === undefined) {
          callback(new Error("请输入新密码"));
        } else {
          if (value.length < 6 || value.length > 15) {
            callback(new Error("必须是6到15位,支持数字、字母、符号组合"));
          } else {
            if (this.ruleForm.password2 !== "") {
              this.$refs.ruleForm.validateField("password2");
            }
            callback();
          }
        }
      };
      let validatePass2 = (rule, value, callback) => {
        if (value === "" || value === undefined) {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.ruleForm.password) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          if (value.length < 6 || value.length > 15) {
            callback(new Error("必须是6到15位,支持数字、字母、符号组合"));
          } else {
            callback();
          }
        }
      };
      return {
        loading: false,
        title: "添加管理人员",
        showModelThis: this.showModel,
        ruleForm: {
          tel: "",
          fullName: "",
          loginName: "",
          passport: ""
          // isAdmin: 0
        },
        rulesThis: this.rules,
        rules: {
          loginName: [
            {
              required: true,
              message: "请输入登录名",
              trigger: "blur"
            },
            {
              min: 2,
              max: 30,
              message: "长度在 2 到 30 个字符",
              trigger: "blur"
            }
          ],
          fullName: [
            {
              required: true,
              message: "请输入姓名",
              trigger: "blur"
            },
            {
              min: 2,
              max: 30,
              message: "长度在 2 到 30 个字符",
              trigger: "blur"
            }
          ],
          password: [
            {
              required: true,
              validator: validatePass,
              trigger: "blur"
            }
          ],
          password2: [
            {
              required: true,
              validator: validatePass2,
              trigger: "blur"
            }
          ],
          tel: [
            {
              required: true,
              message: "请输入手机号码",
              trigger: "blur"
            },
            {
              validator: this.$rulesPhone,
              trigger: "blur"
            }
          ]
        }
      };
    },
    methods: {
      closeModel() {
        console.log("closeModel!!");
      },
      async initForm() {
        if (this.id === "add") {
          this.title = "添加管理人员";
          // this.ruleForm.isAdmin = 0;
          this.loading = false;
          this.rulesThis = this.rules;
          await this.resetForm();
        } else {
          if (this.isDetail) {
            this.title = "管理人员详情";
            this.rulesThis = {};
          } else {
            this.title = "修改管理人员";
            this.rulesThis = this.rules;
          }
          await this.resetForm();
          await this.initData();
        }
      },
      async initData() {
        this.loading = true;
        let res = await asyncRequest.detail({id:this.id});
        this.loading = false;
        if (res.code === 0) {
          this.ruleForm = res.data;
        }
      },
      async resetForm() {
        // 重置
        await this.$nextTick(() => {
          if (this.$refs.ruleForm) {
            this.$refs.ruleForm.resetFields();
            this.$refs.ruleForm.clearValidate();
            this.ruleForm = {
              tel: "",
              fullName: "",
              loginName: "",
              passport: ""
              // isAdmin: 0
            };
          }
        });
      },
      async submitForm() {
        await this.$refs.ruleForm.validate(async valid => {
          if (valid) {
            this.loading = true;
            let obj = JSON.parse(JSON.stringify(this.ruleForm));
            let res = {};
            if (this.id === "add") {
              obj.passport = obj.password;
              res = await asyncRequest.add(obj);
            } else {
              res = await asyncRequest.update(obj);
            }
            this.loading = false;
            if (res.code === 0) {
              let title = this.id === "add" ? "添加成功" : "修改成功";
              this.$notify.success({
                title,
                message: ""
              });
              this.showModelThis = false;
              // 刷新
              this.$emit("refresh");
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      }
    },
    watch: {
      showModel: function(val) {
        this.showModelThis = val;
        if (val) {
          this.initForm();
        }
      },
      showModelThis(val) {
        if (!val) {
          this.$emit("cancel");
        }
      }
    }
  };
  </script>

   <style lang="scss" scoped>
   .${compoenntName} {
    
   }
   </style>
   `;
};
