<template>
  <view>
    <uni-segmented-control :current="current" :values="tabList" @clickItem="tabClick" styleType="text" activeColor="#ffaa00" />
    <view class="content">
      <view style="margin-top: 10rpx;">
        <uni-list :border="true">
          <uni-swipe-action v-for="(item,index) in dataList" :key="index">
            <!-- 全部 -->
            <uni-swipe-action-item v-if="current === 0" :right-options="getOpts(item.ddzt)" :disabled="item.ddzt === '4'" :show="isOpened"
              :auto-close="false" @click="bindClick(item)">
              <uni-list-item :title="item.cpName" :note="'下单人:' + item.shxm + ' 单价:' + item.je + '元'" :thumb="item.tx" thumb-size="lg"
                :rightText="transDictLabel(dicts.WL007,item.ddzt)" link="navigateTo" :to="`/pages/order/add?item=`+JSON.stringify(item)">
              </uni-list-item>
            </uni-swipe-action-item>

            <!-- 待接单/待派单/进行时/完成 -->
            <uni-swipe-action-item v-else-if="current == item.ddzt" :right-options="getOpts(item.ddzt)" :disabled="item.ddzt === '4'" :show="isOpened"
              :auto-close="false" @click="bindClick(item)">
              <uni-list-item :title="item.cpName" :note="'下单人:' + item.shxm + ' 单价:' + item.je + '元'" :thumb="item.tx" thumb-size="lg"
                :rightText="transDictLabel(dicts.WL007,item.ddzt)" link="navigateTo" :to="`/pages/order/add?item=`+JSON.stringify(item)">
              </uni-list-item>
            </uni-swipe-action-item>
          </uni-swipe-action>
        </uni-list>
      </view>
      <uni-load-more :status="status" :content-text="contentText" @clickLoadMore="clickLoadMore" />
    </view>

    <uni-fab :pattern="pattern" :horizontal="horizontal" :vertical="vertical" @fabClick="fabClick"></uni-fab>
  </view>
</template>

<script>
  import { listDdjy, updateDdjy } from '@/api/order.js'
  import config from '@/config.js'
  import { decryptRowData_ECB } from '@/common/sm4.js'
  export default {
    data() {
      return {
        dicts: {
          WL007: [],
        },
        isOpened: 'none',
        tabList: ['全部', '待接单', '待派单', '进行中', '已完成'],
        current: 0,
        horizontal: 'right',
        vertical: 'bottom',
        pattern: {
          buttonColor: '#ffaa00',
          iconColor: '#fff'
        },
        status: 'more',
        contentText: {
          contentdown: '查看更多>',
          contentrefresh: '加载中...',
          contentnomore: '没有更多了'
        },
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        dataList: [],
      }
    },
    created() {
      this.getDictList(Object.keys(this.dicts), this.dicts)
      this.getList()
    },
    onPullDownRefresh() {
      this.queryParams.pageNum = 1
      this.getList(true)
    },
    methods: {
      getList(isClear) {
        isClear ? this.dataList = [] : ''
        listDdjy(this.queryParams).then(res => {
          this.dataList = res.rows
          this.dataList.forEach(e => {
            e.tx = e.tx ? config.baseUrl + e.tx : config.baseUrl + config.head;
          })
          this.total = res.total
          this.total > this.dataList.length ? this.status = 'more' : this.status = 'noMore'
        })
        setTimeout(function() {
          uni.stopPullDownRefresh();
        }, 1000);
      },

      tabClick(e) {
        if (this.current != e.currentIndex) {
          this.current = e.currentIndex;
        }
      },

      fabClick() {
        uni.navigateTo({
          url: '/pages/order/add'
        })
      },
      
      bindClick(e) {
        this.isOpened = 'none'
        const params = {
          id: e.id,
          ddzt: Number(e.ddzt) + 1
        }
        updateDdjy(params).then(res => {
          if (res.code === 200) {
            uni.showToast({
              title: `保存成功！`,
              duration: 2000,
              success: (res) => {
                this.getList(true)
              }
            })
          }
        })
      },

      clickLoadMore(e) {
        if (this.total > this.dataList.length) {
          this.status = 'loading'
          this.form.pageNum += 1
          this.getList()
        }
      },

      getOpts(ddzt) {
        let options = [{ text: '完成', style: { backgroundColor: '#F56C6C' } }]
        switch (ddzt) {
          case '1':
            options[0].text = '接单'
            break;
          case '2':
            options[0].text = '派单'
            break;
          case '3':
            options[0].text = '完成'
            break;
        }
        return options
      },
    }
  }
</script>

<style>
</style>