<template>
  <div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="obj"
      ref="crud"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      :before-open="beforeOpen"
      :page.sync="page"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <!--自定义列-->
      <template slot="missionTitle" slot-scope="{ row }">
        <el-tooltip effect="dark" :content="row.missionTitle" placement="top">
          <div style="color: black">
            <b>{{ row.missionTitle | ellipsis }}</b>
          </div>
        </el-tooltip>
      </template>
      <!-- 开始日期 -->
      <template slot="stime" slot-scope="{row}">
        <span>{{ format(row.stime) }}</span>
      </template>
      <!-- 完成日期 -->
      <template slot="etime" slot-scope="{row}">
        <span>{{ format(row.etime) }}</span>
      </template>
      <template slot="takeOnCount" slot-scope="{ row }">
        <div style="color: #409eff; cursor: pointer;" @click="employmentNum(row)">
          <b>{{ row.takeOnCount }}人</b>
        </div>
      </template>
      <template slot="toEnd" slot-scope="{ row }">
        <div>
          <i class="el-icon-warning-outline"></i> 距任务结束剩余
          <span style="color: #f56c6c">
            <b>{{ row.toEnd }}天</b>
          </span>
        </div>
      </template>
      <template slot="missionDate" slot-scope="{ row }">
        <div>{{ format(row.stime) }}至{{ format(row.etime) }}</div>
      </template>

      <!--/自定义列-->
      <!--自定义按钮-->
      <template slot="menuLeft">
        <save-button
          type="warning"
          plain
          icon="el-icon-plus"
          size="small"
          @click="rowCopy('', 'add')"
          v-show="vaildData(permission.tenant_mission_add, false)"
        >发布任务
        </save-button
        >
      </template>
      <!--/自定义按钮-->
      <!--自定义操作栏-->
      <div slot="menu" slot-scope="{ size, type, row }">
        <save-button
          :size="size"
          :type="type"
          @click="rowComplete(row)"
          v-show="vaildData(permission.tenant_mission_finish, false)"
        >完成任务
        </save-button
        >
        <el-button
          icon="el-icon-view"
          :size="size"
          :type="type"
          @click="rowView(row)"
          v-show="vaildData(permission.tenant_mission_view, false)"
        >查看
        </el-button
        >
        <save-button
          icon="el-icon-check"
          :size="size"
          :type="type"
          @click="rowCopy(row, 'copy')"
          v-show="vaildData(permission.tenant_mission_add, false)"
        >复制
        </save-button
        >
      </div>
      <!--/自定义操作栏-->
    </avue-crud>
    <!--复制dialog-->
    <copy-mission ref="copy" @back="backIndex" v-show="false"></copy-mission>
    <!--复制dialog-->
    <!--录用人员dialog-->
    <Employ ref="employ" :status="status" v-show="false"></Employ>
    <!-- 录用人数 -->
    <employment-num
      ref="employmentNum"
      :status="1"
      v-show="false"
    ></employment-num>
    <!--录用人员dialog-->
    <!--查看dialog-->
    <mission-view :visible.sync="viewDrawer" :model="view"></mission-view>
    <!--/查看dialog-->
  </div>
</template>

<script>
import {getList, complete, detail} from "@/api/tenant/mission";
import CopyMission from "../Dialog/CopyMission";
import SkillSelect from "../Dialog/Skill";
import Employ from "../Dialog/Employ";
import missionView from "./missionView.vue";
import employmentNum from "../Dialog/EmployNumber";
import {mapGetters} from "vuex";
import {dateFormat} from "@/util/date";
import saveButton from "./saveButton";

