<template>
  <div>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="obj"
      @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="signUpCount" slot-scope="{ row }">
        <div style="color: #409eff">
          <b>{{ row.signUpCount }}人</b>
        </div>
      </template>
      <template slot="takeOnCount" slot-scope="{ row }">
        <div style="color: #409eff">
          <b>{{ row.takeOnCount }}人</b>
        </div>
      </template>
      <template slot="missionDate" slot-scope="{ row }">
        <div>{{ format(row.stime) }}至{{ format(row.etime) }}</div>
      </template>

      <!--/自定义列-->
      <!--自定义操作栏-->
      <div slot="menu" slot-scope="{size,type,row}">
        <!-- <el-button
          icon="el-icon-view"
          :size="size"
          :type="type"
          @click="rowView(row);"
          v-show="vaildData(permission.tenant_mission_view,false)"
        >查看</el-button>-->
        <el-button
          icon="el-icon-check"
          :size="size"
          :type="type"
          @click="rowCopy(row, 'copy')"
          v-show="vaildData(permission.tenant_mission_add, false)"
        >复制
        </el-button>
        <el-button
          icon="el-icon-delete"
          :size="size"
          :type="type"
          @click="$refs.crud.rowDel(row)"
          v-show="vaildData(permission.tenant_mission_delete, false)"
        >删除
        </el-button>
      </div>
      <!--/自定义操作栏-->
      <!--自定义按钮-->
      <template slot="menuLeft">
        <el-button
          type="warning"
          plain
          icon="el-icon-plus"
          size="small"
          @click="rowCopy('', 'add')"
          v-show="vaildData(permission.tenant_mission_add, false)"
        >发布岗位
        </el-button>
      </template>
      <!--/自定义按钮-->
    </avue-crud>
    <copy-mission ref="copy" @back="backIndex" v-show="false"></copy-mission>
    <!--查看dialog-->
    <mission-view :visible.sync="viewDrawer" :model="view"></mission-view>
    <!--/查看dialog-->
  </div>
</template>

<script>
import {getExpiredList, remove, detail} from "@/api/tenant/postzp";
import CopyMission from "../Dialog/CopyMission";
import SkillSelect from "../Dialog/Skill";
import missionView from "./missionView.vue";
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: {},
      loading: false,
      isCanEdit: false,
      selectedSkills: [],
      tradeData: [],
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
      },
      query: {},
      data: [],
      obj: {},
      wageUnitCategory: [
        {
          value: 0,
          label: "元/人·次",
        },
        {
          value: 1,
          label: "元/人·时",
        },
        {
          value: 2,
          label: "元/人·天",
        },
        {
          value: 3,
          label: "元/人·周",
        },
        {
          value: 4,
          label: "元/人·月",
        },
        {
          value: 5,
          label: "元/人·个",
        },
        {
          value: 6,
          label: "元/人·件",
        },
      ],
    };
  },
  components: {
    CopyMission,
    SkillSelect,
    missionView,
    saveButton,
  },
  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%",
        dialogClickModal: false,
        column: [
          {
            label: "岗位名称",
            prop: "jobName",
            slot: true,
            display: false,
          },
          {
            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: "missionDate",
          //   slot: true,
          //   display: false,
          //   width: 200,
          // },
          {
            label: "申请人数",
            prop: "signUpCount",
            slot: true,
            display: false,
          },
          {
            label: "录用人数",
            prop: "takeOnCount",
            slot: true,
            display: false,
          },
          {
            label: "备注",
            prop: "remarks",
            display: false,
          },
        ],
      };
    },
  },
  methods: {
    format(date) {
      if (date) {
        return dateFormat(new Date(date), "yyyy/MM/dd");
      }
    },
    /*查看*/
    rowView(row) {
      detail(row.missionNo).then((res) => {
        this.viewDrawer = true;
        this.view = res;
      });
    },
    /*打开新建 复制*/
    rowCopy(row, type) {
      this.$refs.copy.onLoad(row, type);
      this.$nextTick(() => {
        this.$refs.copy.resetFields();//等弹窗里的form表单的dom渲染完在执行this.$refs.staffForm.resetFields()，去除验证
      })
    },
    /*删除岗位*/
    rowDel(row) {
      const h = this.$createElement;
      this.$confirm(
        h("div", null, [
          h("p", {style: "font-size: 16px"}, "您确定要删除此岗位吗? "),
          h("p", {style: "color: red"}, "一旦删除则无法找回"),
        ]),
        {
          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(() => {
          remove(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;
      getExpiredList(
        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>
