<!--
 * @Description: create
 * @Version: 1.0
 * @Autor: XuTongZhang
 * @Date: 2020-07-28 15:25:06
 * @LastEditors: XuTongZhang
 * @LastEditTime: 2020-08-07 09:55:05
-->
<template>
  <div class="indexPage">
    <v-input
      :btn="isCollapse?isCollapse===1?btn1:btn2:btn0"
      :list="list"
      :key="isCollapse+3"
      @del="delAll"
      @search="search"
      @through="throughAll"
      @notice="noticeAll"
      @eliminate="eliminateAll"
    ></v-input>
    <el-radio-group v-model="isCollapse" size="medium" style="margin-bottom: 20px;">
      <el-radio-button :label="0">候选人列表</el-radio-button>
      <el-radio-button :label="1">已通过候选人</el-radio-button>
      <el-radio-button :label="2">已淘汰候选人</el-radio-button>
    </el-radio-group>
    <v-table
      :key="isCollapse"
      :table="isCollapse===2?table2:table"
      :tableList="tableList"
      :sortType="true"
      :form="searchForm"
      :queryData="queryData"
      @details="details"
      @eliminate="eliminate"
      @through="through"
      @notice="notice"
      @del="del"
      @selection-change="selection"
      id="id"
    ></v-table>
    <v-pager @page="callPage" :total="totalrecords"></v-pager>
    <el-dialog
      :visible.sync="dialogFormVisible"
      width="600px"
      :before-close="close"
      :show-close="false"
      >
      <el-form :model="form" ref="form" label-width="140px" :rules="rules" label-position="left">
        <el-form-item label-width="0">
          <div class="title">通知群发列表</div>
          <div class="content">
            <el-tag
              v-for="(item, index) in copyPickList"
              :key="item"
              closable
              @close="copyPickList.splice(index, 1)"
              type="success"
            >{{tableList.some(i => i.id === item) ? tableList.find(i => i.id === item).delivererName : ''}}</el-tag>
          </div>
        </el-form-item>
        <el-form-item label="选择群发模板" prop="templateId">
          <el-select v-model="form.templateId" placeholder="请选择群发模板">
            <el-option
              v-for="item in downList"
              :key="item.id"
              :label="item.templateName"
              :value="item.id"
            ></el-option>
          </el-select>
          <!-- <div v-else>{{this.downList1.some(i => i.id === form.processId) ? this.downList1.find(i => i.id === form.processId).processName : ''}}</div> -->
        </el-form-item>
        <el-form-item label="群发邮箱">139820930@qq.com</el-form-item>
        <el-form-item>
          <el-button type="primary" @click="launch">立即发出</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <resume :dialogFormVisible="dialogVisible" :close="closeDia" :info="info" :searchForm="searchForm" :queryData="queryData"></resume>
  </div>
</template>

