<template>
  <view class="search">
    <u-search
      placeholder="请输入收货/出货单号"
      v-model="keyword"
      :showAction="false"
      :bgColor="'#fff'"
      :borderColor="'rgba(0, 0, 0, 0.1)'"
      :placeholderColor="'#C1C1C1'"
      @search="handleSearch()"
    ></u-search>
    <view class="btn" @click="handleAdd()"> 创建 </view>
  </view>
  <view class="filter">
    <!--  -->
    <view @click="handleDialog('showTime', true)"
      ><text>{{ state.name }}</text
      ><u-icon name="arrow-down"></u-icon
    ></view>
    <view @click="state.isShowStatus = true"
      ><text>费用类型</text><u-icon name="arrow-down"></u-icon
    ></view>
    <view class="btn" @click="handleDialog('showFilter', true)">筛选</view>
  </view>
  <view class="show-time">
    <view v-if="state.name === '昨日' || state.name === '今日'">{{
      state.startTime
    }}</view>
    <view v-else>{{ state.startTime }} - {{ state.endTime }}</view>
  </view>
  <page-view
    @loadList="
      (v) => {
        getList(v);
      }
    "
    :noMoreData="pageList.noMoreData"
    :list="pageList.list"
    :height="140"
    :isLoading="pageList.isLoading"
  >
    <view class="list">
      <u-swipe-action>
        <u-swipe-action-item
          :options="options1"
          v-for="(item, index) in pageList.list"
          :key="index"
          @click="handleItem(item)"
        >
          <view
            class="item"
            :style="{ border: index === 0 ? 'none' : '' }"
            @click="getDetail(item)"
          >
            <u-row justify="space-between">
              <u-col span="9">
                <view class="">
                  <view class=""> {{ item.supCusName || "-" }} </view>
                  <view class=""> {{ item.paymentNumber }} </view>
                  <view class="time">
                    结算时间：{{ item.settlementTime }}
                  </view>
                </view>
              </u-col>
              <u-col span="3">
                <view class="amount">
                  <view>￥ {{ item.totalPrice }}</view>
                  <view class="tip">{{ getPayment(item) }}</view>
                </view>
              </u-col>
            </u-row>
          </view>
        </u-swipe-action-item>
      </u-swipe-action>
    </view>
  </page-view>

  <!-- 时间弹框 -->
  <TimeDialog
    ref="timeDialog"
    :show="showDialog.showTime"
    @handleDialog="(v:boolean) => {handleDialog('showTime', v)}"
    @changeTime="changeTime"
  />

  <!-- 费用类型 -->
  <u-action-sheet
    :closeOnClickOverlay="true"
    :closeOnClickAction="true"
    :actions="state.statusList"
    :title="'单据状态'"
    :show="state.isShowStatus"
    @select="handleSelectStatus"
    @close="state.isShowStatus = false"
  ></u-action-sheet>

  <!-- 筛选弹框 -->
  <FilterDialog
    :show="showDialog.showFilter"
    @handleDialog="(v:boolean) => {handleDialog('showFilter', v)}"
    @changeOther="changeOther"
    :isShipment="false"
  />
</template>
<script setup lang="ts">
import { FinanceApi } from "@/services";
import PageView from "@/components/PageView/index.vue";
import { formatDate, getCurrentMonthStartAndEnd } from "@/utils";
import TimeDialog from "./components/TimeDialog.vue";
import FilterDialog from "./components/CustomFilterDialog.vue";
import { onShow } from "@dcloudio/uni-app";

const options1 = ref([
  {
    text: "删除",
    style: {
      backgroundColor: "rgba(217, 4, 30, 1)",
      fontSize: "24rpx",
    },
  },
]);
const keyword = ref("");
const state = reactive({
  startTime: formatDate(getCurrentMonthStartAndEnd().start, "{y}-{m}-{d}"),
  endTime: formatDate(getCurrentMonthStartAndEnd().end, "{y}-{m}-{d}"),
  name: "本月",
  currentPaymentType: -1,
  isShowStatus: false,
  statusList: [
    {
      name: "全部",
      key: -1,
    },
    {
      name: "供应商付款",
      key: 0,
    },
    {
      name: "客户付款",
      key: 1,
    },
    {
      name: "供应商(手动)",
      key: 3,
    },
    {
      name: "客户(手动)",
      key: 2,
    },
  ],
  userId: -1,
  userType: -1,
  params: {},
});

