<template>
  <el-dialog
    class="client-data-dialog"
    title="订单分配"
    :visible.sync="visible"
    :before-close="handleClose"
    style="width:90%"
  >
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="140px"
      class="demo-ruleForm"
    >
      <el-form-item label="选择业务组" prop="bizGroup">
        <el-select v-model="ruleForm.bizGroup" placeholder="请选择" @change="handleChange">
          <el-option
            v-for="item in userGroupList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
    
      <el-form-item label="该组休息人员" prop="checkedNames" style="width:75%;height:100px">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="当前组没有休息的人员"
          v-model="ruleForm.checkedNames"
        ></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" style="margin-right:30px">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script type="text/ecmascript-6">
export default {
  name: "ByhassignMarketCase",
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    allIndex: {
      type: String
    },
    planIndex: {
      type: String
    },
    planDate: {
      type: String
    }
  },
  data() {
    return {
      ui: {
        submitLoading: false
      },
      visible: this.$props.dialogVisible,
      ruleForm: {
        bizGroup: "",
        allIndex: "",
        planIndex: "",
        planDate: "",
        checkedNames: ""
      },
      userGroupList: [],
      rules: {
        bizGroup: [
          { required: true, message: "未选择当前业务组", trigger: "blur" }
        ]
      }
    };
  },

  created() {
    const {allIndex,planIndex,planDate} = this;
    this.ruleForm.allIndex = allIndex;
    this.ruleForm.planIndex = planIndex;
    this.ruleForm.planDate = planDate;
  },
  methods: {
    /** 提交点击 */ // 提醒分案记录
    submitForm(ruleForm) {
      if (!this.ruleForm.bizGroup) {
        this.$message({ message: "业务组不能为空", type: "error" });
        return;
      }
      this.$$post("/division/distribution", {
        bizGroup: this.ruleForm.bizGroup,
        planIndex: this.ruleForm.planIndex,
        planDate: this.ruleForm.planDate,
        allIndex: this.ruleForm.allIndex
      })
        .then(res => {
          if (res.data.code < 1) {
            this.$message.error(res.data.msg);
            return;
          }
          this.$message({ message: "分配成功", type: "success" });
          this.handleClose();
          this.$emit("my-event", true);
        })
        .catch(err => {
          alert("error!");
        });
    },

    loadOperate(item) {
      this.$$post("/group/selectGroupUser", this.buildParam(item))
        .then(res => {
          if (res.data.code != 1) {
            this.$message.error(res.data.msg);
            return;
          }
          // 当前休息的人员
          if (res.data.data != null) {
            var restName = new Array();
            res.data.data.forEach(obj => {
              restName.push(obj.loginName);
            });
            const restNum = restName.join(",");
            this.ruleForm.checkedNames = restNum;
          } else {
            this.$message.error(res.data.msg);
            return;
          }
        })
        .catch(error => {
          this.$Message.error(error || "系统异常");
        });
    },

    handleChange(item) {
      // console.log(item)
      this.loadOperate(item);
    },
    handleIconClick(ev) {
      //console.log(ev);
    },
    buildParam(item) {
      var return_hash = {
        status: "NO"
      };

      if (item != "") {
        return_hash["bizGroup"] = item;
      }

      return return_hash;
    },

    /** 关闭处理 */
    handleClose() {
      this.visible = false;
      setTimeout(() => {
        this.$emit("update:dialogVisible", false);
      }, 300);
    }
  },
  mounted: function() {
    // 获取当前用户组
    this.$$get("/group/getGroupList/")
      .then(res => {
        if (res.data.code == 1) {
          if (res.data.code != 1) {
            this.$message.error(res.data.msg);
            this.userGroupList = "";
            return;
          }
          this.userGroupList = res.data.data;
        } else {
          this.$message({ message: res.data.msg, type: "error" });
        }
      })
      .catch(err => this.$$msg.err(err));
  }
};
</script>

<style>
</style>