<template>
  <view class="page">
    <qn-header>
      <view>
        <qn-data-picker
          v-model="params.cooperationState"
          :border="false"
          class="qn-picker"
          placeholder="客户类型"
          popup-title="请选择客户类型"
          :map="{ text: 'name', value: 'id' }"
          @change="onStatusChange"
          :clear-icon="false"
          :localdata="cooperationStateList"
        ></qn-data-picker>
      </view>
    </qn-header>
    <view class="content">
      <scroll-list ref="list" :option="option" @load="upCallback" @refresh="downCallback">
        <packingStationItem style="margin-bottom: 20rpx" v-for="item in list" :key="item.enterpriseId" :info="item">
          <view class="tax-area flex-row-center-center">
            <view class="tax-button flex-row-center-center" @click="showModal">
              <text class="text">设置税点</text>
            </view>
          </view>
        </packingStationItem>
      </scroll-list>
    </view>
    <uni-popup ref="popup" type="bottom" @maskClick="closeModal">
      <view class="flex-col section_7">
        <view class="flex-col">
          <view class="flex-col group_16">
            <view class="justify-between group_17">
              <text class="text_23">供应企业</text>
              <image src="/static/imgs/general/close-icon.png" class="image_15" />
            </view>
            <qn-select :options="mallSupplierList" v-model="popupInfo.mallSupplierId" placeholder="请选择当前账号下的供应商"></qn-select>
          </view>
          <view class="flex-col group_18">
            <view class="flex-row group_19">
              <text class="text_25">税点(%)</text>
              <text class="text_26">(例如：12=12%)</text>
            </view>
            <view class="bottom-section">
              <qn-easyinput type="number" :maxlength="4" v-model="popupInfo.taxPoint" :inputBorder="false" text="left" placeholder="请输入税点"></qn-easyinput>
            </view>
          </view>
        </view>
        <view class="flex-col items-center button" @click="savaTax">
          <text>保存税点</text>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import qnHeader from '@/components/qn-header/qn-header.vue'
import { getCompanyList, getClientTaxPoint, editClientTaxPoint } from '@/apis/clientListApi'
import { getBaseInfo } from '@/apis/commonApi'
import scrollList from '@/components/scroll-list/scroll-list.vue'
import packingStationItem from '@/components/business-components/packingStationItem.vue'
export default {
  components: {
    qnHeader,
    scrollList,
    packingStationItem
  },
  data() {
    return {
      // items: [],
      // area: null,
      params: {
        cooperationState: 0,
        latitude: 23.12616,
        longitude: 113.38466,
        km: 5
      },
      pagination: {
        pageNum: 0, // 初始会执行一次下拉加载
        pageSize: 10
      },
      cooperationStateList: Object.freeze([
        {
          id: 0,
          name: '全部客户'
        },
        {
          id: 1,
          name: '未合作'
        },
        {
          id: 2,
          name: '已合作'
        }
      ]),
      list: [],
      option: {
        size: 10,
        auto: true,
        emptyText: '暂无数据~',
        background: '#F7F8FA'
      },
      popupInfo: {
        enterpriseId: '',
        mallSupplierId: '',
        mallSupplierName: '',
        taxPoint: '',
        id: null
      },
      mallSupplierList: []
    }
  },
  methods: {
    onStatusChange(e) {
      this.pagination.pageNum = 1
      this.$refs.list.refresh()
    },
    getList() {
      return new Promise((resolve, reject) => {
        getCompanyList({ ...this.params, ...this.pagination })
          .then((res) => {
            if (res) {
              if (res.current <= 1) {
                this.list = res.records
              } else {
                this.list = this.list.concat(res.records)
              }
              resolve({ list: this.list, total: res.total })
            } else {
              reject()
            }
          })
          .catch((err) => {
            reject(err)
          })
      })
    },
    downCallback() {
      this.pagination.pageNum = 1
      this.getList()
        .then(({ list, total }) => {
          this.$refs.list.refreshSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.list.refreshFail()
        })
    },
    upCallback(page) {
      this.pagination.pageNum++
      this.getList()
        .then(({ list, total }) => {
          this.$refs.list.loadSuccess({ list, total })
        })
        .catch(() => {
          this.$refs.list.loadFail()
        })
    },
    showModal(company) {
      this.popupInfo.enterpriseId = company.enterpriseId
      if (this.mallSupplierList.length == 0) {
        getBaseInfo({}, true).then((res) => {
          if (res) {
            this.mallSupplierList = res.enterpriseList.map((item) => ({
              value: item.supplier.id,
              label: item.name
            }))
            this.$refs.popup.open('bottom')
          }
        })
      } else {
        this.$refs.popup.open('bottom')
      }
    },
    closeModal() {
      this.popupInfo = {
        enterpriseId: null,
        mallSupplierId: null,
        mallSupplierName: '',
        taxPoint: null,
        id: null
      }
      this.$refs.popup.close()
    },
    addCustomer() {
      console.log('addCustomer', this.popupInfo)
      this.closeModal()
    },
    savaTax() {
      if (!this.popupInfo.mallSupplierId) {
        uni.showToast({
          title: '请先选择纸盘商',
          icon: 'none'
        })
        return
      }
      if (this.popupInfo.taxPoint === '') {
        uni.showToast({
          title: '请输入税点',
          icon: 'none'
        })
        return
      }
      editClientTaxPoint({
        supplierId: this.popupInfo.mallSupplierId,
        customerEnterpriseId: this.popupInfo.enterpriseId,
        taxPoint: this.popupInfo.taxPoint / 100,
        id: this.popupInfo.id
      }).then((res) => {
        if (res) {
          uni.showToast({
            title: '保存成功',
            icon: 'none'
          })
          this.closeModal()
        }
      })
    }
  },
  watch: {
    params: {
      handler(val) {
        this.getList()
      },
      deep: true
    },
    ['popupInfo.mallSupplierId'](val) {
      if (val) {
        getClientTaxPoint({ supplierId: val, customerEnterpriseId: this.popupInfo.enterpriseId }).then((res) => {
          if (res && res.taxPoint) {
            this.popupInfo.taxPoint = res.taxPoint * 100
            this.popupInfo.id = res.id
          }
        })
      }
    }
  },
  computed: {
    hasLogin() {
      return this.$store.state.qnToken != ''
    }
  },
  onLoad(option) {
    if (option) {
      option.latitude && (this.params.latitude = option.latitude)
      option.longitude && (this.params.longitude = option.longitude)
      option.km && (this.params.km = option.km)
    }
  }
}
</script>

