<template>
  <basic-container>
    <!--搜索栏-->
    <el-form
      size="small"
      label-position="right"
      style="padding-left: 10px; padding-right: 10px"
      :inline="true"
    >
      <el-row :span="24">
        <el-form-item label="公司名称:">
          <el-input
            v-model="query.companyName"
            placeholder="公司名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="任务编码:">
          <el-input
            v-model="query.missionNo"
            placeholder="任务编码"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="任务标题:">
          <el-input
            v-model="query.missionTitle"
            placeholder="任务标题"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="query.type" placeholder="请选择搜索时间类型">
            <el-option label="任务开始日期" :value="0"></el-option>
            <el-option label="任务结束日期" :value="2"></el-option>
            <el-option label="报名截止日期" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="query.time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <tool-tip></tool-tip>
        <br>
        <el-form-item label="任务状态：">
          <el-select v-model="query.status">
            <el-option
              v-for="(item, index) in mStatusList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <div class="searchBtn">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="searchChange1"
            >搜 索</el-button
          >
          <el-button size="small" icon="el-icon-delete" @click="searchReset1"
            >清 空</el-button
          >
        </div>
      </el-row>
    </el-form>
    <!--搜索-->
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :page.sync="page"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          v-if="vaildData(permission.manage_mission_check, false)"
          size="small"
          @click.stop="handleCheck"
          type="primary"
          >任务审核</el-button
        >
      </template>
      <template slot="menuRight">
        <el-button size="small" @click.stop="downRecords" type="primary"
          >导出</el-button
        >
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button
          v-if="vaildData(permission.manage_mission_delay, false) && row.status==3"
          size="small"
          @click.stop="handleDelay(row)"
          type="text"
          >发工资延期</el-button
        >
        <el-button
          type="text"
          size="small"
          @click.stop="rowView(row)"
          v-if="vaildData(permission.manage_mission_view, false)"
          >详情</el-button
        >
        <el-button
          type="text"
          size="small"
          @click.stop="rowZPView(row)"
          v-if="vaildData(permission.manage_mission_zpview, false)"
          >招聘情况</el-button
        >
        <el-button
          v-if="row.status==2"
          size="small"
          @click.stop="handleFinish(row)"
          type="text"
          >完成任务</el-button
        >
      </template>
    </avue-crud>
    <delay-dialog
      ref="delay"
      :ids="ids"
      @success="onLoad(page, query)"
    ></delay-dialog>
    <el-drawer title="详情" :visible.sync="viewDrawer" size="60%">
      <mission-view :model="view"></mission-view>
    </el-drawer>
    <zp-view ref="zpView"></zp-view>
    <wage-view ref="wageView"></wage-view>
  </basic-container>
</template>

<script>
import {
  getList,
  review,
  complet,
  getDetail,
  download,
} from "@/api/manage/mission";
import { mapGetters } from "vuex";
import delayDialog from "./delayDialog";
import missionView from "@/views/util/mission-view";
import zpView from "./zpView";
import wageView from "./wageView";
import { missionState, wageUnitCategoryState } from "@/common/dic";
import { calcDate } from "@/util/date";
import { Message } from "element-ui";
import lodash from "lodash";

const message = lodash.throttle(
  function (options) {
    Message(options);
  },
  3000,
  { trailing: false }
);

