<template>
  <view class="container">
    <z-paging ref="pagingRef" class="containerBox" v-model="localListData" :auto="false"
      :default-page-size="queryParams.pageSize" @query="handleQuery">
      <template #top>
        <uv-search v-model="queryParams.billNo" placeholder="请输入" :showAction="true" actionText="搜索"
          @search="handleSearch" @custom="handleSearch"></uv-search>
        <view class="dropDown">
          <view class="item_dropDown" @tap="openPicker">
            <uv-text :lines="1" :text="storageTypeText" :color="storageTypeText != '全部' ? '#3c9cff' : ''"
              class="item_text"></uv-text>
            <uv-icon name="arrow-down-fill" :color="storageTypeText != '全部' ? '#3c9cff' : '#666'"></uv-icon>
          </view>
          <view class="item_dropDown" @tap="openPickerStatus">
            <uv-text :lines="1" :text="storageStatusText" :color="storageStatusText != '全部' ? '#3c9cff' : ''"
              class="item_text"></uv-text>
            <uv-icon name="arrow-down-fill" :color="storageStatusText != '全部' ? '#3c9cff' : '#666'"></uv-icon>
          </view>
        </view>
      </template>

      <view class="box">
        <view v-for="item in localListData" class="item" @tap="itemDetail(item)">
          <view class="title">
            <view class="sign" v-show="item.execStatus == 0">{{ item.execStatus == 0 ? '预' : '已' }}</view>
            单据号：{{ item.billNo }}
            <!-- <uv-icon name="file-text" size="36rpx" style="margin-top: 4rpx;" @tap="itemCopy(item.billNo)"></uv-icon> -->
          </view>
          <!-- <view class="title" v-if="props.pickerCode == 'ckType'">单据号：{{ item.billNoCk }}<uv-icon name="file-text"
              size="36rpx" style="margin-top: 4rpx;" @tap="itemCopy(item.billNoCk)"></uv-icon></view> -->
          <!-- <view><text>订单号：</text>{{ item.sapNo }}</view> -->
          <view>
            <text>{{ props.pickerCode == 'rkType' ? '入库类型' : '出库类型' }}</text>：
            {{ item.operationTypeName }}
          </view>
          <view v-if="props.pickerCode == 'rkType'"><text>物资类型：</text>{{ item.wlTypeName }}</view>
          <view><text>理货员：</text>{{ item.operatorName }}</view>
          <view v-if="props.pickerCode == 'ckType'"><text>施工队：</text>{{ item.teamName }}</view>
          <view><text>{{ props.pickerCode == 'rkType' ? '入库时间' : '出库时间' }}：</text>{{ item.operationTime }}</view>
          <!-- <view v-show="item.showMore">
            <view><text>物料号：</text>{{ item.wlNo }}</view>
            <view><text>物料描述：</text>{{ item.wlMs }}</view>
            <view><text>数量：</text>{{ item.realQty }}{{ item.dw }}</view>
            <view><text>项目定义号：</text>{{ item.xmNo }}</view>
            <view><text>项目描述：</text>{{ item.xmMs }}</view>
            <view><text>供应商：</text>{{ item.gysMc }}</view>
            <view><text>存放位置：</text>{{ item.pcode || "-" }}</view>
            <view><text>身份码：</text>{{ item.entityId || "-" }}</view>
            <view v-if="props.pickerCode == 'rkType'"><text>备注：</text>{{ item.remark || "-" }}</view>
            <view v-if="props.pickerCode == 'ckType'"><text>备注：</text>{{ item.ckRemark || "-" }}</view>
          </view>
          <view class="more" v-show="!item.showMore" @tap="changeMore(item)">
            <view>详细信息</view>
            <uv-icon name="arrow-down" color="#A4A6A7" size="28rpx" style="margin-left: 10rpx;"></uv-icon>
          </view>
          <view class="more" v-show="item.showMore" @tap="changeMore(item)">
            <view>收起</view>
            <uv-icon name="arrow-up" color="#A4A6A7" size="28rpx" style="margin-left: 10rpx;"></uv-icon>
          </view> -->
        </view>
      </view>

      <view class="btn">
        <!-- <uv-button type="error" text="确定" size="large" style="width: 48%;" @tap="openModal">{{ props.deleteText
          }}</uv-button> -->
        <uv-button type="primary" text="确定" size="large" class="mainBtn" style="width: 100%;" @tap="handleAdd">{{
          props.GoText
          }}</uv-button>
      </view>
    </z-paging>

    <uv-picker ref="picker" :columns="storageType" keyName="typeName" activeColor="#3c9cff"
      @confirm="pickerConfirm"></uv-picker>
    <uv-picker ref="pickerStatus" :columns="storageStatus" keyName="label" activeColor="#3c9cff"
      @confirm="pickerConfirmStatus"></uv-picker>

    <uv-modal ref="storageModalRef" title="单据号" @confirm="handleRemove" :showCancelButton="true"
      :closeOnClickOverlay="false" asyncClose>
      <view class="slot-content" style="width: 100%;">
        <uv-form labelPosition="left" :model="storageForm" labelWidth="80" :rules="storageRules" ref="storageFormRef">
          <uv-form-item label="单据号" prop="billNo" borderBottom>
            <uv-input v-model="storageForm.billNo" border="none" placeholder="请输入单据号" />
          </uv-form-item>
        </uv-form>
      </view>
    </uv-modal>
  </view>