<style lang="scss" scoped>
.page {
  display: flex;
  flex-direction: column;
  flex: 1;
  height: 100vh;
  .content {
    // flex: 1;
    flex-grow: 1;
    overflow: hidden;
  }
}
.qn-picker {
  border: none;
}
.section_7 {
  width: 750rpx;
  padding: 3rpx 32rpx 12rpx;
  background-color: rgb(255, 255, 255);
  border-radius: 20rpx 20rpx 0px 0px;
  .button {
    margin-top: 455rpx;
    padding: 23rpx 0;
    color: rgb(255, 255, 255);
    font-size: 36rpx;
    font-weight: 500;
    line-height: 50rpx;
    white-space: nowrap;
    background-color: rgb(0, 122, 255);
    border-radius: 10rpx;
  }
  .group_16 {
    padding-bottom: 28rpx;
    border-bottom: solid 6rpx rgb(216, 216, 216);
    .group_17 {
      padding: 31rpx 0 24rpx;
      color: rgb(51, 51, 51);
      font-size: 30rpx;
      line-height: 42rpx;
      white-space: nowrap;
      .text_23 {
        margin-top: 6rpx;
      }
      .image_15 {
        margin-right: 4rpx;
        width: 40rpx;
        height: 40rpx;
      }
    }
  }
  .group_18 {
    padding-bottom: 32rpx;
    .group_19 {
      padding: 31rpx 0 24rpx;
      .text_25 {
        color: rgb(51, 51, 51);
        font-size: 30rpx;
        line-height: 42rpx;
        white-space: nowrap;
      }
      .text_26 {
        margin: 5rpx 0 5rpx 16rpx;
        color: rgb(136, 136, 136);
        font-size: 24rpx;
        line-height: 33rpx;
        white-space: nowrap;
      }
    }
  }
  .bottom-section {
    line-height: 40rpx;
    // padding: 12rpx 24rpx;
    border-radius: 8rpx;
    background-color: rgb(245, 245, 245);
    width: 100%;
    height: 64rpx;
  }
}
.tax-area {
  width: 750rpx;
  height: 100rpx;
  padding: 18rpx 32rpx;
  border-top: 1px solid rgba(221, 221, 221, 0.5);
  .tax-button {
    width: 268rpx;
    height: 64rpx;
    background: #007aff;
    border-radius: 4rpx;
    .text {
      font-size: 28rpx;
      color: #ffffff;
      letter-spacing: 0;
      font-weight: 400;
    }
  }
}
</style>