export default {
  components: { delayDialog, missionView, zpView, wageView },
  name: "manage_mission",
  data() {
    return {
      view: {},
      form: {},
      selectionList: [],
      query: { type: '' },
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      checkDialog: false,
      finishDialog: false,
      viewDrawer: false,
      mStatusList: [
        { value: 1, label: "招聘中" },
        { value: 2, label: "任务中" },
        { value: 3, label: "已完成" },
        { value: 4, label: "已失效" },
        {
          label: "审核未通过",
          value: 9,
        },
      ],
      option: {
        height: "auto",
        tip: false,
        columnBtn: false,
        searchShow: true,
        searchMenuSpan: 5,
        menuWidth: 270,
        border: true,
        selection: true,
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        dialogClickModal: false,
        dialogType: "drawer",
        dialogFullscreen: true,
        mStatusList: missionState,
        column: [
          {
            label: "公司名称",
            prop: "companyName",
            display: false,
          },
          {
            label: "任务编码",
            prop: "missionNo",
            display: false,
          },
          {
            label: "任务标题",
            prop: "missionTitle",
            display: false,
          },
          {
            label: "招聘人数",
            prop: "peopleNum",
            search: false,
            searchSpan: 1,
            display: false,
            minWidth: 70,
          },
          {
            label: "申请人数",
            prop: "applyNum",
            search: false,
            searchSpan: 1,
            display: false,
            minWidth: 70,
          },
          {
            label: "确认人数",
            prop: "applyPassNum",
            search: false,
            searchSpan: 1,
            display: false,
            minWidth: 70,
          },
          {
            label: "任务时长",
            prop: "missionDuration",
            search: false,
            searchSpan: 5,
            display: false,
            minWidth: 70,
          },
          {
            label: "",
            prop: "time",
            type: "date",
            hide: true,
            valueFormat: "yyyy-MM-dd",
          },
          {
            label: "任务开始日期",
            prop: "stime",
            type: "date",
            display: false,
            minWidth: 100,
            format: "yyyy-MM-dd",
          },
          {
            label: "任务结束日期",
            prop: "etime",
            type: "date",
            display: false,
            minWidth: 100,
            format: "yyyy-MM-dd",
          },
          {
            label: "报名截止日期",
            prop: "etimePub",
            type: "date",
            search: false,
            searchSpan: 5,
            display: false,
            minWidth: 100,
            format: "yyyy-MM-dd",
          },
          {
            label: "工资",
            prop: "wage",
            search: false,
            searchSpan: 5,
            display: false,
            width: 80,
          },
          {
            label: "工资单位",
            prop: "wageUnitCategory",
            search: false,
            dicData: wageUnitCategoryState,
            searchSpan: 5,
            display: false,
          },
          {
            label: "任务状态",
            prop: "status",
            type: "select",
            dicData: missionState,
            display: false,
            span: 24,
            width: 80,
          },
          {
            label:"备注",
            prop:"remarks"
          }
        ],
      },
      data: [],
    };
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        viewBtn: this.vaildData(
          this.permission.manage_station_mainList_view,
          false
        ),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    },
  },
  methods: {
    initData() {
      this.onLoad(this.page, this.query);
    },
    rowView(row) {
      getDetail(row.missionNo).then((resp) => {
        this.viewDrawer = true;
        this.view = resp.data.data;
      });
    },
    rowZPView(row) {
      this.$refs.zpView.open(row.missionNo);
    },
    handleWageView(row) {
      this.$refs.wageView.open(row.missionNo);
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.time) {
        params.stime = params.time[0];
        params.etime = params.time[1];
        delete params.time;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchChange1() {
      if (this.query.time) {
        this.query.stime = this.query.time[0];
        this.query.etime = this.query.time[1];
       // delete this.query.time;
      }
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
    },
    searchReset1() {
      this.query = {};
      this.onLoad(this.page);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    beforeOpen(done, type) {
      if (["add", "edit"].includes(type)) {
        this.initData();
        done();
      }
      if (["edit", "view"].includes(type)) {
        this.loading = true;
      }
    },
    beforeClose(done) {
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      params = Object.assign(this.query, params);
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(this.query, params)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.data.map((val) => {
          val.missionDuration =
            calcDate(
              new Date(val.stime).getTime(),
              new Date(val.etime).getTime()
            ).days + "天";
          val.payStatus =
            (val.payPersonTotal && val.payPersonTotal) > 0
              ? "已支付"
              : "未支付";
        });
      });
    },
    handleDelay(row) {
      this.$refs.delay.open(row.missionNo);
    },
    handleCheck() {
      if (this.ids.length < 1) {
        message({
          type: "warning",
          message: "请至少选择一条数据",
        });
        return;
      }
      let idList = [];
      for (let i = 0; i < this.ids.length; i++) {
        this.data.forEach((ele) => {
          if (ele.status == 1 && ele.id == this.ids[i]) {
            idList.push(ele.id);
          }
        });
      }

      if (idList.length == 0) {
        this.$message.error("只能驳回【招聘中】的任务");
      } else {
        let h = this.$createElement;
        this.$prompt(
          h("p", { style: "color: #F56C6C" }, "一旦驳回无法进行后续操作"),
          "您确定要驳回此任务吗？",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            center: true,
            inputType: "textarea",
            inputPlaceholder: "备注驳回原因",
          }
        )
          .then(({ value }) => {
            this.loading = true;
            review(idList.join(","), value)
              .then(() => {
                this.loading = false;
                this.onLoad(this.page, this.params);
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
              })
              .catch(() => {
                this.loading = false;
              });
          })
          .catch(() => {});
      }
    },
    handleFinish(row) {
        if (row.status!=2) {
            this.$message.error('只能提前完成【任务中】的任务')
        }
        /*只能提前完成【任务中】的任务*/
        else{
          let h = this.$createElement;
        this.$confirm(
          h("p", { style: "color: #F56C6C" }, "一旦确定无法修改"),
          "您确定要提前此任务吗？",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
            center: true,
            inputType: "textarea",
            inputPlaceholder: "备注原因",
          }
        )
          .then(() => {
            this.loading = true;
            complet(row.id)
              .then(() => {
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.loading = false;
                this.onLoad(this.page, this.query);
              })
              .catch(() => {
                this.loading = false;
              });
          })
          .catch(() => { });
        }
    },
    downRecords() {
      if (this.query.time) {
        this.query.stime = this.query.time[0];
        this.query.etime = this.query.time[1];
       // delete this.query.time;
      }
      window.open(download(this.query));

    },

  },
};
</script>

<style>
  .el-form-item {
    margin-right: 18px !important;
  }
.searchBtn{
  display: inline-block;
}
</style>
