<template>
  <div class="main_box">
    <navbar title="日常任务列表"></navbar>
    <div class="wrapper">
      <div class="banner-box">
        <van-tabs
          @change="tabChange()"
          v-model="selectTab"
          class="content"
          type="line"
          color="#4682ff"
          title-active-color="#4682ff"
          line-width="80"
        >
          <van-tab title="任务列表" name="1"></van-tab>
          <van-tab title="完成情况统计" name="2" v-if="isShowFlag"></van-tab>
        </van-tabs>
        <div class="search-input special-input-box">
          <van-search
            type="text"
            placeholder="请选择频率"
            v-model="frequencyType"
            left-icon
            class="special-search"
            readonly
            @click="showFrequencyTypePicker = true"
            right-icon
          >
            <template #right-icon>
              <van-icon name="arrow-down" size="16" v-if="!searchForm.frequencyType" />
              <!-- <van-icon name="clear" size="16" v-else @click.stop="clearOption('frequencyType')" /> -->
            </template>
          </van-search>
          <van-search
            type="text"
            placeholder="请选择岗位"
            v-model="postId"
            left-icon
            class="special-search"
            readonly
            @click="showPostIdPicker = true"
            right-icon
          >
            <template #right-icon>
              <van-icon name="arrow-down" size="16" v-if="!searchForm.postId" />
              <van-icon name="clear" size="16" v-else @click.stop="clearOption('postId')" />
            </template>
          </van-search>
        </div>
        <div class="search-input special-input-box">
          <van-search
            type="text"
            placeholder="请选择状态"
            v-model="status"
            v-if="selectTab==='1'"
            left-icon
            class="special-search"
            readonly
            @click="showStatusPicker = true"
            right-icon
          >
            <template #right-icon>
              <van-icon name="arrow-down" size="16" v-if="!searchForm.status" />
              <van-icon name="clear" size="16" v-else @click.stop="clearOption('status')" />
            </template>
          </van-search>
          <van-search
            type="text"
            placeholder="请选择日期"
            v-model="searchForm.validTime"
            left-icon
            class="special-search"
            readonly
            @click="showValidTimePicker = true"
            right-icon
          >
            <template #right-icon>
              <van-icon name="arrow-down" size="16" v-if="!searchForm.validTime" />
              <!-- <van-icon name="clear" size="16" v-else @click.stop="clearOption('validTime')" /> -->
            </template>
          </van-search>
        </div>
        <div class="search-input special-input-box">
          <van-search
            type="text"
            placeholder="请选择重点工作类别"
            v-if="selectTab==='1'"
            v-model="searchForm.workType"
            left-icon
            class="special-search"
            readonly
            @click="showWorkTypePicker = true"
            right-icon
          >
            <template #right-icon>
              <van-icon name="arrow-down" size="16" v-if="!searchForm.workType" />
              <van-icon name="clear" size="16" v-else @click.stop="clearOption('workType')" />
            </template>
          </van-search>
          <div class="search_btn" @click="onSearch">
            <van-icon name="search" class="icon"></van-icon>
          </div>
          <div class="search_btn reset" @click="resetForm">
            <van-icon name="cross" class="icon"></van-icon>
          </div>
        </div>
      </div>
      <div class="container-list">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            :immediate-check="false"
            @load="onLoad"
          >
            <template v-if="selectTab == '1'">
              <div class="grid_item" v-for="(item, index) in list" :key="index">
                <div class="sp">
                  <div class="sp_ft">重点工作类别:{{item.workType}}</div>
                </div>
                <div class="sphandle" @click="handleFn(item)">处理</div>
                <!-- <div class="sphandle" :disabled="item.isReed === '1'" v-else @click="readFn(item)">已读</div> -->
                <div class="sp">
                  <div class="sp_lf">清单详情:</div>
                  <div class="sp_rg">{{item.workDetail}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">频率:</div>
                  <div
                    class="sp_rg"
                  >{{item.frequencyType | formatFilter('frequencyType',frequencyTypeList)}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">岗位名称:</div>
                  <div class="sp_rg">{{item.postName}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">是否逾期:</div>
                  <div class="sp_rg">{{item.isOverdue == '0' ? '否' : '是'}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">状态:</div>
                  <div class="sp_rg">{{item.status | formatFilter('status',statusList)}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">备注:</div>
                  <div class="sp_rg">{{item.remark}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">柜员姓名:</div>
                  <div class="sp_rg">{{item.nickName}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">开始时间:</div>
                  <div class="sp_rg">{{item.startTime}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">结束时间:</div>
                  <div class="sp_rg">{{item.endTime}}</div>
                </div>
              </div>
            </template>
            <template v-if="selectTab == '2'">
              <div class="grid_item" v-for="(item, index) in list" :key="index">
                <div class="sp">
                  <div class="sp_ft">排名:{{item.rankNum}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">岗位名称:</div>
                  <div class="sp_rg">{{item.postName}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">柜员姓名:</div>
                  <div class="sp_rg">{{item.nickName}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">频率:</div>
                  <div
                    class="sp_rg"
                  >{{item.frequencyType | formatFilter('frequencyType',frequencyTypeList)}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">总数:</div>
                  <div class="sp_rg">{{item.totalCount}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">完成数:</div>
                  <div class="sp_rg">{{item.completeCount}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">完成率:</div>
                  <div class="sp_rg">{{item.completeRate}}%</div>
                </div>
              </div>
            </template>
          </van-list>
        </van-pull-refresh>
      </div>
      <van-popup v-model="showFrequencyTypePicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="frequencyTypeList"
          value-key="label"
          @cancel="showFrequencyTypePicker = false"
          @confirm="onConfirmFrequencyType"
        />
      </van-popup>
      <van-popup v-model="showPostIdPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="postIdList"
          value-key="postName"
          @cancel="showPostIdPicker = false"
          @confirm="onConfirmPostId"
        />
      </van-popup>
      <van-popup v-model="showStatusPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="statusList"
          value-key="label"
          @cancel="showStatusPicker = false"
          @confirm="onConfirmStatus"
        />
      </van-popup>
      <van-popup v-model="showValidTimePicker" position="bottom">
        <van-datetime-picker
          v-model="validTime"
          type="date"
          show-toolbar
          @cancel="showValidTimePicker = false"
          @confirm="onConfirmValidTime"
        />
      </van-popup>
      <van-popup v-model="showWorkTypePicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="workTypeList"
          value-key="dictLabel"
          @cancel="showWorkTypePicker = false"
          @confirm="onConfirmWorkType"
        />
      </van-popup>
    </div>
  </div>
</template>
<script>
import moment from "moment";
import navbar from "../../../components/navbar/navbar.vue";
import {
  workRecordList,
  workRecordCompleteStatus,
  systemUserOptionselect,
  systemUserType,
  recordRead
} from "@/service/api/home";
import _ from "lodash";
import {
  Tab,
  Tabs,
  Icon,
  Button,
  Search,
  RadioGroup,
  Radio,
  List,
  PullRefresh,
  Dialog,
  Toast,
  Picker,
  DatetimePicker
} from "vant";
export default {
  data() {
    return {
      selectTab: "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: [],
      searchForm: {
        frequencyType: "",
        postId: "",
        status: "",
        validTime: "",
        workType: ""
      },
      frequencyType: "",
      postId: "",
      status: "",
      validTime: "",
      workType: "",
      showFrequencyTypePicker: false,
      showPostIdPicker: false,
      showStatusPicker: false,
      showValidTimePicker: false,
      showWorkTypePicker: false,

      pageNum: 1,
      pageSize: 5,
      refreshing: false,
      loading: false,
      finished: false,
      currentPage: 1,
      list: []
    };
  },
  computed: {
    isShowFlag() {
      let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
      let arr = _.intersection(userInfo.roles || [], [
        "admin",
        "headAdmin",
        "branchAdmin",
        "headPublic",
        "headPrivate",
        "outletAdmin",
        "headOps"
      ]);
      if (arr.length > 0) {
        return true;
      } else {
        return false;
      }
    }
  },
  components: {
    navbar: navbar,
    VanTab: Tab,
    VanTabs: Tabs,
    VanIcon: Icon,
    VanButton: Button,
    VanSearch: Search,
    VanRadioGroup: RadioGroup,
    VanRadio: Radio,
    VanList: List,
    VanPullRefresh: PullRefresh,
    VanToast: Toast,
    [Dialog.Component.name]: Dialog.Component,
    VanPicker: Picker,
    VanDatetimePicker: DatetimePicker
  },
  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;
      }
    }
  },
  created() {
    let userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    this.systemUserOptionselectFn();
    this.systemUserTypeFn();
    // this.searchForm.frequencyType = "0";
    // this.frequencyType = "每日";
    // this.searchForm.validTime = moment(new Date()).format("YYYY-MM-DD");
    // this.validTime = new Date();
    this.queryList();
  },
  methods: {
    async systemUserOptionselectFn() {
      const res = await systemUserOptionselect({});
      if (res.code == 200) {
        this.postIdList = res.data;
      }
    },
    async systemUserTypeFn() {
      const res = await systemUserType({ distType: "work_type" });
      if (res.code == 200) {
        this.workTypeList = res.data;
      }
    },
    onConfirmFrequencyType(item) {
      this.searchForm.frequencyType = item.value;
      this.frequencyType = item.label;
      this.showFrequencyTypePicker = false;
      this.onRefresh();
    },
    onConfirmPostId(item) {
      this.searchForm.postId = item.postId;
      this.postId = item.postName;
      this.showPostIdPicker = false;
      this.onRefresh();
    },
    onConfirmStatus(item) {
      this.searchForm.status = item.value;
      this.status = item.label;
      this.showStatusPicker = false;
      this.onRefresh();
    },
    onConfirmValidTime() {
      this.searchForm.validTime = moment(this.validTime).format("YYYY-MM-DD");
      this.showValidTimePicker = false;
      this.onRefresh();
    },
    onConfirmWorkType(item) {
      this.searchForm.workType = item.dictLabel;
      this.workType = item.dictLabel;
      this.showWorkTypePicker = false;
      this.onRefresh();
    },
    clearOption(key) {
      this.searchForm[key] = "";
      this[key] = undefined;
      this.onRefresh();
    },
    onSearch() {
      this.onRefresh();
    },
    tabChange() {
      this.searchForm = {
        frequencyType: "",
        postId: "",
        status: "",
        validTime: "",
        workType: ""
      };
      this.frequencyType = "";
      this.postId = "";
      this.status = "";
      this.validTime = "";
      this.workType = "";
      this.searchForm.frequencyType = this.selectTab === '1' ? '' : "0";
      this.frequencyType = this.selectTab === '1' ? '' : "每日";
      // this.searchForm.validTime = moment(new Date()).format("YYYY-MM-DD");
      // this.validTime = new Date();
      this.onRefresh();
    },
    resetForm() {
      this.searchForm = {
        frequencyType: "",
        postId: "",
        status: "",
        validTime: "",
        workType: ""
      };
      this.frequencyType = "";
      this.postId = "";
      this.status = "";
      this.validTime = "";
      this.workType = "";
      // this.searchForm.frequencyType = "0";
      // this.frequencyType = "每日";
      // this.searchForm.validTime = moment(new Date()).format("YYYY-MM-DD");
      // this.validTime = new Date();
      this.onRefresh();
    },
    //下拉刷新
    onRefresh() {
      this.list = [];
      this.finished = false; //解除上拉加载之后数据不能再次上拉
      this.currentPage = 1;
      this.pageNum = this.currentPage;
      this.queryList();
    },
    //上拉加载
    onLoad() {
      this.pageNum = this.currentPage;
      this.queryList();
    },
    // 查询列表
    async queryList() {
      if (this.currentPage === 1) {
        this.list = [];
      }
      this.loading = true;
      const params = {
        ...this.searchForm,
        validTime: `${this.searchForm.validTime} 01:00`,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      let api =
        this.selectTab == "1" ? workRecordList : workRecordCompleteStatus;
      const res = await api(params);
      if (res.code == 200) {
        if (this.refreshing) {
          this.refreshing = false;
        }
        this.loading = false;
        if (res.rows) {
          this.list =
            params.pageNum == "1" ? res.rows : [...this.list, ...res.rows];
          if (res.rows.length < params.pageSize) {
            this.finished = true;
          }
          if (this.list.length == res.total) {
            this.finished = true;
          }
          this.currentPage++;
        } else {
          this.finished = true;
        }
      }
    },
    handleFn(item) {
      sessionStorage.setItem("workmngdaytaskedit", JSON.stringify(item));
      this.$router.push("/workmngdaytaskedit");
    },
    readFn(item) {
      recordRead({ id: item.id }).then(res => {
        if (res.code === 200) {
          this.$toast("已读成功");
          this.queryList();
        }
      })
    }
  }
};
</script>
<style lang="less" scoped>
.sphandle {
  position: absolute;
  right: 10px;
  top: 8px;
  background-color: rgba(233, 241, 253);
  color: #2372eb;
  padding: 5px 5px;
  border-radius: 5px;
  width: 44px;
}
.sp {
  display: flex;
  flex-direction: row;
  font-size: 14px;
  color: #595959;
  &_ft {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 10px;
  }
  &_lf {
    // width: 100px;
    flex: 0 0 auto;
  }
  &_rg {
    margin-left: 8px;
    flex: 1;
    min-width: 0px;
    line-height: 22px;
  }
}
.main_box {
  position: relative;
  .backTop {
    position: fixed;
    bottom: 150px;
    right: 15px;
    width: 64px;
    height: 64px;
    border-radius: 50%;
    background: #4682ff;
    box-shadow: 0px -2px 6px 0px rgba(70, 130, 255, 1);
    color: #fff;
    transform: rotate(180deg);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 20px;
    .heng {
      display: block;
      width: 15px;
      border: 1px solid #fff;
    }
  }
}
.wrapper {
  padding-top: 46px;
  min-height: 100vh;
  font-size: 16px;
  padding-bottom: 60px;
  // position: relative;
  .banner-box {
    // position: fixed;
  }
}
.task-box {
  margin: 12px;
  background: #1990ff;
  border-radius: 4px;
  padding-bottom: 9px;
  .task-title {
    font-size: 16px;
    color: #fff;
    text-align: center;
    line-height: 43px;
    font-weight: 600;
  }
  .grid_item_middle {
    margin: 0 8px;
    background: rgba(255, 255, 255, 0.85);
    border-radius: 4px;
  }
}

.wrapper_top {
  background-color: #fff;
  padding: 15px 20px;
}

.search-input {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 15px;
  background: #fff;
  .special-search {
    ::v-deep .van-field__control {
      font-size: 12px;
    }
  }
}

.special-input-box {
  margin-top: 0;
}

.search_btn {
  background-color: rgb(24, 144, 255);
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 40px;
  height: 40px;
  color: #fff;
  font-size: 20px;
  .icon {
    display: inline-block;
    width: 40px;
    text-align: center;
  }
}
.reset {
  background: #f7f8fa;
  color: rgba(0, 0, 0, 0.45);
  margin-left: 3px;
  margin-right: 8px;
}

/deep/ .van-search {
  width: 100%;
  padding: 5px 8px;
}

.radio_box {
  display: flex;
  align-items: center;
  margin: 15px 0 15px 15px;
}

.btn_box {
  padding: 8px 16px;
  background-color: #fff;
}

.isChecked {
  background-color: rgb(35, 114, 235);
  color: #fff;
  border-radius: 4px;
}
.grid_item {
  margin-top: 8px;
  padding: 10px 15px;
  background-color: #fff;
  position: relative;
  .item_content_box {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    .item_content {
      margin-top: 8px;
      flex: 0 0 50%;
      font-size: 12px;
      .item_left {
        color: #8c8c8c;
      }
      .item_right {
        color: #262626;
        font-weight: 600;
        word-break: break-all;
      }
    }
  }
  .item_footer {
    text-align: right;
    margin-top: 10px;
    .btn {
      height: 30px;
      margin-left: 8px;
    }
  }
}
.top-box {
  display: flex;
  justify-content: space-between;
  .right-label {
    .label {
      display: inline-block;
      background: rgba(73, 177, 108, 0.1);
      color: rgba(73, 177, 108, 1);
      font-size: 14px;
      border-radius: 4px;
      margin-left: 5px;
      line-height: 20px;
      padding: 0 5px;
    }
  }
}
.grid_item_top {
  display: flex;
  align-items: center;
}
.icon_box {
  background-color: rgb(35, 114, 235);
  border-radius: 4px;
  width: 25px;
  height: 25px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.grid_item_top_icon {
  font-size: 16px;
  color: #fff;
}
.grid_item_top_title {
  font-size: 16px;
  font-weight: 600;
  margin-left: 10px;
}
.grid_item_middle {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: rgb(249, 249, 250);
  padding: 10px 15px;
  margin-top: 10px;
}
.grid_item_middle_item {
  text-align: center;
  width: 30%;
}
.middle_num {
  margin-top: 10px;
  font-weight: 600;
  font-size: 14px;
  color: #262626;
}
.middle_title {
  font-size: 12px;
  color: #595959;
}
.line {
  width: 1px;
  height: 30px;
  background-color: rgb(255, 255, 255);
  border: 1px solid rgb(218, 220, 224);
}
.grid_item_bottom {
  margin-top: 8px;
  font-size: 12px;
}
.grid_item_bottom_item {
  margin-top: 5px;
  .branch-detail {
    color: #4886f8;
    margin-left: 5px;
  }
}
.bottom_item_left {
  color: rgb(140, 140, 140);
}
.myDialog {
  .content {
    padding: 5px 15px;
    .my-title {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.45);
    }
    .item {
      margin: 8px 0;
      .index {
        display: inline-block;
        width: 16px;
        height: 16px;
        background: rgba(22, 132, 252, 1);
        color: #fff;
        border-radius: 4px;
        font-size: 14px;
        text-align: center;
        line-height: 16px;
      }
      .branch-name {
        font-size: 14px;
        color: rgba(0, 0, 0, 0.85);
        margin-left: 5px;
      }
    }
    .grid-box {
      max-height: 150px;
      overflow-y: auto;
    }
  }
  ::v-deep .van-button__content {
    color: #666;
  }
}
</style>