<template>
  <div id="resumeCenter">
    <search-box
      ref="search_box"
      :formList="formList"
      @search="search"
      @init="init"
    />
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="addClick(props.scope.row, 2)"
          >详情</el-button
        >
        <el-button
          type="text"
          v-if="props.scope.row.status == 0"
          @click="addClick(props.scope.row, 0)"
          >邀请</el-button
        >
        <el-button
          type="text"
          v-if="props.scope.row.status == 0"
          @click="addClick(props.scope.row, 1)"
          >拒绝</el-button
        >
        <el-button type="text" v-if="props.scope.row.status == 1"
          >已邀请</el-button
        >
        <el-button type="text" v-if="props.scope.row.status == 3"
          >已谢绝</el-button
        >
        <el-button type="text" v-if="props.scope.row.status == 2"
          >已确定</el-button
        >
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="searchPamam.pageSize"
      :currentPage="searchPamam.pageNum"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />

    <el-dialog
      :visible.sync="calendarDialogVisible"
      width="30%"
      custom-class="inviteDialog"
      center
    >
      <div style="min-width: 360px">
        <div class="d_title">
          <div>当前共有 {{ peopleNum }} 人需要面试</div>
          <div class="d_title_r">查看全部</div>
        </div>
        <div class="week_box">
          <i
            class="el-icon-arrow-left"
            style="cursor: pointer; margin-top: 5px"
            @click="changeWeek(-1)"
          ></i>
          <div v-for="(item, index) in weekDay" :key="index" class="card_date">
            <div>{{ item }}</div>
            <div
              v-if="weekDayList.length > 0"
              :class="
                dayValue.getDate() == weekDayList[index].num
                  ? 'selCircle'
                  : 'selNone'
              "
              @click="selDay(weekDayList[index], index)"
            >
              {{ weekDayList[index].num }}
            </div>
          </div>
          <i
            class="el-icon-arrow-right"
            style="cursor: pointer; margin-top: 5px"
            @click="changeWeek(1)"
          ></i>
        </div>
        <div
          v-for="(item, index) in recruitList"
          :key="index"
          style="display: flex; padding: 20px"
        >
          <div>{{ item.timeStr }}</div>
          <div style="margin-left: 30px">
            <div v-for="(item_c, index_c) in item.list" :key="index_c">
              {{ item_c.job }}-{{ item_c.recruitResumeVo.realname
              }}<span v-if="index_c < item.list.length - 1">，</span>
            </div>
          </div>
        </div>
        <div>
          <el-form
            :model="form"
            :rules="rules"
            label-width="80px"
            ref="inviteForm"
          >
            <el-form-item label="公司" prop="companyName">
              <el-input v-model="form.companyName"></el-input>
            </el-form-item>
            <el-form-item label="时间" prop="resumeTime">
              <el-date-picker
                type="datetime"
                placeholder="选择时间"
                :picker-options="pickerOptions"
                v-model="form.resumeTime"
                value-format="timestamp"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="地点" prop="address">
              <el-input v-model="form.address"></el-input>
            </el-form-item>
            <el-form-item label="电话" prop="linkTel">
              <el-input v-model="form.linkTel"></el-input>
            </el-form-item>
            <el-form-item label="联系人" prop="linkMan">
              <el-input v-model="form.linkMan"></el-input>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="calendarDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm(0)">发送邀请</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="refuseDialogVisible" width="30%" center>
      <div style="min-width: 360px">
        <div style="margin-top: 30px">
          <div style="width: 100%">
            <el-radio v-model="radio" label="1">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                :disabled="radio == '2'"
                v-model="refuseText1"
              >
              </el-input>
            </el-radio>
          </div>
          <div style="margin-top: 30px">
            <el-radio v-model="radio" label="2"> 自定义原因 </el-radio>
            <div style="margin-left: 25px; margin-top: 10px; width: 90%">
              <el-input
                type="textarea"
                :rows="2"
                :disabled="radio == '1'"
                placeholder="请输入内容"
                v-model="refuseText2"
              >
              </el-input>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="refuseDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm(1)">确认</el-button>
      </span>
    </el-dialog>
    <resumes ref="resumes" @addClicks="addClick"></resumes>
  </div>
