<template>
  <div class="main_box">
    <navbar title="预警任务列表"></navbar>
    <div class="wrapper">
      <div class="banner-box">
        <div class="search-input special-input-box">
          <van-search
            type="text"
            placeholder="请选择状态"
            v-model="status"
            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>
        </div>
        <!-- <div class="search-input special-input-box">
          <van-search
            type="text"
            placeholder="请输入预警类型"
            v-model="searchForm.alterType"
            left-icon
            class="special-search"
            right-icon
            :clearabled="false"
          >
          </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>
              <div class="grid_item" v-for="(item, index) in list" :key="index">
                <div class="sp">
                  <div class="sp_ft">客户名称:{{item.custName}}</div>
                </div>
                <div :class="['sphandle', item.status === '2' && 'sphandledisabled']" v-if="item.isFeedback === '1'" @click="handleFn(item)">处理</div>
                <el-buttom :class="['sphandle', !!item.readTime && 'sphandledisabled']" v-else @click="readFn(item)">确认</el-buttom>
                <div class="sp">
                  <div class="sp_lf">客户号:</div>
                  <div class="sp_rg">{{item.custId}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">客户内码:</div>
                  <div class="sp_rg">{{item.custIsn}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">预警类型:</div>
                  <div class="sp_rg">{{item.alterType}}</div>
                </div>
                <div class="sp">
                  <div class="sp_lf">预警详情:</div>
                  <div class="sp_rg">{{item.alterDetail}}</div>
                </div>
                <div class="sp" v-if="item.isFeedback !== '1'">
                  <div class="sp_lf">是否已读:</div>
                  <div class="sp_rg" v-if="item.readTime">是</div>
                  <div class="sp_rg" v-else>否</div>
                </div>
                <div class="sp" v-if="item.isFeedback !== '1' && item.readTime">
                  <div class="sp_lf">已读时间:</div>
                  <div class="sp_rg">{{item.readTime}}</div>
                </div>
                <div class="sp" v-if="item.isFeedback === '1'">
                  <div class="sp_lf">状态:</div>
                  <div class="sp_rg">{{item.status | formatFilter('status',statusList)}}</div>
                </div>
                <div class="sp" v-if="item.isFeedback === '1'">
                  <div class="sp_lf">{{ alterType === '贷款流失提醒' ? '流失原因反馈:' : '备注:' }}</div>
                  <div class="sp_rg">{{item.remark}}</div>
                </div>
              </div>
            </template>
          </van-list>
        </van-pull-refresh>
      </div>
      <van-popup v-model="showStatusPicker" position="bottom">
        <van-picker
          show-toolbar
          :columns="statusList"
          value-key="label"
          @cancel="showStatusPicker = false"
          @confirm="onConfirmStatus"
        />
      </van-popup>
    </div>
  </div>
</template>
<script>
import moment from "moment";
import navbar from "../../../components/navbar/navbar.vue";
import {
  workWarningList,
  workRecordCompleteStatus,
  systemUserOptionselect,
  systemUserType,
  recordRead
} from "@/service/api/home";
import {
  Tab,
  Tabs,
  Icon,
  Button,
  Search,
  RadioGroup,
  Radio,
  List,
  PullRefresh,
  Dialog,
  Toast,
  Picker,
  DatetimePicker
} from "vant";
export default {
  data() {
    return {
      statusList: [
        {
          label: "未完成",
          value: "0"
        },
        {
          label: "已完成",
          value: "2"
        }
      ],
      searchForm: {
        // alterType: "",
        status: "",
      },

      pageNum: 1,
      pageSize: 5,
      refreshing: false,
      loading: false,
      finished: false,
      currentPage: 1,
      list: [],
      showStatusPicker: false,
      statusList: [
        {
          label: "未完成",
          value: "0"
        },
        {
          label: "已完成",
          value: "2"
        }
      ],
      status: "",
      alterType: ""
    };
  },
  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 == "status") {
        let arr = _.filter(list, { value: v });
        return arr[0].label;
      }
    }
  },
  created() {
    this.alterType = sessionStorage.getItem('yujingrenwyType');
    this.queryList();
  },
  methods: {
    onConfirmStatus(item) {
      this.searchForm.status = item.value;
      this.status = item.label;
      this.showStatusPicker = false;
      this.onRefresh();
    },
    clearOption(key) {
      this.searchForm[key] = "";
      this[key] = undefined;
      this.onRefresh();
    },
    onSearch() {
      this.onRefresh();
    },
    tabChange() {
      this.searchForm = {
        // alterType: "",
        status: "",
      };
      this.onRefresh();
    },
    resetForm() {
      this.searchForm = {
        // alterType: "",
        status: "",
      };
      this.status = "";
      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,
        alterType: this.alterType,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      const res = await workWarningList(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) {
      if (item.status === '2') return
      sessionStorage.setItem("workmngwarningtaskedit", JSON.stringify(item));
      this.$router.push("/workmngwarningtaskedit");
    },
    readFn(item) {
      if (item.readTime) return
      recordRead({ id: item.id }).then(res => {
        if (res.code === 200) {
          this.$toast("已读成功");
          this.onRefresh();
        }
      })
    }
  }
};
</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;
  text-align: center;
}
.sphandledisabled {
  background-color: #9d9d9d;
  color: #d2d1d1;
}
.sp {
  display: flex;
  flex-direction: row;
  font-size: 14px;
  color: #595959;

  &_ft {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 10px;
    width: calc(100vw - 77px);
  }
  &_lf {
    // width: 100px;
    flex: 0 0 auto;
  }
  &_rg {
    margin-left: 8px;
    flex: 1;
    min-width: 0px;
    line-height: 22px;
    color: #696868;
  }
}
.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>