<template>
  <div id="publish-present">
    <el-row>
      <el-col :span="24" align="center"><h2>驻场招聘信息表</h2></el-col>
    </el-row>
    <el-row>
      <el-col :span="19"><h3>基本信息</h3></el-col>
    </el-row>
    <el-row>
      <el-col :span="12">项目ID：<span>{{related_id}}</span></el-col>
      <el-col :span="12" >需求方：<span>{{uid}}</span></el-col>
    </el-row>
    <el-row>
      <el-col :span="24" >项目名称：<span>{{title}}</span></el-col>
    </el-row>
    <el-row>
      <el-col :span="24">项目类型：<span>{{related_type}}</span><span style="display: none">{{job_type}}</span></el-col>
    </el-row>
    <el-row>
      <el-col :span="12">客户经理：<span>{{manager}}</span></el-col>
      <el-col :span="12" >
        <div class="inlineb">
          <span class>开放状态:</span>
          <div class="inlineb">
            <el-select v-model="statusValue" >
              <el-option
                v-for="item of openStatus"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
        </div>
    </el-col>
    </el-row>
    <el-row>
      <el-col :span="24"><h3>驻场信息</h3></el-col>
    </el-row>
    <el-row>
      <el-col :span="24" >
        <div class="inlineb">
          <span class>薪酬范围:</span>
          <div class="inlineb">
            ￥<el-input style="width: 230px;"  v-model="salary_from" ></el-input>至
          </div>
          <div class="inlineb">
            ￥<el-input style="width: 230px;" v-model="salary_to" ></el-input>
          </div>
          <el-alert
            title=""
            class="top"
            id="salaryAlert"
            type="error"
            style="display: none"
            show-icon>
          </el-alert>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="inlineb">
          <span class>驻场地点:</span>
          <div class="inlineb">
            <el-select v-model="city" >
              <el-option
                v-for="item of citys"
                :key="item.city"
                :label="item.label"
                :value="item.city"
              ></el-option>
            </el-select>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" >
        <div class="inlineb">
          <span class>驻场周期:</span>
          <div class="inlineb">
            <el-input style="width: 230px;" v-model="month" ></el-input>个月
          </div>
          <el-alert
            title=""
            class="top"
            id="monthAlert"
            type="error"
            style="display: none"
            show-icon>
          </el-alert>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="inlineb">
          <span class>工作年限:</span>
          <div class="inlineb">
            <el-input style="width: 230px;" v-model="work_year" ></el-input>年
          </div>
          <el-alert
            title=""
            class="top"
            id="workYear"
            type="error"
            style="display: none"
            show-icon>
          </el-alert>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div class="inlineb">
          <span class>是否有文件:</span>
          <div class="inlineb">
            <el-select v-model="file" >
              <el-option
                v-for="item of files"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              ></el-option>
            </el-select>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">职位方向：<span>{{directions}}</span><span style="display: none">{{match_directions}}</span></el-col>
    </el-row>
    <el-row>
      <el-col :span="24">技能要求：<span>{{skill}}</span><span style="display: none">{{match_skills}}</span></el-col>
    </el-row>
    <el-row>
      <el-col :span="20">工作内容：
        <el-alert
          title=""
          class="top"
          id="jobInfoAlert"
          type="error"
          style="display: none"
          show-icon>
        </el-alert>
        <el-input
          type="textarea"
          :autosize="{ minRows: 5, maxRows: 4}"
          placeholder="请输入内容"
          v-model="job_description">
        </el-input>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-button type="primary" @click="savePresent">保存</el-button>
        <el-button type="info" @click="cancelPresent">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
    export default {
        data () {
            return {
                related_id:'',
                uid:'',
                title:'',
                related_type:'',
                job_type:'',
                manager:'',
                salary_from:'',
                salary_to:'',
                month:'',
                work_year:'',
                directions:'',
                match_skills:'',
                skill:'',
                job_description:'',
                match_directions:'',
                jump_url:'',
                citys: [{
                    city: '0',
                    label: '不限'
                }, {
                    city: '1',
                    label: '北京'
                }, {
                    city: '2',
                    label: '上海'
                }, {
                    city: '3',
                    label: '广州'
                }, {
                    city: '4',
                    label: '深圳'
                }, {
                    city: '5',
                    label: '成都'
                }, {
                    city: '6',
                    label: '杭州'
                }],
                city: '0',

                openStatus: [{
                    id: 0,
                    label: '下架隐藏'
                }, {
                    id: 1,
                    label: '开放中'
                }, {
                    id: 2,
                    label: '停止申请'
                }, {
                    id: 3,
                    label: '开发中'
                }, {
                    id: 4,
                    label: '已结束'
                }],
                statusValue: 0,

                files: [{
                    id: '1',
                    label: '是'
                }, {
                    id: '0',
                    label: '否'
                }],
                file: '0',
            }
        },
        mounted() {
            this.getPresentInfo();
        },
        methods: {
            async getPresentInfo() {

                let query = window.location.search.substring(1);
                let vars = query.split("&");
                let body = {
                    'job_id' : vars[0].split("=")[1],
                    'job_type' : vars[1].split("=")[1],
                };
                let url = window.location.href;
                if(url.indexOf('dev')!=-1){
                    this.jump_url = 'https://dev.test.proginn.com/rooter/cloudjobitem/';
                } else if(url.indexOf('local')!=-1){
                    this.jump_url = 'http://local.proginn.com/rooter/cloudjobitem/';
                } else {
                    this.jump_url = 'https://proginn.com/rooter/cloudjobitem/';
                }



                const res = await this.$post("/api/admin/present_job/present_info",body);
                this.related_id = res.data.job.id;
                this.uid = res.data.job.uid;
                this.title = res.data.job.title2;
                this.related_type = res.data.job_type;
                this.manager = res.data.manager;
                this.salary_from = res.data.job.match_salary_min;
                this.salary_to = res.data.job.match_salary_max;
                this.month = res.data.job.month?res.data.job.month:'';
                this.work_year = res.data.job.work_year?res.data.job.work_year:'';
                this.directions = res.data.job.directions;
                this.match_skills = res.data.job.match_skills;
                this.skill = res.data.job.skill;
                this.statusValue = res.data.job.status?res.data.job.status:0;
                this.city = res.data.job.city?res.data.job.city:'0';
                this.file = res.data.job.show_file?res.data.job.show_file:'0';
                this.job_type = vars[1].split("=")[1];
                this.job_description = res.data.job.description;
                this.match_directions = res.data.job.match_directions;

            },
            async cancelPresent() {
                window.history.back();
            },
            async savePresent() {
                let flag = false;
                let salaryAlert = document.getElementById('salaryAlert');
                let monthAlert = document.getElementById('monthAlert');
                let jobInfoAlert = document.getElementById('jobInfoAlert');
                let workYearAlert = document.getElementById('workYear');
                if (this.work_year.length==0){
                    workYearAlert.innerText = '请输入工作年限';
                    workYearAlert.style.display = 'block';
                    flag = true;
                }
                if (this.salary_from.length==0||this.salary_to.length==0){
                    salaryAlert.innerText = '请正确输入薪酬范围';
                    salaryAlert.style.display = 'block';
                    flag = true;
                }
                if (this.month.length==0){
                    monthAlert.innerText = '请输入驻场周期';
                    monthAlert.style.display = 'block';
                    flag = true;
                }
                if (this.job_description.length==0){
                    jobInfoAlert.innerText = '请输入工作内容';
                    jobInfoAlert.style.display = 'block';
                    flag = true;
                }
                if ((this.work_year<0||this.work_year>99)&&this.work_year.length!=0){
                    workYearAlert.innerText = '工作年限正确填写范围0-99';
                    workYearAlert.style.display = 'block';
                    flag = true;
                }
                this.salary_from = parseInt(this.salary_from);
                this.salary_to = parseInt(this.salary_to);
                if (this.salary_from<1||this.salary_from>1000000||this.salary_to<1||this.salary_to>1000000||this.salary_from>=this.salary_to){
                    salaryAlert.innerText = '薪酬填写范围1-1000000且前一个值要小于后一个值';
                    salaryAlert.style.display = 'block';
                    flag = true;
                }
                if ((this.month<1||this.month>99)&&this.month.length!=0){
                    monthAlert.innerText = '驻场周期正确填写范围1-99';
                    monthAlert.style.display = 'block';
                    flag = true;
                }
                if (this.job_description.length>10000){
                    jobInfoAlert.innerText = '工作内容填写最多10000个字符且不能为空';
                    jobInfoAlert.style.display = 'block';
                    flag = true;
                }
                if (flag){
                    return false;
                }
                let body = {
                    'related_id' : this.related_id,
                    'title' : this.title,
                    'related_type' : this.job_type,
                    'status' : this.statusValue,
                    'salary_from' : this.salary_from,
                    'salary_to' : this.salary_to,
                    'city' : this.city,
                    'show_file' : this.file,
                    'month' : this.month,
                    'work_year' : this.work_year,
                    'job_direction' : this.match_directions,
                    'job_skills' : this.match_skills,
                    'job_description' :this.job_description
                }
                const res = await this.$post("/api/admin/present_job/present_edit",body);
                if (res.status==1) {
                    alert(res.info);
                    location.href=this.jump_url+this.related_id;

                }
            }
        }
    }
</script>

<style lang="scss" scoped>
  .el-row {
    margin-bottom: 20px;
  }
  .top{
    margin-top: 10px;
  }

</style>
