<template>
  <Page name="freight-order" flank>
    <template #header>
      <u-tabs :current="activeTab" :list="tabList" @click="onTabChange" :scrollable="false" :activeStyle="{ color: '#1E7B6B' }"> </u-tabs>
      <view class="line"></view>
      <view class="quick-tag">
        <view class="quick-tag-group">
          <view class="quick-tag-item" :class="searchForm.feeType ? 'active' : ''" @click="selectQuickTag({ key: 'feeType', value: searchForm.feeType })">
            <text>{{ searchForm.feeTypeName || '款项类型' }}</text>
            <view class="sx-img">
              <image v-if="searchForm.feeType" class="sx-image" :src="formatImagePath('active-down')"></image>
              <image v-else class="sx-image" :src="formatImagePath('down')"></image>
            </view>
          </view>
          <view class="quick-tag-item" :class="searchForm.projectCode ? 'active' : ''" @click="selectQuickTag({ key: 'projectCode', value: searchForm.projectCode })">
            <text>{{ searchForm.projectName || '所属项目' }}</text>
            <view class="sx-img">
              <image v-if="searchForm.projectCode" class="sx-image" :src="formatImagePath('active-down')"></image>
              <image v-else class="sx-image" :src="formatImagePath('down')"></image>
            </view>
          </view>
          <view class="quick-tag-item" :class="searchForm.startCityCode ? 'active' : ''" @click="selectQuickTag({ key: 'startCityCode', value: searchForm.startCityCode })">
            <text>{{ searchForm.startCityName || '始发地' }}</text>
            <view class="sx-img">
              <image v-if="searchForm.startCityCode" class="sx-image" :src="formatImagePath('active-down')"></image>
              <image v-else class="sx-image" :src="formatImagePath('down')"></image>
            </view>
          </view>
          <view class="quick-tag-item" :class="searchForm.endCityCode ? 'active' : ''" @click="selectQuickTag({ key: 'endCityCode', value: searchForm.endCityCode })">
            <text>{{ searchForm.endCityName || '目的地' }}</text>
            <view class="sx-img">
              <image v-if="searchForm.startCityCode" class="sx-image" :src="formatImagePath('active-down')"></image>
              <image v-else class="sx-image" :src="formatImagePath('down')"></image>
            </view>
          </view>
        </view>
        <view class="quick-screen" @click="openFilter">
          <image class="search-image" :src="formatImagePath('search')"></image>
        </view>
      </view>
    </template>
    <template #content>
      <List ref="list" v-model="list" :api="orderAPI" @update:loading="e => (loading = e)" :verify="true">
        <template v-for="(item, index) in list">
          <card @apply="applySub" @audit="auditFun" @cancelApply="cancelApplySub" @tap="toDetail(item.orderCode)" :key="index" :item="item"></card>
        </template>
        <template #empty>
          <Empty tips="暂无数据" />
        </template>
      </List>
    </template>
    <u-popup :show="popShow" @close="closePop" closeable>
      <view class="popup">
        <view class="title">确定审核</view>
        <u-form label-width="90">
          <u-form-item label="审核结果" required>
            <u-radio-group v-model="auditForm.auditResultFlag">
              <u-radio name="true"> 审核通过 </u-radio>
              <u-radio name="false"> 审核不通过 </u-radio>
            </u-radio-group>
          </u-form-item>
          <u-form-item label="驳回原因" :required="auditForm.auditResultFlag === 'false'">
            <u--textarea v-model="auditForm.auditRejectMsg" placeholder="200字以内描述" :count="true" :maxlength="200" :cursorSpacing="60"></u--textarea>
          </u-form-item>
        </u-form>
        <view class="but">
          <u-button type="primary" @tap="auditSub">确定</u-button>
        </view>
      </view>
    </u-popup>
    <u-toast ref="uToast"></u-toast>
  </Page>
