<template>
  <div class="container">
    <el-row>
      <el-col :span="22">
        <el-form ref="form" :model="memberFrom" label-width="120px" :rules="rules">
          <el-form-item label="普通成员" prop="ordinary">
            <el-checkbox-group v-model="memberFrom.ordinary">
              <el-checkbox v-for="city in datalist" :label="city.userId" :key="city.userId">
                {{
                city.userName
                }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>

          <el-form-item>
            <el-button type="success" icon="el-icon-upload2" @click="submitForm()">提交</el-button>
            <el-button type="info" icon="el-icon-refresh-left" @click="resetForm()">清空选择</el-button>
            <el-button @click="back()">返回</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { secletAllStaff } from "@/api/user";
import {
  selectAllByProjectId,
  insertBatch,
  selectByProjectId
} from "@/api/member";
export default {
  data() {
    return {
      memberFrom: {
        ordinary: []
      },
      staffList: [],
      rules: {
        ordinary: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个普通成员",
            trigger: "change"
          }
        ]
      },
      memberQurey: {
        projectId: null,
        pageNum: 1,
        pageSize: 10
      },
      memberList: [],
      //过滤后面的数组
      datalist: [],
      //过滤后的新数组
      queryList: [],
      memberId: null,
    };
  },
  computed: {
    projectId() {
      return this.$route.query.id;
    }
  },
  created() {
    this.memberQurey.projectId = this.$route.query.id;
    this.memberId = this.$store.getters.memberId;


    
    //查询员工
    //this.getStaffList();
    //查询项目的成员信息
    this.getPeojectMember();
    this.getselectlist();
  },
  methods: {
    getselectlist() {
      //查询员工，得到初始员工值
      secletAllStaff({ companyId: this.$store.getters.companyId })
        .then(res => {
          if (res.status == "success") {
            this.staffList = res.data;
            //数据整理，排除项目负责人
            selectAllByProjectId({ projectId: this.projectId })
              .then(res => {
                if (res.success) {
                  let queryList = res.data;
                  console.log(queryList);
                  if (this.memberId != 0) {
                    let list = [];
                    //查出来是项目负责人组成一个数组
                    list = queryList.filter(a => {
                      return a.memberPression == 0;
                    });
                    const datalistyou = list;;
                    //将项目负责人与查询员工分离整理
                    let datalist1 = this.staffList.filter(
                      a => !datalistyou.some(b => a.userId === b.userId)
                    );
                    this.datalist = datalist1;
                  } else {
                    //排除普通成员
                    let list = [];
                    //查出来是项目成员组成一个数组
                    list = queryList.filter(a => {
                      return a.memberPression == 1;
                    });
                    const datalistyou = list;
                    //console.log(list);
                    console.log(this.$route.query.memberId);
                    //将项目负责人与查询员工分离整理
                    let datalist1 = this.staffList.filter(
                      a => !datalistyou.some(b => a.userId === b.userId)
                    );
                    this.datalist = datalist1;
                  }
                }
              })
              .catch(error => {
                this.$message.error("获取员工信息失败");
              });
          } else {
            this.$message.error(res.data);
            this.back();
          }
        })
        .catch(error => {
          this.$message.error("获取员工信息失败");
          //this.back();
        });
    },
    //查询项目的成员信息
    getPeojectMember() {
      selectAllByProjectId({ projectId: this.projectId })
        .then(res => {
          if (res.success) {
            //处理数据
            if (res.data.length > 0) {
              if (this.memberId != 0) {
                let ordinary = [];
                res.data.forEach(e => {
                  if (e.memberPression != 0) {
                    //普通员工
                    ordinary.push(e.userId);
                  }
                });
                this.memberFrom.ordinary = ordinary;
              } else {
                let ordinary = [];
                res.data.forEach(e => {
                  if (e.memberPression == 0) {
                    //项目负责人
                    ordinary.push(e.userId);
                  }
                });
                this.memberFrom.ordinary = ordinary;
              }
            } else {
              this.memberFrom.ordinary = [];
            }
          } else {
            this.$message.error(res.msg);
            this.back();
          }
        })
        .catch(error => {
          this.$message.error("获取项目的成员信息失败");
          this.back();
        });
    },
    //提交
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          //处理数据
          let memberList = this.handleMember();
          insertBatch(memberList)
            .then(res => {
              if (res.success) {
                this.$message.success(res.msg);
                this.$store.dispatch("tagsView/delView", this.$route);
                this.$router.push({
                  path: "/memberList",
                  query: {
                    id: this.$route.query.id
                  }
                });
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              this.$message.error("添加失败");
            });
        }
      });
    },
    //清空选择
    resetForm() {
      this.$refs.form.resetFields();
    },
    //返回
    back() {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({
        path: "/memberList",
        query: {
          id: this.$route.query.id,
          memberId:this.memberId
        }
      });
    },
    //处理成员数据
    handleMember() {
      //0负责人，1普通员工
      let member = [];
      if (this.memberId != 0) {
        this.memberFrom.ordinary.forEach(e => {
          member.push({
            projectId: this.projectId,
            userId: e,
            memberPression: 1
          });
        });
      }else{
        this.memberFrom.ordinary.forEach(e => {
          member.push({
            projectId: this.projectId,
            userId: e,
            memberPression: 0
          });
        });
      }
      return member;
    }
  }
};
</script>

<style scoped>
.container {
  padding: 10px;
}
</style>