<script>
import resume from './resume'
export default {
  data () {
    return {
      dialogFormVisible: false,
      dialogVisible: false,
      tableList: [],
      page: 1,
      isCollapse: 0,
      totalrecords: 0,
      pickList: [],
      copyPickList: [],
      downList: [],
      searchForm: {},
      form: {},
      info: {},
      rules: {
        templateId: [
          { required: true, message: '请选择群发模板', trigger: 'blur' }
        ]
      },
      list: [
        {
          placeholder: '请输入关键字查询',
          props: 'condition'
        },
        {
          type: 'select',
          placeholder: '性别',
          props: 'delivererSex',
          options: [
            { label: '男', value: 1 },
            { label: '女', value: 0 }
          ]
        },
        {
          type: 'select',
          placeholder: '来源',
          props: 'resumeFrom',
          options: [
            { label: '51job', value: 0 },
            { label: '智联', value: 1 },
            { label: '58同城', value: 2 }
          ]
        },
        {
          placeholder: '请输入投递职位',
          props: 'positionApplied'
        },
        {
          type: 'select',
          placeholder: '应聘职位',
          props: 'desiredPositionId',
          options: []
        },
        {
          type: 'date'
        }
      ],
      btn0: [
        {
          name: '确定',
          type: 'primary',
          method: 'search'
        },
        {
          name: '标记通过',
          type: 'success',
          method: 'through'
        },
        {
          name: '标记淘汰',
          type: 'danger',
          method: 'eliminate'
        }
      ],
      btn1: [
        {
          name: '确定',
          type: 'primary',
          method: 'search'
        },
        {
          name: '通知成功',
          type: 'success',
          method: 'notice'
        }
      ],
      btn2: [
        {
          name: '确定',
          type: 'primary',
          method: 'search'
        },
        {
          name: '标记通过',
          type: 'success',
          method: 'through'
        },
        {
          name: '删除',
          type: 'danger',
          method: 'del'
        }
      ],
      table: {
        selection: true,
        column: [
          {
            label: '编号',
            props: 'id'
          },
          {
            label: '姓名',
            props: 'delivererName'
          },
          {
            label: '性别',
            props: 'delivererSex',
            options: ['女', '男']
          },
          {
            label: '简历来源',
            props: 'resumeFrom',
            options: ['51job', '智联招聘', '58同城']
          },
          {
            label: '招聘类型',
            props: 'recruitType',
            options: ['社招', '校招', '直接校招']
          },
          {
            label: '简历投递职位',
            props: 'positionApplied'
          },
          {
            label: '面试应聘职位',
            props: 'desiredPositionId'
          },
          {
            label: '工作年限',
            props: 'delivererWorkExp'
          },
          {
            label: '学历',
            props: 'delivererEducation'
          },
          {
            label: '联系电话',
            props: 'delivererPhone'
          },
          {
            label: '通知面试时间',
            props: 'sendMailTime'
          },
          {
            label: '完成面试时间',
            props: 'confirmInterviewTime'
          }
        ],
        width: 310,
        handle: [
          {
            title: '查看简历',
            method: 'details',
            type: 'info'
          },
          {
            title: '标记通过',
            method: 'through',
            type: 'success'
          },
          {
            title: '标记淘汰',
            method: 'eliminate',
            type: 'danger'
          }
        ]
      },
      table2: {
        selection: true,
        column: [
          {
            label: '编号',
            props: 'id'
          },
          {
            label: '姓名',
            props: 'delivererName'
          },
          {
            label: '性别',
            props: 'delivererSex',
            options: ['女', '男']
          },
          {
            label: '简历来源',
            props: 'resumeFrom',
            options: ['51job', '智联招聘', '58同城']
          },
          {
            label: '简历投递职位',
            props: 'positionApplied'
          },
          {
            label: '面试应聘职位',
            props: 'desiredPositionId'
          },
          {
            label: '工作年限',
            props: 'delivererWorkExp'
          },
          {
            label: '学历',
            props: 'delivererEducation'
          },
          {
            label: '联系电话',
            props: 'delivererPhone'
          },
          {
            label: '被标记未通过时间',
            props: 'rejectTime'
          },
          {
            label: '理由',
            props: 'rejectReason'
          }
        ],
        width: 300,
        handle: [
          {
            title: '查看简历',
            method: 'details',
            type: 'info'
          },
          {
            title: '标记通过',
            method: 'through',
            type: 'success'
          },
          {
            title: '删除',
            method: 'del',
            type: 'danger'
          }
        ]
      }
    }
  },
  mounted () {
    this.queryData()
    this.queryDownList()
  },
  components: {
    resume
  },
  methods: {
    queryData (form = {}) {
      let page = this.page
      this.searchForm = form
      let reqdata = form
      reqdata.interviewResult = this.isCollapse
      this.$api
        .post('/resumeInfo/queryCandidateList', {
          reqdata,
          page
        })
        .then((res) => {
          this.totalrecords = res.totalrecords
          this.tableList = res.list
        })
    },
    queryDownList () {
      this.$api
        .post('/position/queryPositionList', {
          reqdata: {}
        })
        .then((res) => {
          this.list[4].options = res.list.map((item) => ({
            value: item.id,
            label: item.positionName
          }))
        })
      this.$api
        .post('/template/queryTemplateList', {
          reqdata: {}
        })
        .then((res) => {
          this.downList = res.list
        })
    },
    search (form) {
      this.queryData(form)
    },
    details (row) {
      this.openDia()
      this.info = row
    },
    launch () {
      let a
      this.$refs['form'].validate((valid) => {
        a = valid
      })
      if (!a) return
      this.copyPickList.length
        ? this.$api
          .post('/resumeInfo/sendInviteMail', {
            reqdata: {
              ids: this.copyPickList,
              templateId: this.form.templateId
            }
          })
          .then((res) => {
            this.$message({
              message: '通知成功',
              type: 'success'
            })
            this.close()
            this.queryData(this.searchForm)
          })
        : this.$message({ type: 'info', message: '请添加需要通知的对象' })
    },
    notice (row) {
      this.open()
      this.copyPickList = [row.id]
    },
    noticeAll () {
      this.pickList.length
        ? this.open()
        : this.$message({ type: 'info', message: '请选择需要通知的对象' })
    },
    through (row, type = false) {
      let ids = type ? row : [row.id]
      let resumeInfoStatusList = ids.map(item => ({ id: item, interviewResult: 1 }))
      this.$api
        .post('/resumeInfo/changeInterviewResult', {
          reqdata: {
            resumeInfoStatusList
          }
        })
        .then((res) => {
          this.queryData(this.searchForm)
        })
    },
    throughAll () {
      this.pickList.length
        ? this.through(this.pickList, true)
        : this.$message({ type: 'info', message: '请选择通过的应聘者' })
    },
    eliminate (row, type = false) {
      let ids = type ? row : [row.id]
      let resumeInfoStatusList = ids.map(item => ({ id: item, interviewResult: 2 }))
      this.$api
        .post('/resumeInfo/changeInterviewResult', {
          reqdata: {
            resumeInfoStatusList
          }
        })
        .then((res) => {
          this.queryData(this.searchForm)
        })
    },
    eliminateAll () {
      this.pickList.length
        ? this.eliminate(this.pickList, true)
        : this.$message({ type: 'info', message: '请选择被淘汰的应聘者' })
    },
    del (row, type = false) {
      let ids = type ? row : [row.id]
      let deleteResumeInfoList = ids.map(item => ({ id: item, status: 1 }))
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$api
            .post('/resumeInfo/deleteResumeInfo', {
              reqdata: {
                deleteResumeInfoList
              }
            })
            .then((res) => {
              this.queryData(this.searchForm)
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    delAll () {
      this.pickList.length
        ? this.del(this.pickList, true)
        : this.$message({ type: 'info', message: '请选择需要删除的内容' })
    },
    selection (val) {
      this.$set(this.$data, 'pickList', val)
    },
    open () {
      this.dialogFormVisible = true
    },
    close () {
      this.dialogFormVisible = false
      this.form = {}
      this.copyPickList = Array.from(this.pickList)
    },
    closeDia () {
      this.dialogVisible = false
    },
    openDia () {
      this.dialogVisible = true
    },
    callPage (val) {
      this.page = val
      this.queryData(this.searchForm)
    }
  },
  watch: {
    isCollapse () {
      this.queryData(this.searchForm)
      this.isCollapse === 1
        ? this.$set(this.table, 'handle', [
          {
            title: '查看简历',
            method: 'details',
            type: 'info'
          },
          {
            title: '通知成功',
            method: 'notice',
            type: 'success'
          }
        ])
        : this.isCollapse === 0 && this.$set(this.table, 'handle',
          [
            {
              title: '查看简历',
              method: 'details',
              type: 'info'
            },
            {
              title: '标记通过',
              method: 'through',
              type: 'success'
            },
            {
              title: '标记淘汰',
              method: 'eliminate',
              type: 'danger'
            }
          ]
        )
    },
    pickList () {
      this.copyPickList = Array.from(this.pickList)
    }
  }
}
</script>
