<!--
 * @Description: create
 * @Version: 1.0
 * @Autor: XuTongZhang
 * @Date: 2020-07-30 10:17:25
 * @LastEditors  : yuanrunwei
 * @LastEditTime : 2021-02-05 17:11:16
-->
<template>
<div class="indexPage">
    <v-input :btn="btn" :list="list" @search="search" @exports="exports" @upload="upload=true" @excel="handleExcel"></v-input>
    <v-table :table="table" :tableList="tableList" :sortType="true" :queryData="queryData" :form="searchForm" @details="details" @update="update" @duplicate="handleDuplicate" @delete="handleDelete"></v-table>
    <v-pager @page="callPage" :total="totalrecords"></v-pager>
    <!-- <resume :dialogFormVisible="dialogFormVisible" :close="close" :info="info" :searchForm="searchForm" :queryData="queryData"></resume> -->
    <el-dialog :visible.sync="dialogVisible" width="600px" :before-close="closeDia" :close-on-click-modal="false">
        <el-form :model="form" ref="uform" label-width="140px" :rules="rules" label-position="right">
            <el-form-item label="姓名" prop="delivererName">
                <el-input placeholder="请输入姓名" v-model="form.delivererName" maxlength="32" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="delivererSex">
                <el-select v-model="form.delivererSex" class="select" placeholder="请选择性别">
                    <el-option v-for="item in [{ value: 0, label: '女' }, { value: 1, label: '男' }]" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="投递职位" prop="positionApplied">
                <el-input placeholder="请输入投递职位" v-model="form.positionApplied" maxlength="32" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="工作年限" prop="delivererWorkExp">
                <el-input placeholder="请输入工作年限" v-model="form.delivererWorkExp" maxlength="200" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="学历" prop="delivererEducation">
                <el-select class="select" v-model="form.delivererEducation" placeholder="请选择学历">
                    <el-option v-for="item in ['大专', '本科', '研究生', '博士', '其他']" :key="item" :label="item" :value="item">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="联系电话" prop="delivererPhone">
                <el-input placeholder="请输入联系电话" v-model.number="form.delivererPhone" maxlength="11" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="邮箱地址" prop="delivererMail">
                <el-input placeholder="请输入邮箱地址" v-model="form.delivererMail" maxlength="32" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="应聘职位" prop="desiredPositionId">
                <el-select class="select" v-model="form.desiredPositionId" placeholder="请选择应聘职位">
                    <el-option v-for="item in positionList" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="closeDia">取 消</el-button>
                <el-button type="primary" @click="submit">修 改</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <!-- upload -->
    <el-dialog :visible.sync="upload" width="600px" :before-close="() => {upload=false}" :close-on-click-modal="false">
        <el-form label-width="140px" label-position="right">
            <el-form-item label="姓名">
                <el-input placeholder="请输入姓名" v-model="uploadForm.delivererName" maxlength="32" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="性别">
                <el-select v-model="uploadForm.delivererSex" class="select" placeholder="请选择性别">
                    <el-option v-for="item in [{ value: 0, label: '女' }, { value: 1, label: '男' }]" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="请选择出生日期">
                <el-date-picker
                  v-model="uploadForm.delivererBirthday"
                  value-format="yyyy/MM/dd"
                  type="date"
                  placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="手机号码">
                <el-input placeholder="请输入手机号码" v-model.number="uploadForm.delivererPhone" maxlength="11" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
                <el-input placeholder="请输入邮箱" v-model="uploadForm.delivererMail" maxlength="32" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="国籍">
                <el-input placeholder="请输入国籍" v-model="uploadForm.delivererNationality" maxlength="32" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="联系地址">
                <el-input placeholder="请输入联系地址" v-model="uploadForm.delivererAddress" maxlength="32" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="意向城市-省市">
                <!-- <el-input placeholder="请选择意向城市-省市" v-model="uploadForm.delivererIntention" maxlength="32" show-word-limit></el-input> -->
                <!-- <el-cascader placeholder="请选择意向城市-省市" v-model="uploadForm.delivererIntention" :props="{
                  lazy: true,
                  value: 'id',
                  label: 'name',
                  lazyLoad (node, resolve) {
                    const { level } = node;
                    $api.post('/addr/all_addr').then(result=>{
                      resolve(result.list.map(item => ({
                        value: item.id,
                        label: item.name,
                        leaf: level >= 2,
                      })))
                    })
                  }
                }"></el-cascader> -->
                <el-cascader :options="addrOptions" placeholder="请选择意向城市-省市" v-model="uploadForm.delivererIntention" :props="{
                  value: 'name',
                  label: 'name'
                }"></el-cascader>
            </el-form-item>
            <el-form-item label="学历">
                <el-select class="select" v-model="uploadForm.delivererEducation" placeholder="请选择学历">
                    <el-option v-for="item in ['大专', '本科', '研究生', '博士', '其他']" :key="item" :label="item" :value="item"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="年级">
                <el-input placeholder="请输入年级" v-model="uploadForm.delivererGrade" maxlength="200" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="语言能力">
                <el-cascader
                  v-model="uploadForm.delivererLanguageAbility"
                  :options="options"
                  :props="{
                    multiple:false,
                    emitPath:false
                  }"
                  :show-all-levels="false"
                  clearable></el-cascader>
            </el-form-item>
            <el-form-item label="期望薪资">
                <el-input placeholder="请输入期望薪资" v-model="uploadForm.delivererExpectSalary" maxlength="200" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="工作年限">
                <el-select v-model="uploadForm.delivererWorkExp" class="select" placeholder="请选择工作年限">
                    <el-option v-for="item in expOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button @click="upload = false">取 消</el-button>
                <el-button type="primary" @click="handleSubmit">提 交</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <el-dialog :visible.sync="duplicate" width="90%" :before-close="()=>{duplicate=false}" :close-on-click-modal="false">
        <v-table :table="table" :tableList="duplicateList" :sortType="true" @update="update" @delete="handleDelete" readonly></v-table>
    </el-dialog>
