<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="5vh"
    width="900px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="closeModel"
  >
    <el-card>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rulesThis"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-form-item label="组名称" prop="name">
              <el-input
                v-model="ruleForm.name"
                placeholder="请输入组名称"
                :disabled="isDetail"
              />
            </el-form-item>
            <el-form-item label="组成员" prop="group_user">
              <el-select
                v-model="ruleForm.group_user"
                multiple
                filterable
                clearable
                :disabled="isDetail"
                class="setWidth"
              >
                <el-option
                  v-for="item in activeOptions"
                  :key="`group_user` + item.id"
                  :label="item.nickname"
                  :value="item.id"
                  :disabled="item.status !== '1'"
                >
                  <span style="float: left">{{ item.nickname }}</span>
                  <span
                    style="
                      float: right;
                      color: #8492a6;
                      font-size: 13px;
                      padding: 0 25px 0 0;
                    "
                    >{{ item.role_name }}</span
                  >
                </el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="备注" prop="group_remark">
              <el-input
                v-model="ruleForm.group_remark"
                type="textarea"
                placeholder="请输入备注"
                :rows="5"
                maxlength="300"
                show-word-limit
                :disabled="isDetail"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button v-if="!isDetail" type="primary" @click="submitForm"
            >保 存
          </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/interest/group";
import resToken from "@/mixins/resToken";
export default {
  name: "Group",
  props: ["showModel", "id", "isDetail"],
  mixins: [resToken],
  data() {
    return {
      loading: false,
      title: "添加用户组",
      showModelThis: this.showModel,
      ruleForm: {
        id: this.id,
        name: "",
        group_user: [],
        group_remark: "",
      },
      rulesThis: this.rules,
      rules: {
        name: [
          {
            required: true,
            message: "请输入组名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        group_user: [
          {
            required: true,
            message: "请选择数据共享用户",
            trigger: "change",
          },
        ],
        group_remark: [
          {
            required: true,
            message: "请输入备注",
            trigger: "blur",
          },
          {
            min: 2,
            max: 300,
            message: "长度在 2 到 300 个字符",
            trigger: "blur",
          },
        ],
      },

      dataForm: {
        name: "", // 业务员名字
        status: "", //
      },
      activeOptions: [],
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    closeModel() {
      console.log("closeModel!!");
    },
    async initForm() {
      await this.getAllActive();
      this.loading = true;
      if (this.id === "add") {
        this.title = "添加用户组";
        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();
      }
      this.loading = false;
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.id });
      if (res && res.code === 0) {
        const { group_name, group_user, group_remark } = res.data;
        const arr = group_user.split(",");
        this.ruleForm = {
          id: this.id,
          name: group_name,
          group_user: arr,
          group_remark: group_remark,
        };
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.ruleForm = {
            id: this.id,
            name: "",
            group_user: [],
            group_remark: "",
          };
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const obj = JSON.parse(JSON.stringify(this.ruleForm));
          const arr = obj.group_user.join();
          obj.group_user = arr;
          let res = {};
          if (this.id === "add") {
            delete obj["id"];
            res = await asyncRequest.add(obj);
          } else {
            res = await asyncRequest.update(obj);
          }
          this.loading = false;
          if (res && res.code === 0) {
            const title = this.id === "add" ? "添加成功" : "修改成功";
            this.$notify.success({
              title,
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async getAllActive() {
      const res = await asyncRequest.accountall(this.dataForm);
      if (res && res.code === 0 && res.data) {
        this.activeOptions = res.data;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
  },
};
</script>

   <style lang="scss" scoped>
.group {
  .setWidth {
    width: 100% !important;
    // display: flex;
    // display: block !important;
    // .el-input__inner {
    //   display: flex;
    //   width: 1000px !important;
    //   display: block !important;
    // }
  }
}
</style>