</template>
<script>
import { urlParam } from '@/utils/param';
import card from './card.vue';
import UFormItem from '../../uni_modules/uview-ui/components/u-form-item/u-form-item.vue';
export default {
  name: 'settlement',
  components: { UFormItem, card },
  data() {
    return {
      loading: false,
      popShow: false,
      auditForm: {
        ids: [],
        auditResultFlag: '',
        auditRejectMsg: '',
      },
      searchForm: {
        feeType: '', // 费用类型
        feeTypeName: '', // 费用类型
        projectCode: '',
        projectName: '',
        startCityCode: '',
        startCityName: '',
        applyStatus: '',
        orderCode: '', // 订单号
        customerOrderCodes: '', // 内部订单编号
        driverName: '',
        driverMobile: '', //
        vehicleLicenseNum: '',
        unHandledExceptionFlag: '', // 存在异常
        receiptFlag: '', // 是否已签单
        payStatus: '', // 支付状态
        payStatusName: '', //
      },
      activeTab: 0,
      count: {},
      list: [],
    };
  },
  onPullDownRefresh() {
    this.onSearch();
  },
  computed: {
    tabList() {
      let res = [
        { name: '待申请', value: 'WAIT_APPLY', key: 'waitApplyCount' },
        { name: '待审核', value: 'WAIT_AUDIT', key: 'waitAuditCount' },
        { name: '已审核', value: 'AUDITED', key: 'auditedCount' },
      ];
      return Object.keys(this.count).length > 0
        ? res.map(item => {
            return { ...item, name: `${item.name}(${this.count[item.key] || 0})`, disabled: this.loading };
          })
        : res;
    },
  },
  methods: {
    toDetail(code) {
      uni.navigateTo({ url: '/pages/order/detail?code=' + code });
    },
    onTabChange({ index }) {
      if (this.loading) {
        return;
      }
      this.activeTab = index;
      this.list = [];
      this.onSearch();
    },
    orderAPI(params) {
      let format = {
        ...this.searchForm,
        applyStatus: this.tabList[this.activeTab].value,
      };
      this.getAggregateType(format);
      return uni.$u.api.freightPayApply.page({
        ...format,
        ...params,
      });
    },
    // 获取聚合数量
    getAggregateType(params) {
      uni.$u.api.freightPayApply.getCount({ ...params, applyStatus: '' }).then(res => {
        this.count = res?.result || {};
      });
    },
    selectQuickTag({ key, value }) {
      this.searchForm = Object.assign(this.searchForm, { [key]: value });
      if (key == 'feeType') {
        uni.$once('select-dict', option => {
          this.searchForm.feeType = option.valueCode;
          this.searchForm.feeTypeName = option.valueName;
          this.onSearch();
        });
        uni.navigateTo({
          url: `/pages/global/search-dict${urlParam({
            title: '款项类型',
            dict: 'ONLINE_FREIGHT_ORDER_FEE_TYPE',
          })}`,
        });
      } else if (key == 'startCityCode') {
        uni.$once('select-city', option => {
          this.searchForm.startCityCode = option.code;
          this.searchForm.startCityName = option.shortName;
          this.onSearch();
        });
        uni.navigateTo({ url: '/pages/global/search-city' });
      } else if (key == 'endCityCode') {
        uni.$once('select-city', option => {
          this.searchForm.endCityCode = option.code;
          this.searchForm.endCityName = option.shortName;
          this.onSearch();
        });
        uni.navigateTo({ url: '/pages/global/search-city' });
      } else if (key == 'projectCode') {
        uni.$once('select-common', option => {
          this.searchForm.projectCode = option.code;
          this.searchForm.projectName = option.name;
          this.onSearch();
        });
        uni.navigateTo({
          url: `/pages/global/search-common${urlParam({
            mode: 'select',
            url: 'projectCode',
            title: '项目',
            label: 'name',
            value: '',
          })}`,
        });
      }
    },
    // 筛选
    openFilter() {
      uni.navigateTo({
        url: `/pages/settlement/filter${urlParam(this.searchForm)}`,
        events: {
          refreshData: option => {
            this.searchForm = { ...this.searchForm, ...option };
            this.onSearch();
          },
        },
      });
    },
    applySub(item) {
      uni.$u.api.freightPayApply
        .apply([item.id])
        .then(res => {
          if (res.success) {
            uni.showToast({ title: '申请成功', icon: 'none' });
            this.onSearch();
          }
        })
        .catch(e => {
          this.$refs.uToast.show({
            type: 'default',
            message: e,
          });
        });
    },
    cancelApplySub(item) {
      uni.$u.api.freightPayApply
        .cancelApply([item.id])
        .then(res => {
          if (res.success) {
            uni.showToast({ title: '取消成功', icon: 'none' });
            this.onSearch();
          }
        })
        .catch(e => {
          this.$refs.uToast.show({
            type: 'default',
            message: e,
          });
        });
    },
    auditFun(item) {
      this.popShow = true;
      this.auditForm = {
        ids: [item.id],
        auditResultFlag: '',
        auditRejectMsg: '',
      };
      uni.hideTabBar();
    },
    closePop() {
      setTimeout(() => uni.showTabBar(), 300);
      this.popShow = false;
    },
    auditSub() {
      if (!this.auditForm.auditResultFlag) {
        uni.showToast({ title: '审核结果不能为空', icon: 'none' });
        return;
      }
      if (this.auditForm.auditResultFlag === 'false' && !this.auditForm.auditRejectMsg) {
        uni.showToast({ title: '驳回原因不能为空', icon: 'none' });
        return;
      }
      uni.$u.api.freightPayApply
        .audit(this.auditForm)
        .then(res => {
          if (res.success) {
            uni.showToast({ title: '审核成功', icon: 'none' });
            this.closePop();
            this.onSearch();
          }
        })
        .catch(e => {
          this.$refs.uToast.show({
            type: 'default',
            message: e,
          });
        });
    },
  },
};
</script>
<style lang="scss">
.page-freight-order {
  &__header {
    padding: 0 !important;

    .line {
      height: 1rpx;
      background: #f6f6f6;
      position: relative;
      top: -5rpx;
    }

    .quick-tag {
      display: flex;
      padding: 15upx 22upx;
      box-sizing: border-box;
      position: relative;

      .quick-cover {
        width: 25upx;
        height: 90upx;
        position: absolute;
        right: 122upx;
        top: 0;

        .cover-image {
          width: 100%;
          height: 100%;
        }
      }

      .quick-screen {
        width: 100upx;
        display: flex;
        align-items: center;
        justify-content: center;

        .search-image {
          width: 42upx;
          height: 42upx;
        }
      }

      .quick-tag-group {
        flex: 1;
        display: flex;
        align-items: center;
        background-color: #fff;
        padding-left: $padding-xs !important;
        padding-right: $padding-xs !important;
        overflow-y: auto;

        .quick-tag-item {
          flex-shrink: 0;
          min-width: 158upx;
          height: 58upx;
          background: #f6f6f6;
          border-radius: 8upx;
          font-size: 28upx;
          font-weight: 400;
          color: #2b2a27;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 0 10upx;
          box-sizing: border-box;

          &:not(:last-child) {
            margin-right: 18upx;
          }

          &.active {
            background: #ffffff;
            border-radius: 8upx;
            border: 2upx solid #2673fb;
            font-size: 28upx;
            font-family:
              PingFangSC,
              PingFang SC;
            font-weight: 400;
            color: #2673fb;
          }

          .sx-img {
            height: 12upx;
            width: 14upx;
            position: relative;
            margin-left: 12upx;

            .sx-image {
              width: 100%;
              height: 100%;
              position: absolute;
              top: 0;
              left: 0;
            }
          }
        }

        .z-dropdown__text .text {
          word-break: keep-all;
        }
      }
    }
  }
  .add-order {
    right: 34upx;
    bottom: 34upx;
    height: 106upx;
    width: 106upx;
    position: fixed;
    .sx-image {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }
  }
  .popup {
    padding-left: $padding-md;
    padding-right: $padding-md;
    border-top-left-radius: $radius-md;
    border-top-right-radius: $radius-md;
    .title {
      padding-top: $padding-xs;
      padding-bottom: $padding-xs;
      display: flex;
      align-content: center;
      justify-content: center;
      font-weight: 400;
      font-size: 32rpx;
      color: #2b2a27;
      line-height: 45px;
      text-align: left;
      font-style: normal;
    }
    .but {
      margin-top: $padding-md;
    }
  }
}
</style>
