<template>
  <div class="app-container" style="padding: 0px !important;">
    <el-radio-group v-model="selectedTab" class="header-radio" @input="inputFn">
      <el-radio-button label="1">任务列表</el-radio-button>
      <el-radio-button label="2" v-if="isShowFlag">完成情况统计</el-radio-button>
    </el-radio-group>
    <el-form
      :model="searchArray"
      ref="searchArrayRef"
      size="small"
      :inline="true"
      style="margin-top:20px;"
    >
      <el-form-item label="频率" prop="frequencyType">
        <el-select v-model="searchArray.frequencyType" placeholder="请选择频率" style="width: 100%">
          <el-option
            v-for="item in frequencyTypeList"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="岗位" prop="postId">
        <el-select v-model="searchArray.postId" placeholder="请选择岗位" style="width: 100%" clearable>
          <el-option
            v-for="item in postIdList"
            :key="item.postId"
            :value="item.postId"
            :label="item.postName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status" v-if="selectedTab==='1'">
        <el-select v-model="searchArray.status" placeholder="请选择状态" style="width: 100%" clearable>
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期" prop="validTime">
        <el-date-picker
          v-model="searchArray.validTime"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
          :clearable="false"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="重点工作类别" prop="workType" v-if="selectedTab==='1'">
        <el-select
          v-model="searchArray.workType"
          placeholder="请选择重点工作类别"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="item in workTypeList"
            :key="item.dictLabel"
            :label="item.dictLabel"
            :value="item.dictLabel"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="searchFn">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetFn">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableData" :height="dyHeight" :key="tableKey">
      <template v-if="selectedTab == '1'">
        <el-table-column label="序号" prop="xh" width="80">
          <template slot-scope="scope">
            <span>{{ scope.$index+1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="重点工作类别" prop="workType" min-width="100" show-overflow-tooltip />
        <el-table-column label="清单详情" prop="workDetail" min-width="140" show-overflow-tooltip />
        <el-table-column label="频率" prop="frequencyType" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.frequencyType | formatFilter('frequencyType',frequencyTypeList) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="岗位名称" prop="postName" min-width="100" show-overflow-tooltip />
        <el-table-column label="是否逾期" prop="isOverdue" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.isOverdue == '0'">否</span>
            <span v-if="scope.row.isOverdue != '0'">是</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.status | formatFilter('status',statusList) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" min-width="150" show-overflow-tooltip />
        <el-table-column label="柜员姓名" prop="nickName" min-width="140" show-overflow-tooltip />
        <el-table-column label="开始时间" prop="startTime" min-width="140" show-overflow-tooltip />
        <el-table-column label="结束时间" prop="endTime" min-width="140" show-overflow-tooltip />
        <!-- <el-table-column
          label="最后一次查看时间"
          prop="lastViewTime"
          min-width="140"
          show-overflow-tooltip
        />-->
      </template>
      <template v-if="selectedTab == '2'">
        <el-table-column label="序号" prop="xh" width="100">
          <template slot-scope="scope">
            <span>{{ scope.$index+1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="岗位名称" prop="postName" min-width="100" show-overflow-tooltip />
        <el-table-column label="柜员姓名" prop="nickName" min-width="140" show-overflow-tooltip />
        <el-table-column label="频率" prop="frequencyType" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.frequencyType | formatFilter('frequencyType',frequencyTypeList) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="总数" prop="totalCount" min-width="140" show-overflow-tooltip />
        <el-table-column label="完成数" prop="completeCount" min-width="140" show-overflow-tooltip />
        <el-table-column label="完成率" prop="completeRate" min-width="140" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.completeRate }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="排名" prop="rankNum" min-width="140" show-overflow-tooltip />
      </template>
    </el-table>
    <div class="pagination_end">
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        @current-change="currentChangeFn"
        :total="total"
        @size-change="sizeChangeFn"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import {
  workRecordListAll,
  workRecordCompleteStatus,
  systemPostOptionselect,
  systemDictDataType
} from "@/api/system/home";
import _ from "lodash";
import dayjs from "dayjs";
import { mapGetters } from "vuex";
export default {
  name: "workmngdaytaskindex",
  computed: {
    ...mapGetters(["roles"]),
    isShowFlag() {
      let arr = _.intersection(this.roles, [
        "admin",
        "headAdmin",
        "branchAdmin",
        "headPublic",
        "headPrivate",
        "outletAdmin",
        "headOps"
      ]);
      if (arr.length > 0) {
        return true;
      } else {
        return false;
      }
    }
  },
  data() {
    return {
      selectedTab: "1",
      frequencyTypeList: [
        {
          label: "每日",
          value: "0"
        },
        {
          label: "每周",
          value: "1"
        },
        {
          label: "每月",
          value: "2"
        },
        {
          label: "每季",
          value: "3"
        },
        {
          label: "每年",
          value: "4"
        }
      ],
      statusList: [
        {
          label: "未完成",
          value: "0"
        },
        {
          label: "部分完成",
          value: "1"
        },
        {
          label: "已完成",
          value: "2"
        }
      ],
      postIdList: [],
      workTypeList: [],

      searchArray: {
        frequencyType: "",
        postId: "",
        status: "",
        validTime: "",
        workType: ""
      },
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      total: 0,
      loading: false,
      dyHeight: {},
      tableKey: false
    };
  },
  created() {
    window.addEventListener("resize", () => {
      this.dyHeight = Number(window.innerHeight) - 330;
    });
    this.dyHeight = Number(window.innerHeight) - 330;
    this.systemPostOptionselectFn();
    this.systemDictDataTypeFn();
    this.resetFn();
  },
  filters: {
    formatFilter(v, type, list) {
      if (type == "frequencyType") {
        let arr = _.filter(list, { value: v });
        return arr[0].label;
      } else if (type == "status") {
        let arr = _.filter(list, { value: v });
        return arr[0].label;
      }
    }
  },
  methods: {
    inputFn() {
      this.resetFn();
    },
    systemPostOptionselectFn() {
      systemPostOptionselect({}).then(response => {
        if (response.code == "200") {
          this.postIdList = response.data;
        } else {
          this.$message.error(response.msg || "操作失败");
        }
      });
    },
    systemDictDataTypeFn() {
      systemDictDataType("work_type").then(response => {
        if (response.code == "200") {
          this.workTypeList = response.data;
        } else {
          this.$message.error(response.msg || "操作失败");
        }
      });
    },
    resetFn() {
      if(this.selectedTab === '1') {
        this.searchArray = {
          frequencyType: "",
          postId: "",
          status: "",
          validTime: "",
          workType: ""
        };
      } else {
        this.searchArray = {
          frequencyType: "0",
          postId: "",
          status: "",
          validTime: "",
          workType: ""
        };
      }
      
      this.searchFn();
    },
    searchFn() {
      this.pageNum = 1;
      this.pageSize = 10;
      this.total = 0;
      this.queryListFn();
    },
    queryListFn() {
      this.loading = true;
      let p = {
        ...this.searchArray,
        validTime: `${this.searchArray.validTime} 01:00`,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      let api =
        this.selectedTab == "1" ? workRecordListAll : workRecordCompleteStatus;
      api(p).then(response => {
        this.loading = false;
        if (response.code == "200") {
          this.tableData = response.rows || [];
          this.total = response.total || 0;
          if (this.selectedTab == "1" && this.tableData.length > 0) {
            _.forEach(this.tableData, function(item, index) {
              item.isEnabledShow = item.isEnabled == "0" ? true : false;
            });
          }
        } else {
          this.$message.error(response.msg || "操作失败");
        }
        this.tableKey = !this.tableKey;
      });
    },
    currentChangeFn(val) {
      this.pageNum = val;
      this.queryListFn();
    },
    sizeChangeFn(val) {
      this.pageSize = val;
      this.queryListFn();
    }
  }
};
</script>

<style lang="scss" scoped>
.pagination_end {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
}
.header-radio {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #ebebeb;

  .el-radio-button {
    flex: 1;

    ::v-deep .el-radio-button__inner {
      width: 100%;
      border: none;
      font-weight: 400;
      letter-spacing: 0.44px;
      line-height: 25px;
      font-size: 16px;
      color: #666666;
      padding: 11px 0 12px 0;
    }

    ::v-deep .el-radio-button__orig-radio:checked + .el-radio-button__inner {
      background-color: #4886f8;
      font-weight: 400;
      color: #ffffff;
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }

    &:nth-child(2) {
      &::before,
      &::after {
        content: "";
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        height: 21px;
        width: 1px;
        background: #ebebeb;
        z-index: 1;
      }

      &::after {
        right: 1px;
      }
    }

    &.is-active {
      &::before,
      &::after {
        content: none;
      }
    }
  }
}
</style>