</template>

<script>
import searchBox from "@/components/searchBox";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
import resumes from "@/components/resumexq";
import { mapGetters } from "vuex";
export default {
  components: { searchBox, tableList, pagination, resumes },
  data() {
    return {
      pickerOptions: {
        selectableRange:
          this.parseTime(
            new Date().setMinutes(new Date().getMinutes() + 1),
            "{hh}:{ii}:{ss}"
          ) + "- 23:59:00",
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      radio: "1",
      refuseText1:
        "您的简历已经纳入我们公司的人才储备库，如果有适合您的岗位，我们会第一时间通知您，祝您求职成功，心想事成。",
      refuseText2: "",
      refuseForm: {
        refuse: "",
      },
      refuseDialogVisible: false,
      dayValue: new Date(),
      calendarDialogVisible: false,
      weekDay: ["一", "二", "三", "四", "五", "六", "日"],
      weekDayList: [],
      weekDay1: new Date(),
      weekDay2: new Date(),
      recruitList: [],
      dayInterviewVos: [],
      form: {
        companyName: "",
        linkMan: "",
        linkTel: "",
        address: "",
        resumeTime: "",
      },
      peopleNum: 0,
      loading: false, //当前表单加载是否加载动画
      searchPamam: {
        pageSize: 10, //每页多少条数据
        pageNum: 1, //当前页码
        statusArray: "0,1",
      },
      navText: {
        title: "简历中心",
        index: 0,
        ch: "条",
        num: true,
        choice: false,
        addHide: true,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      //搜索
      formList: [
        {
          label: "应聘岗位",
          prop: "job",
          placeholder: "请输入应聘岗位",
        },
        {
          label: "工作年限",
          prop1: "workStart",
          prop2: "workEnd",
          scope: "numList",
        },
        {
          label: "年龄",
          prop1: "ageStart",
          prop2: "ageEnd",
          scope: "numList",
        },
        {
          label: "学历",
          prop: "eduLevel",
          scope: "select",
          options: [],
          placeholder: "请选择学历",
        },
        {
          label: "简历完成度(%)",
          prop: "completeness",
          scope: "inputNumber",
          placeholder: "请输入简历完成度",
        },
      ],
      // 表单
      tableSet: [
        {
          label: "姓名",
          prop: "resumeRealName",
          hidden: true,
        },
        {
          label: "性别",
          prop: "sexLabel",
          hidden: true,
        },
        {
          label: "工作年限",
          prop: "workYear",
          hidden: true,
        },
        {
          label: "年龄",
          prop: "age",
          hidden: true,
        },
        {
          label: "邮箱",
          prop: "email",
          hidden: true,
        },
        {
          label: "联系电话",
          prop: "telphone",
          hidden: true,
          width: "200px",
        },
        {
          label: "期望薪资",
          prop: "wantIncomeLabel",
          hidden: true,
          width: "200px",
        },
        {
          label: "地点",
          prop: "district",
          hidden: true,
          width: "240px",
        },
        {
          label: "应聘岗位",
          prop: "job",
          hidden: true,
          width: "300px",
        },
        {
          label: "工作类型",
          prop: "jobType",
          hidden: true,
          width: "240px",
        },
        {
          label: "视频介绍",
          prop: "recruitResumeVo",
          prop1: "introduceVideoUrl",
          hidden: true,
          scope: "video",
          width: "300px",
        },
        {
          label: "简历完成度",
          prop: "completenessLabel",
          hidden: true,
          width: "240px",
        },
        {
          label: "投递时间",
          scope: "aTimeList",
          prop: "createTime",
          hidden: true,
          width: "300px",
        },
        {
          label: "附件简历",
          prop: "recruitResumeVo",
          prop1: "attachUrl",
          prop2: "attachName",
          scope: "PDF",
          hidden: true,
          width: "240px",
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
      // 弹窗字段
      listitem: [
        {
          label: "字典名称",
          prop: "dictName",
        },
        {
          label: "字典类型",
          prop: "dictType",
        },
        {
          label: "状态",
          prop: "status",
          scope: "status",
          options: [
            {
              label: "启用",
              value: "1",
            },
            {
              label: "关闭",
              value: "0",
            },
          ],
        },
        {
          label: "备注",
          prop: "remark",
          scope: "textarea",
        },
      ],
      //   弹窗数据
      listData: {},
      statusPop: -1,
      dialogVisible: false,
      //表单验证
      rules: {
        linkTel: [
          { required: true, message: "请填写联系电话", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请填写面试地址", trigger: "blur" },
        ],
        companyName: [
          { required: true, message: "请填写公司名称", trigger: "blur" },
        ],
        linkMan: [{ required: true, message: "请填写联系人", trigger: "blur" }],
        resumeTime: [
          { required: true, message: "请选择面试时间", trigger: "blur" },
        ],
      },
      companyGMdata: [], //公司规模
      companyLXdata: [], //公司类型
      statusIndexList: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    };
  },
  computed: { ...mapGetters(["UserDict"]) },
  created() {
    // if (this.$route.params.job) {
    //   this.searchPamam.job = this.$route.params.job;
    // }
  },
  mounted() {
    this.search();
    this.searchCalendar();
    this.getNowTime();
    this.pushDict();
  },
  methods: {
    pushDict() {
      var arrays = [];
      this.UserDict["edu_level"].forEach((item, index) => {
        arrays.push({ label: item.dictLabel, value: item.dictLabel });
      });
      this.formList.forEach((item, index) => {
        if (item.prop === "eduLevel") {
          item.options = arrays;
        }
      });
    },
    editrecruitinterviewRefuse(refuse) {
      let data = JSON.parse(JSON.stringify(this.refuseForm));
      data.status = 3;
      data.refuse = refuse;
      let that = this;
      this.$api.editrecruitinterview(data).then((res) => {
        if (res.code == 200) {
          that.calendarDialogVisible = false;
          that.refuseDialogVisible = false;
          that.search();
        }
      });
    },
    editrecruitinterview() {
      let data = JSON.parse(JSON.stringify(this.form));
      data.status = 1;
      data.resumeTime = parseInt(data.resumeTime / 1000);
      let that = this;
      this.$api.editrecruitinterview(data).then((res) => {
        if (res.code == 200) {
          that.calendarDialogVisible = false;
          that.search();
        }
      });
    },
    submitForm(type) {
      if (type == 0) {
        this.$refs["inviteForm"].validate((valid) => {
          if (valid) {
            this.editrecruitinterview();
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      }
      if (type == 1) {
        let refuse = "";
        if (this.radio == "1") {
          if (this.refuseText1 == "") {
            this.$message({
              message: "请输入拒绝原因",
              type: "warning",
            });
            return;
          }
          refuse = this.refuseText1;
        }
        if (this.radio == "2") {
          if (this.refuseText2 == "") {
            this.$message({
              message: "请输入拒绝原因",
              type: "warning",
            });
            return;
          }
          refuse = this.refuseText2;
        }
        this.editrecruitinterviewRefuse(refuse);
      }
    },
    getNowTime() {
      let t = new Date();
      let newTime =
        t.getTime() -
        (t.getMinutes() * 60 + t.getSeconds()) * 1000 +
        3600 * 1000;
      t.setTime(newTime);
      this.dayValue = t;
    },
    makeRecruitList(quantumList) {
      this.form.resumeTime = this.dayValue.getTime();
      this.recruitList = [];
      this.peopleNum = 0;
      for (let i = 0; i < quantumList.length; i++) {
        let item = quantumList[i];
        if (item.recruitInterviewList.length > 0) {
          this.peopleNum += item.recruitInterviewList.length;
          let timeStr = item.timingStart + "点 ~ " + item.timingEnd + "点";
          let newItem = { timeStr: timeStr, list: item.recruitInterviewList };
          this.recruitList.push(newItem);
        }
      }
    },
    changeWeek(index) {
      if (index == -1) {
        this.dayValue.setTime(
          this.dayValue.getTime() - 7 * 24 * 60 * 60 * 1000
        );
      }
      if (index == 1) {
        this.dayValue.setTime(
          this.dayValue.getTime() + 7 * 24 * 60 * 60 * 1000
        );
      }
      this.searchCalendar();
    },
    selDay(day, index) {
      this.dayValue = day.day;
      this.makeRecruitList(this.dayInterviewVos[index].quantumList);
    },
    searchCalendar() {
      let that = this;
      this.loading = true;
      var data = {
        year: this.dayValue.getFullYear(),
        month: this.dayValue.getMonth() + 1,
        day: this.dayValue.getDate(),
        statusIds: this.statusIndexList.toString(),
      };
      this.$api.recruitinterviewlist(data).then((res) => {
        if (res.code == 200) {
          that.dayInterviewVos = res.data.dayInterviewVos;
          that.weekDayList = [];
          for (let i = 0; i < that.dayInterviewVos.length; i++) {
            if (res.data.dayInterviewVos[i].date == that.dayValue.getDate()) {
              that.currIndex = i;
              that.makeRecruitList(res.data.dayInterviewVos[i].quantumList);
            }
            let weekDay = {
              num: that.dayInterviewVos[i].date,
            };
            that.weekDayList.push(weekDay);
          }
          for (let i = 0; i < that.weekDayList.length; i++) {
            var d = new Date();
            d.setTime(
              that.dayValue.getTime() +
                (i - that.currIndex) * 24 * 60 * 60 * 1000
            );
            that.weekDayList[i].day = d;
            if (i == 0) {
              that.weekDay1 = d;
            }
            if (i == 6) {
              that.weekDay2 = d;
            }
          }
          console.log(that.weekDayList, 99);
        }
      });
      this.loading = false;
    },
    search(v, os) {
      if (os) {
        this.currentPage = 1;
      }
      this.loading = true;
      let that = this;
      var data = JSON.parse(JSON.stringify(this.searchPamam));
      if (v !== undefined) {
        data = Object.assign(data, v);
      }
      this.$api.recruitinterviewlistAll(data).then((res) => {
        if (res.code == 200) {
          that.tableData = res.rows;
          that.total = res.total;
          that.navText.index = res.total;
          let nowYear = new Date().getFullYear();
          for (let i = 0; i < that.tableData.length; i++) {
            let item = that.tableData[i];
            item.sexLabel = item.sex == 1 ? "男" : "女";
            item.wantIncomeLabel =
              item.recruitResumeVo.wantIncomeStart +
              "-" +
              item.recruitResumeVo.wantIncomeEnd;
            item.resumeRealName = item.recruitResumeVo.realname;
            item.telphone = item.recruitResumeVo.telphone;
            item.email = item.recruitResumeVo.email;
            item.workYear = item.recruitResumeVo.startYear
              ? nowYear - item.recruitResumeVo.startYear + "年"
              : "暂无数据";
            item.jobType = item.recruitResumeVo.jobType;
            item.completenessLabel = item.recruitResumeVo.completeness + "%";
            let userBirth = new Date(item.recruitResumeVo.userBirth);
            let birthday = [
              userBirth.getFullYear(),
              userBirth.getMonth() + 1,
              userBirth.getDate(),
            ];
            item.age =
              item.recruitResumeVo.userBirth == null
                ? "暂无数据"
                : this.getAge(birthday)[0] + "岁";
          }
        }
      });

      this.loading = false;
    },
    getAge(birthday) {
      // 新建日期对象
      let date = new Date();
      // 今天日期，数组，同 birthday
      let today = [date.getFullYear(), date.getMonth() + 1, date.getDate()];
      // 分别计算年月日差值
      let age = today.map((value, index) => {
        return value - birthday[index];
      });
      // 当天数为负数时，月减 1，天数加上月总天数
      if (age[2] < 0) {
        // 简单获取上个月总天数的方法，不会错
        let lastMonth = new Date(today[0], today[1], 0);
        age[1]--;
        age[2] += lastMonth.getDate();
      }
      // 当月数为负数时，年减 1，月数加上 12
      if (age[1] < 0) {
        age[0]--;
        age[1] += 12;
      }
      return age;
    },
    init() {
      this.search();
    },
    addClick(v, int) {
      if (int == 0) {
        this.calendarDialogVisible = true;
        this.form.companyName = v.companyName;
        this.form.address = v.city + " " + v.district + " " + v.companyAddress;
        this.form.linkMan = v.linkMan;
        this.form.linkTel = v.linkTel;
        this.form.interviewId = v.interviewId;
        return;
      }
      if (int == 1) {
        this.refuseDialogVisible = true;
        this.refuseForm.interviewId = v.interviewId;
        return;
      }
      if (int == 2) {
        this.$refs.resumes.getCInfos(v);
        this.$refs.resumes.init(v.interviewId);
        // this.$refs.resumes.dialogBox = true
      }
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.rulesTableSumbit();
        } else {
          return false;
        }
      });
    },
    rulesTableSumbit() {
      if (this.statusPop === 1) {
        this.$message.success("新增成功");
        this.dialogVisible = false;
      }
      if (this.statusPop === 0) {
        this.$message.success("修改成功");
        this.dialogVisible = false;
      }
    },
    close() {
      if (this.statusPop === 2) {
        this.dialogVisible = false;
        return;
      }
      this.$confirm("是否关闭该窗口", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.dialogVisible = false;
        })
        .catch(() => {});
    },
    handleSizeChange(v) {
      this.searchPamam.pageSize = v;
      this.searchPamam.pageNum = 1;
      this.search(this.$refs.search_box.formData);
    },
    handleCurrentChange(v) {
      this.searchPamam.pageNum = v;
      this.search(this.$refs.search_box.formData);
    },
  },
  watch: {
    "form.resumeTime": {
      handler(newValue, oldValue) {
        if (newValue) {
          let date = new Date();
          if (date.getTime() + 24 * 3600 * 1000 < newValue) {
            this.pickerOptions.selectableRange = "00:00:00 - 23:59:59";
            this.pickerOptions = this.pickerOptions;
          } else {
            let date1 = new Date();
            date1.setTime(newValue);
            if (date.getDate() >= date1.getDate()) {
              this.pickerOptions.selectableRange =
                this.parseTime(
                  date.setMinutes(date.getMinutes() + 1),
                  "{hh}:{ii}:{ss}"
                ) + "- 23:59:59";
              this.pickerOptions = this.pickerOptions;
            } else {
              this.pickerOptions.selectableRange = "00:00:00 - 23:59:59";
              this.pickerOptions = this.pickerOptions;
            }
          }
        }
      },
      deep: true,
      immediate: true,
    },
  },
};
</script>
<style lang="less" scoped>
/deep/ .el-dialog {
  min-width: 400px;
}
/deep/ .el-cascader-node > .el-radio,
.el-radio:last-child {
  width: 90%;
}
#resumeCenter .el-dialog {
  min-width: 400px !important;
}
</style>
<style lang="less" scoped>
.selNone {
  margin: 13px auto;
  cursor: pointer;
  border-radius: 50%;
  border: 1px solid #000000;
  width: 30px;
  height: 30px;
  text-align: center;
  line-height: 30px;
}
.selCircle {
  border-radius: 50%;
  width: 30px;
  height: 30px;
  color: #ffffff !important;
  background-color: #3494f7;
  text-align: center;
  line-height: 30px;
  margin: 13px auto;
  cursor: pointer;
}
.card_date {
  width: 14%;
  text-align: center;
  color: #000;
}
.week_box {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
.d_title_r {
  color: #3494f7;
  cursor: pointer;
}
.d_title {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
</style>