</template>

<script setup>
import { ref, watch } from 'vue';

// Props定义
const props = defineProps({
  listData: {
    type: Array,
    default: () => []
  },
  storageType: {
    type: Array,
    default: () => []
  },
  storageStatus: {
    type: Array,
    default: () => []
  },
  pickerCode: {
    type: String,
    default: "rkType"
  },
  deleteText: {
    type: String,
    default: "打 开"
  },
  GoText: {
    type: String,
    default: "下一步"
  },
  queryByParams: {
    type: Object,
    default: () => { }
  }
});

// Emits定义
const emit = defineEmits(['update:listData', 'remove', 'add', 'reload']);

// 本地数据
const localListData = ref([]);

const queryParams = ref(props.queryByParams);
queryParams.value[props.pickerCode] = "";

// 组件引用
const pagingRef = ref(null);
const storageModalRef = ref(null);
const storageFormRef = ref(null);

// 表单数据
const storageForm = ref({
  billNo: '',
});
const storageRules = ref({
  'billNo': {
    type: 'string',
    required: true,
    message: '请填写单据号',
    trigger: ['blur', 'change']
  },
});

// 监听props变化
watch(() => props.listData, (newVal) => {
  // localListData.value = newVal;
  pagingRef.value.complete(newVal)
}, { deep: true });

// 方法定义
const handleQuery = (pageNo, pageSize) => {
  emit('reload', { ...queryParams.value, pageNum: pageNo, pageSize });
};

const handleSearch = () => {
  pagingRef.value.reload();
};

const picker = ref(null);
const storageTypeText = ref("全部");
const openPicker = () => {
  picker.value.open();
};
const pickerConfirm = (e) => {
  queryParams.value.operationType = e.value[0].typeCode;
  storageTypeText.value = e.value[0].typeName;
  pagingRef.value.reload();
};

const pickerStatus = ref(null);
const storageStatusText = ref("全部");
const openPickerStatus = () => {
  pickerStatus.value.open();
};
const pickerConfirmStatus = (e) => {
  queryParams.value.execStatus = e.value[0].value;
  storageStatusText.value = e.value[0].label;
  pagingRef.value.reload();
};

const itemCopy = (id) => {
  uni.setClipboardData({
    data: id,
    success: function () { }
  });
};

const changeMore = (item) => {
  item.showMore = !item.showMore;
};

const openModal = () => {
  storageForm.value.billNo = ""
  storageModalRef.value.open();
};

const itemDetail = (item) => {
  uni.navigateTo({
    url: '/pagesStorage/storageDetail?type=' + props.pickerCode,
    success: function(res) {
      // 通过eventChannel向被打开页面传送数据
      res.eventChannel.emit('acceptDataFrom', { data: item })
    }
  })
}

const handleRemove = () => {
  storageFormRef.value.validate().then(res => {
    emit('remove', storageForm.value.billNo);
    storageModalRef.value.close();
  }).catch(errors => {
    storageModalRef.value.closeLoading();
  });
};

const handleAdd = () => {
  emit('add');
};
</script>

<style scoped lang="scss">
.container {
  position: relative;

  .containerBox {
    padding: 32rpx;
    padding-bottom: 120rpx;

    .dropDown {
      // padding: 0 30rpx;
      border-bottom: 1px solid #dadbde;
      display: flex;

      .item_dropDown {
        padding: 20rpx;
        display: flex;
        align-items: center;
        width: 140rpx;

        ::v-deep .uv-text {
          width: unset !important;
          flex: unset !important;
        }

        ::v-deep .item_text {
          max-width: 200rpx;
          margin-right: 10rpx !important;
        }
      }
    }

    ::v-deep .uv-search {
      flex: unset;
    }

    .box {

      // background-color: #ebebeb;
      // background-color: #f5f5f5;
      // padding-top: 20rpx;
      .item {
        background: #efefef;
        padding: 20rpx;
        margin-top: 20rpx;
        line-height: 50rpx;
        font-size: 28rpx;
        border-radius: 8rpx;
        color: #333;
        position: relative;

        .title {
          font-size: 32rpx;
          font-weight: bold;
          display: flex;
          align-items: center;
        }

        .sign {
          width: 40rpx;
          height: 40rpx;
          line-height: 40rpx;
          text-align: center;
          border-radius: 40rpx;
          color: #fff;
          background: #FF3800;
          font-size: 24rpx;
          font-weight: 400;
          margin-right: 10rpx;
        }

        text {
          color: #666;
        }

        .more {
          color: #A4A6A7;
          display: flex;
          align-items: center;
        }
      }
    }

    .btn {
      position: fixed;
      width: calc(100vw - 64rpx);
      bottom: 0;
      left: 32rpx;
      z-index: 99;
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
  }

  ::v-deep .uv-input__content {
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>