export default {
  comments: {},
  filters: {
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 15) {
        return value.slice(0, 14) + "...";
      }
      return value;
    },
  },
  data() {
    return {
      viewDrawer: false,
      view: {},
      status: 2,
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
      },
      query: {
        status: 2,
      },
      data: [],
      obj: {},
    };
  },
  components: {
    CopyMission,
    SkillSelect,
    Employ,
    missionView,
    saveButton,
    employmentNum
  },
  created() {
  },
  computed: {
    ...mapGetters(["permission"]),
    option() {
      return {
        height: "auto",
        calcHeight: 40,
        align: "center",
        menuAlign: "center",
        tip: false,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        excelBtn: false,
        columnBtn: false,
        searchBtn: false,
        searchShow: false,
        labelWidth: 100,
        border: true,
        index: false,
        selection: false,
        dialogType: "drawer",
        dialogWidth: "60%",
        delBtnText: "取消",
        dialogClickModal: false,
        // column: [
        //   {
        //     label: "岗位名称",
        //     prop: "missionTitle",
        //     display: false,
        //     slot: true,
        //   },
        //   {
        //     label: "任务编码",
        //     prop: "missionNo",
        //     display: false,
        //   },
        //   // {
        //   //   label: "任务时间",
        //   //   prop: "missionDate",
        //   //   slot: true,
        //   //   display: false,
        //   //   width: 200,
        //   // },
        //   {
        //     label: "开始时间",
        //     prop: "stime",
        //     display: false,
        //     width: 130,
        //     slot: true,
        //   },
        //   {
        //     label: "完成时间",
        //     prop: "etime",
        //     display: false,
        //     width: 130,
        //     slot: true,
        //   },
        //   {
        //     label: "录用人数",
        //     prop: "takeOnCount",
        //     slot: true,
        //     display: false,
        //   },
        //   {
        //     label: "提示",
        //     prop: "toEnd",
        //     slot: true,
        //     display: false,
        //   },
        // ],
        column: [
          {
            label: "岗位名称",
            prop: "missionTitle",
            display: false,
            slot: true,
          },
          {
            label: "任务编码",
            prop: "missionNo",
            display: false,
          },
          {
            label: "开始日期",
            prop: "stime",
            display: false,
            width: 130,
            slot: true,
          },
          {
            label: "完成日期",
            prop: "etime",
            display: false,
            width: 130,
            slot: true,
          },
          {
            label: "招聘人数",
            prop: "peopleNum",
            slot: true,
            display: false,
            width: 110,
          },
          {
            label: "申请人数",
            prop: "signUpCount",
            slot: true,
            display: false,
            width: 110,
          },
          {
            label: "录用人数",
            prop: "takeOnCount",
            slot: true,
            display: false,
            width: 110,
          },

          // {
          //   label: "提示",
          //   prop: "toStart",
          //   slot: true,
          //   display: false,
          //   width: 200,
          // },
        ],
      };
    },
  },
  watch: {},
  methods: {
    format(date) {
      if (date) {
        return dateFormat(new Date(date), "yyyy/MM/dd");
      }
    },
    // 录用人数
    employmentNum(row) {
      this.$refs.employmentNum.openDialog(row, 2);//查看已录用人员
    },
    /*查看*/
    rowView(row) {
      detail(row.missionNo).then((res) => {
        this.viewDrawer = true;
        this.view = res;
      });
    },
    /*打开复制*/
    rowCopy(row, type) {
      this.$refs.copy.onLoad(row, type);
    },
    /*打开录用人员*/
    employ(row, state) {
      this.$refs.employ.openDialog(row, state);
    },
    /*完成任务*/
    rowComplete(row) {
      const h = this.$createElement;
      this.$confirm(
        h("div", null, [
          h("p", {style: "font-size: 16px"}, "您确定此任务已经完成吗? "),
        ]),
        {
          type: "warning",
          showClose: false,
          showCancelButton: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          beforeClose: (action, instance, done) => {
            if (action === "confirm") {
              instance.confirmButtonLoading = true;
              instance.cancelButtonLoading = true;
              instance.closeOnPressEscape = false;
              instance.closeOnClickModal = false;
              setTimeout(() => {
                done();
                setTimeout(() => {
                  instance.confirmButtonLoading = false;
                  instance.cancelButtonLoading = false;
                }, 300);
              }, 1000);
            } else {
              done();
            }
          },
        }
      )
        .then(() => {
          complete(row.id).then(
            () => {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.onLoad(this.page, this.query);
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消'
          // });
        });
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    /*刷新本页 带搜索参数*/
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    /*加载数据*/
    onLoad(page, 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;
      });
    },
    /*返回首页*/
    backIndex() {
      this.$emit("refresh");
    },
  },
};
</script>
<style scoped>
.el-col,
.el-form-item {
  margin-bottom: 0px;
}
</style>
