<template>
  <div>
    <q-card flat class="q-pa-lg q-my-lg" v-loading="loading">
      <div class="text-h6 text-weight-bold">分配给以下部门/成员</div>
      <div class="text-subtitle1 text-weight-bold q-my-md">
        <span>适用部门/成员</span>
        <span class="text-negative">*</span>
      </div>
      <div class="q-mb-md row">
        <div class="col">
          <q-btn
            outline
            color="primary"
            class="q-px-lg"
            @click="outerVisible = true"
            >选择部门/成员</q-btn
          >
        </div>
        <div class="col">已选择：{{ chosenUser.length }}人</div>
      </div>
      <div class="q-mb-xl">
        <q-badge
          color="primary"
          class="q-mr-md"
          v-for="(x, i) in chosenUser"
          :key="i"
        >
          {{ x.DeptName }}
        </q-badge>
      </div>
      <div class="text-h6 text-weight-bold q-mt-xl">设置分配规则</div>
      <div class="text-subtitle1 text-weight-bold q-my-md row items-center">
        <div>选择分配规则：</div>
        <div class="col q-ml-md">
          <q-radio v-model="ruleType" val="0" label="顺序分配" />
          <q-radio v-model="ruleType" val="1" label="随机分配" />
        </div>
      </div>
      <q-btn
        class="q-mt-xl q-px-lg"
        unelevated
        color="primary"
        :loading="saving"
        @click="saveHandler"
        >立即保存</q-btn
      >
    </q-card>
    <choiceapproval
      v-if="outerVisible"
      @close="getclose()"
      @success="getsuccess2"
      :byval="chosenUser"
      cptype="0"
    ></choiceapproval>
  </div>
</template>

<script>
import choiceapproval from "../../components/enterprise/choiceapproval";
import { getDictValueList } from "../../api/administration/administration";
import { saveDict } from "../../api/system/notiveSysSet";
export default {
  components: { choiceapproval },
  data() {
    return {
      chosenUser: [],
      outerVisible: false,
      ruleType: "0",
      saving: false,
      loading: true,
      dictObjEmp: {},
      dictObjRule: {}
    };
  },
  created() {
    this.init();
  },
  methods: {
    getclose() {
      // 企业关闭
      this.ismember = false;
      this.outerVisible = false;
    },
    getsuccess2(list, type) {
      this.chosenUser = list;
      this.outerVisible = false;
    },
    init() {
      getDictValueList({ Key: "PUSH_STU_UNION_EMP" }).then(res => {
        if (res.Code == 1) {
          this.dictObjEmp = res.Data[0];
          if (res.Data[0].Content != "" && res.Data[0].Content != null) {
            let tempVal = res.Data[0].Content.split(",");
            let tempLabel = res.Data[0].Mask.split(",");
            tempVal.forEach((x, i) => {
              let temp = {
                DeptId: x,
                DeptName: tempLabel[i]
              };
              this.chosenUser.push(temp);
            });
          }
          getDictValueList({ Key: "PUSH_STU_RULETYPE" }).then(r => {
            this.dictObjRule = r.Data[0];
            if (r.Code == 1) {
              if (r.Data[0].Content != "" && res.Data[0].Content != null) {
                this.ruleType = r.Data[0].Content;
              }
            }
            this.loading = false;
          });
        }
      });
    },
    saveHandler() {
      if (this.chosenUser.length > 0) {
        this.saving = true;
        let contentTemp = "";
        let maskTemp = "";
        this.chosenUser.forEach(x => {
          contentTemp += "," + x.DeptId;
          maskTemp += "," + x.DeptName;
        });
        this.dictObjEmp.Content = contentTemp.substring(1);
        this.dictObjEmp.Mask = maskTemp.substring(1);

        saveDict(this.dictObjEmp).then(r => {
          if (r.Code == 1) {
            setTimeout(() => {
              this.dictObjRule.Content = this.ruleType;
              saveDict(this.dictObjRule).then(res => {
                if (r.Code == 1) {
                  this.$q.notify({
                    icon: "iconfont icon-chenggong",
                    color: "accent",
                    timeout: 2000,
                    message: "数据保存成功！",
                    position: "top"
                  });
                }
                this.saving = false;
              });
            }, 1000);
          } else {
            this.saving = false;
          }
        });
      } else {
        this.$q.notify({
          icon: "",
          color: "negative",
          timeout: 2000,
          message: "请选择适用的部门成员",
          position: "top"
        });
      }
    }
  }
};
</script>

<style></style>