const getPayment = (item: any) => {
  return ["供应商付款", "客户付款", "客户(手动)", "供应商(手动)"][
    item.paymentType
  ];
};
const showDialog = <
  {
    [key: string]: boolean;
  }
>reactive({
  showTime: false,
  showFilter: false,
});

const handleDialog = (key: string, v: boolean) => {
  showDialog[key] = v;
};
const handleSelectStatus = (v: any) => {
  state.isShowStatus = false;
  state.currentPaymentType = v.key;
  resetPageList();
  getList();
};
const changeOther = (obj: any) => {
  state.userId = obj.userId;
  state.userType = obj.type;
  resetPageList();
  getList();
};
const pageList: PageResult<any> = reactive({
  total: 0,
  list: [],
  pageNum: 1,
  pageSize: 10,
});
const resetPageList = () => {
  pageList.noMoreData = false;
  pageList.total = 0;
  pageList.list = [];
  pageList.pageNum = 1;
  pageList.pageSize = 10;
};
const handleSearch = () => {
  resetPageList();
  getList();
};
const handleAdd = () => {
  uni.navigateTo({
    url: "/pagesApp/components/addPayment", // 要跳转到的页面路径
  });
};
// 删除
const handleItem = (item: any) => {
  FinanceApi.deletePaymentDs({ id: item.id }).then((res) => {
    if (res.code === 200) {
      uni.showToast({
        title: "删除成功",
      });
      resetPageList();
      getList();
    }
  });
};
// 获得详情
const getDetail = (item: any) => {
  uni.navigateTo({
    url: "/pagesApp/components/payContent?id=" + item.id, // 要跳转到的页面路径
  });
};
const changeTime = (obj: any) => {
  state.startTime = obj.startTime;
  state.endTime = obj.endTime;
  state.name = obj.name;
  resetPageList();
  getList();
};
const getList = (v?: boolean) => {
  if (v) {
    if (Math.ceil(pageList.total / pageList.pageSize) > pageList.pageNum) {
      pageList.pageNum++;
    } else {
      pageList.noMoreData = true;
      return;
    }
  }
  let params: any = {
    pageSize: pageList.pageSize,
    pageNumber: pageList.pageNum,
    startTime: state.startTime + " 00:00:00",
    endTime: state.endTime + " 23:59:59",
    oddNumbers: keyword.value,
  };
  if (state.currentPaymentType > -1) {
    params.paymentType = state.currentPaymentType;
  }
  if (state.userId > -1) {
    params.supCusId = state.userId;
    params.userType = state.userType;
  }

  pageList.isLoading = true;
  FinanceApi.getPaymentDetailsPage(params).then((res: any) => {
    if (res.code === 200) {
      pageList.isLoading = false;
      (pageList as any).list = (pageList as any).list = pageList.list.concat(
        res.data.list
      );
      pageList.total = (res.data as any).total;
    }
  });
};
onShow(() => {
  resetPageList();
  getList();
});
</script>
<style lang="scss" scoped>
.search {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 26rpx 26rpx 0rpx 26rpx;
  .btn {
    background: #00dcee;
    border-radius: 24rpx;
    border: 1px solid #00dcee;
    font-weight: 500;
    font-size: 26rpx;
    color: #ffffff;
    margin-left: 50rpx;
    padding: 6rpx 30rpx;
  }
}

.filter {
  margin: 0rpx 26rpx 0rpx 26rpx;
  padding: 18rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 400;
  font-size: 26rpx;
  color: #000000;
  > view {
    display: inline-block;
    display: flex;
    align-items: center;
    text {
      margin-right: 10rpx;
    }
  }
  .btn {
    font-size: 26rpx;
    color: #00dcee;
  }
}

.show-time {
  font-weight: 400;
  font-size: 26rpx;
  color: #000000;
  margin: 0rpx 26rpx 20rpx 50rpx;
}

.list {
  background: #ffffff;
  box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(5, 68, 37, 0.12);
  border-radius: 13rpx;
  margin: 0rpx 26rpx;
  padding: 0rpx 18rpx;
  .item {
    font-size: 26rpx;
    line-height: 40rpx;
    border-top: 1px solid rgba(233, 233, 233, 0.76);
    padding: 26rpx 26rpx;
    .time {
      font-size: 24rpx;
      color: #999;
    }
  }
  .amount {
    text-align: right;
    .tip {
      font-size: 24rpx;
      color: #ff7e20;
    }
  }
}
</style>