</div>
</template>

<script>
export default {
  data () {
    let validator = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'))
      } else {
        if (!(/^1[3456789]\d{9}$/.test(value))) {
          callback(new Error('请输入正确的手机格式'))
        }
        callback()
      }
    }
    return {
      tableList: [],
      dialogFormVisible: false,
      dialogVisible: false,
      duplicate: false,
      upload: false,
      page: 1,
      totalrecords: 0,
      pickList: [],
      duplicateList: [],
      downList1: [],
      searchForm: {},
      form: {},
      uploadForm: {
        delivererName: '',
        delivererSex: 0,
        delivererBirthday: '',
        delivererPhone: '',
        delivererMail: '',
        delivererNationality: '',
        delivererAddress: '',
        delivererIntention: '',
        delivererEducation: '',
        delivererGrade: '',
        delivererLanguageAbility: '',
        delivererExpectSalary: '',
        delivererWorkExp: ''
      },
      options: [],
      addrOptions: [],
      expOptions: [
        {
          value: '1年以内工作经验',
          label: '1年以内工作经验'
        },
        {
          value: '1-3年工作经验',
          label: '1-3年工作经验'
        },
        {
          value: '3-5年工作经验',
          label: '3-5年工作经验'
        },
        {
          value: '5-10年工作经验',
          label: '5-10年工作经验'
        },
        {
          value: '10年以上工作经验',
          label: '10年以上工作经验'
        }
      ],
      rules: {
        delivererEducation: [{
          required: true,
          message: '请选择学历',
          trigger: 'change'
        }],
        delivererName: [{
          required: true,
          message: '请输入姓名',
          trigger: 'change'
        }],
        delivererPhone: [{
          required: true,
          validator,
          trigger: 'change'
        }],
        delivererMail: [{
          required: true,
          message: '请输入邮箱',
          trigger: 'change'
        }],
        delivererSex: [{
          required: true,
          message: '请选择性别',
          trigger: 'change'
        }],
        delivererWorkExp: [{
          required: true,
          message: '请填写工作经验',
          trigger: 'change'
        }],
        // desiredPositionId: [{
        //     required: true,
        //     message: '请选择应聘职位',
        //     trigger: 'change'
        // }],
        positionApplied: [{
          required: true,
          message: '请填写投递职位',
          trigger: 'change'
        }]
      },
      positionList: [],
      info: {},
      list: [{
        placeholder: '请输入关键字查询',
        props: 'condition'
      },
      {
        type: 'select',
        placeholder: '性别',
        props: 'delivererSex',
        options: [{
          label: '全部',
          value: null
        }, {
          label: '男',
          value: 1
        }, {
          label: '女',
          value: 0
        }]
      },
      {
        type: 'select',
        placeholder: '来源',
        props: 'resumeFrom',
        options: [{
          label: '全部',
          value: null
        },
        {
          label: '51job',
          value: 0
        },
        {
          label: '智联',
          value: 1
        },
        {
          label: '58同城',
          value: 2
        },
        {
          label: '平台',
          value: 3
        },
        {
          label: 'boss直聘',
          value: 4
        }
        ]
      }
      ],
      btn: [{
        name: '确定',
        type: 'primary',
        method: 'search'
      },
      {
        name: '导出',
        type: 'primary',
        method: 'exports'
      },
      {
        name: '上传简历',
        type: 'primary',
        method: 'upload'
      },
      {
        name: '导入简历',
        type: 'upload',
        method: 'excel'
      }
      ],
      table: {
        selection: true,
        column: [{
          label: this.$t('field.serialNumber'),
          props: 'serialNumber'
        },
        {
          label: '姓名',
          props: 'delivererName',
          width: 60
        },
        {
          label: '性别',
          props: 'delivererSex',
          options: ['女', '男'],
          width: 60
        },
        {
          label: '简历来源',
          props: 'resumeFrom',
          options: ['51job', '智联招聘', '58同城', '平台', 'boss直聘'],
          width: 80
        },
        {
          label: '招聘类型',
          props: 'recruitType',
          options: ['社招', '校招', '直接校招'],
          width: 80
        },
        {
          label: '投递职位',
          props: 'positionApplied'
        },
        {
          label: '简历状态',
          props: 'state'
        },
        {
          label: '工作年限',
          props: 'delivererWorkExp'
        },
        {
          label: '学历',
          props: 'delivererEducation'
        },
        {
          label: '联系电话',
          props: 'delivererPhone'
        },
        {
          label: '邮箱',
          props: 'delivererMail'
        },
        {
          label: '应聘职位',
          props: 'positionName'
        },
        {
          label: '通知时间',
          props: 'sendMailTime'
        },
        {
          label: '完成时间',
          props: 'confirmInterviewTime'
        },
        {
          label: '面试状态',
          props: 'interviewStatus',
          options: ['未面试', '已面试']
        },
        {
          label: '面试结果',
          props: 'interviewResult',
          options: ['未读', '已通过', '已淘汰']
        }
        ],
        // fixed: true,
        width: 200,
        handle: [
          {
            title: '修改',
            method: 'update',
            type: 'warning'
          },
          {
            title: '去重',
            method: 'duplicate',
            type: 'primary',
            readonly: true
          },
          {
            title: '删除',
            method: 'delete',
            type: 'danger'
          }
        // {
        //   title: '查看简历',
        //   method: 'details',
        //   type: 'info'
        // }
        ]
      }
    }
  },
  mounted () {
    this.queryData()
    this.queryDownList()
    this.handleOptions()
  },
  methods: {
    queryData (form = {}) {
      let page = this.page
      this.searchForm = form
      let reqdata = form
      this.$api
        .post('/resumeInfo/queryTalentPoolList', {
          reqdata,
          page
        })
        .then((res) => {
          this.totalrecords = res.totalrecords
          this.tableList = res.list.map((item, index) => {
            item.delivererSex = item.delivererSex - 0 || 0
            item.desiredPositionId = item.desiredPositionId - 0 || null
            item.serialNumber = (this.page - 1) * 10 + index + 1
            // item.desiredPositionId = item.desiredPositionId ? this.downList1.some(i => i.id === item.desiredPositionId) ? this.downList1.find(i => i.id === item.desiredPositionId).positionName : '' : ''
            item.state = item.interviewResult ? ['', '已通过', '未通过'][item.interviewResult] : item.isSendInviteMail ? '已通知' : ['未读', '通过', '淘汰', '待定'][item.isPass]
            return item
          })
        })
    },
    queryDownList () {
      this.$api
        .post('/position/queryPositionList', {
          reqdata: {}
        })
        .then((res) => {
          this.positionList = res.list.map((item) => ({
            value: item.id,
            label: item.positionName
          }))
          this.downList1 = res.list
        })
    },
    search (form) {
      this.queryData(form)
    },
    update (row) {
      this.openDia()
      this.form = Object.assign({}, row)
    },
    submit () {
      let a
      this.$refs['uform'].validate((valid) => {
        a = valid
      })
      if (!a) return
      let reqdata = this.form
      this.$api
        .post('/resumeInfo/updateResumeInfo', {
          reqdata
        })
        .then((res) => {
          this.$message({
            message: '修改成功！',
            type: 'success'
          })
          this.closeDia()
          this.queryData()
          if (this.duplicate) {
            this.handleDuplicateRefresh(reqdata)
          }
        })
    },
    details (row) {
      this.open()
      this.info = row
    },
    exports () {
      this.$api
        .post('/export/export2Web', {
          reqdata: this.searchForm
        })
        .then((res) => {
        })
    },
    openDia () {
      this.dialogVisible = true
    },
    closeDia () {
      this.dialogVisible = false
      this.form = {}
    },
    open () {
      this.dialogFormVisible = true
    },
    close () {
      this.dialogFormVisible = false
      this.form = {}
    },
    callPage (val) {
      this.page = val
      this.queryData(this.searchForm)
    },
    handleOptions () {
      this.options = ['不限', '英语', '日语', '俄语', '阿拉伯语', '法语', '德语', '西班牙语', '葡萄牙语', '意大利语', '韩语/朝鲜语', '普通话', '粤语', '闽南话', '上海话', '其他'].map(item => {
        return {
          label: item,
          value: item,
          children: [item + '-一般', item + '-良好', item + '-熟练', item + '-精通'].map(item => {
            return {
              label: item,
              value: item
            }
          })
        }
      })
      this.$api.post('/addr/all_addr').then(result => {
        this.addrOptions = result.list.map(item => (
          {
            name: item.name,
            children: item.children.map(item => ({
              name: item.name
            }))
          }
        ))
      })
    },
    handleSubmit () {
      const reg = /^\w+@[a-z0-9]+\.[a-z]{2,4}$/
      if (!reg.test(this.uploadForm.delivererMail)) {
        this.$message.error('请输入正确的邮箱')
        return false
      }
      this.$api.post('/resumeInfo/uploadResume', {
        reqdata: {
          ...this.uploadForm,
          delivererIntention: `${this.uploadForm.delivererIntention[0]} ${this.uploadForm.delivererIntention[1]}`
        }
      }).then(result => {
        this.upload = false
        this.uploadForm = {
          delivererName: '',
          delivererSex: 0,
          delivererBirthday: '',
          delivererPhone: '',
          delivererMail: '',
          delivererNationality: '',
          delivererAddress: '',
          delivererIntention: '',
          delivererEducation: '',
          delivererGrade: '',
          delivererLanguageAbility: '',
          delivererExpectSalary: '',
          delivererWorkExp: ''
        }
        this.queryData()
      })
    },
    handleExcel () {
      this.$api.post('/admin/resumeInfo/batchUploadResume').then(result => {
        this.$message.success('导入成功')
        this.queryData()
      })
    },
    handleDuplicate (param) {
      this.$api.post('/resumeInfo/duplicateResume', {
        reqdata: {
          ...param
        }
      }).then(result => {
        if (result.list && result.list.length) {
          this.$confirm(`检索到“${param.delivererName}”的疑似重复记录${result.list.length}条，是否查看？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.duplicateList = result.list.map((item, index) => ({
              serialNumber: index + 1,
              state: item.interviewResult ? ['', '已通过', '未通过'][item.interviewResult] : item.isSendInviteMail ? '已通知' : ['未读', '通过', '淘汰', '待定'][item.isPass],
              ...item
            }))
            this.duplicate = true
          })
        }
      })
    },
    handleDuplicateRefresh (param) {
      this.$api.post('/resumeInfo/duplicateResume', {
        reqdata: {
          ...param
        }
      }).then(result => {
        this.duplicateList = result.list.map((item, index) => ({
          serialNumber: index + 1,
          state: item.interviewResult ? ['', '已通过', '未通过'][item.interviewResult] : item.isSendInviteMail ? '已通知' : ['未读', '通过', '淘汰', '待定'][item.isPass],
          ...item
        }))
      })
    },
    handleDelete (param) {
      this.$confirm(`确定删除该条简历吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.post('/resumeInfo/deleteResumeInfo', {
          reqdata: {
            resumeInfoStatusList: [{
              ...param,
              status: 1
            }]
          }
        }).then(result => {
          this.$message.success('删除成功')
          this.queryData()
          if (this.duplicate) {
            this.handleDuplicateRefresh(param)
          }
        })
      })
    }
  }
}
</script>
