<template>
  <view class="content">
    <view class="title">
      <view class="">
        <text>库区筛选</text>
      </view>
      <view class="view_icon" @click="inputDialogToggle">
        <text>{{ value }}</text>
        <u-icon name="arrow-right" color="#000000" size="20"></u-icon>
      </view>
    </view>
    <view class="content_table">
      <view class="uni-container">
        <uni-table
          ref="table"
          :loading="loading"
          stripe
          emptyText="暂无更多数据"
          @selection-change="selectionChange">
          <uni-tr>
            <uni-th width="50" align="center">库位</uni-th>
            <uni-th width="50" align="center"></uni-th>
            <uni-th width="50" align="center">库位状态</uni-th>
            <uni-th width="50" align="center">库存状态</uni-th>
          </uni-tr>
          <uni-tr
            @filter-change="filtechange"
            v-for="(item, index) in tableData"
            :key="index">
            <uni-td>
              <view class="name" @click.prevent="ferterMan(item)">
                {{ item.locationNo }}
              </view>
            </uni-td>
            <uni-td>
              <view class="name" @click.prevent="ferterMan(item)">
                {{ item.name }}
              </view>
            </uni-td>
            <uni-td align="center">
              <view class="name" @click.prevent="ferterMan(item)">
                {{ item.stockStatus }}
              </view>
            </uni-td>
            <uni-td align="center">
              <view
                class="name"
                @click.prevent="ferterMan(item)"
                v-if="item.isEmpty == 'Y'">
                空
              </view>
              <view
                class="name"
                @click.prevent="ferterMan(item)"
                v-if="item.isEmpty == 'N'">
                非空
              </view>
            </uni-td>
          </uni-tr>
        </uni-table>
        <view class="uni-pagination-box">
          <uni-pagination
            show-icon
            :page-size="pageSize"
            :current="pageCurrent"
            :total="total"
            @change="change" />
        </view>
      </view>
    </view>
    <uni-popup ref="inputDialog" type="dialog">
      <uni-popup-dialog
        ref="inputClose"
        title="选择库区"
        value="对话框预置提示内容!"
        placeholder="请输入内容"
        @confirm="dialogInputConfirm">
        <u-radio-group
          placement="column"
          :borderBottom="true"
          v-model="value"
          iconPlacement="right">
          <u-radio
            :customStyle="{ marginBottom: '8px' }"
            v-for="(item, index) in radiolist1"
            :key="index"
            :label="item.name"
            :name="item.name"
            @change="radioChange($event, item)"></u-radio>
        </u-radio-group>
      </uni-popup-dialog>
    </uni-popup>
  </view>
</template>

<script>
import tableData from "../tableData.js";
export default {
  data() {
    return {
      type: "center",
      msgType: "success",
      messageText: "这是一条成功提示",
      value: "请选择",
      value1: "",
      value3: "",
      searchVal: "",
      tableData: [],
      IDBIndexs: "",
      pageSize: 10,
      host: "",
      pageCurrent: 1,
      total: 0,
      loading: true,
      rangeValue: "",
      zoneId: "",
      tip: 0,
      radiolist1: [
        {
          name: "SE车检",
          disabled: false,
        },
        {
          name: "成品区",
          disabled: false,
        },
      ],
    };
  },
  onLoad(options) {
    console.log("进", options);
    let _this = this;
    uni.getStorage({
      key: "hostappst",
      success(res) {
        console.log("获取成功", res.data);
        _this.host = res.data;
      },
    });
    this.getData(1);
    this.getReservoir();
    this.IDBIndexs = options.show;
    if (options.rangeValue != "") {
      this.rangeValue = options.rangeValue;
    }
    this.tip = options.tip;

    this.selectedIndexs = [];

    this.radiolist1 = [];
  },
  mounted() {
    if (this.tip == 1) {
      this.value = "桶装物料入库缓存区";
      this.dialogInputConfirm();
    } else if (this.tip == 2) {
      this.value = "桶装物料出库缓存区";
      this.dialogInputConfirm();
    } else if (this.rangeValue == "emptyPalletTargetOut") {
      this.value = "桶装物料入库缓存区";
      this.dialogInputConfirm();
    } else {
      this.inputDialogToggle();
    }
    // || this.IDBIndexs == 4
    if (this.IDBIndexs == 1 || this.IDBIndexs == 2) {
      this.inputDialogToggle();
    }
  },
  methods: {
    selectedItems() {
      return this.selectedIndexs.map((i) => this.tableData[i]);
    },

    selectionChange(e) {
      console.log(e.detail.index);
      this.selectedIndexs = e.detail.index;
    },
    ferterMan(val) {
      console.log("val", val);
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      let object = {
        id: val.id,
        locationNo: val.locationNo,
        show: this.IDBIndexs,
      };
      prevPage.$vm.otherFun(object);
      uni.navigateBack({
        delta: 1,
      });
    },

    delTable() {
      console.log(this.selectedItems());
    },
    filtechange(e) {
      console.log("e", e);
    },

    change(e) {
      this.$refs.table.clearSelection();
      this.selectedIndexs.length = 0;
      this.getData(e.current);
    },

    search() {
      this.getData(1, this.searchVal);
    },

    getData(pageCurrent, value = "") {
      this.loading = true;
      this.pageCurrent = pageCurrent;
      this.request({
        pageSize: this.pageSize,
        pageCurrent: pageCurrent,
        value: value,
        success: (res) => {},
      });
    },
    getReservoir() {
      let that = this;
      uni.getStorage({
        key: "GetPhone_Token",
        success: function (res) {
          uni.request({
            url: `http://${that.host}` + "/inv/asn/getZoneList",
            header: {
              Authorization: res.data,
            },
            method: "GET",
            success(res) {
              console.log("chaoShiPanDuan", res);
              if (res.data.code == 200) {
                res.data.data.forEach((item, index) => {
                  that.radiolist1.push({
                    name: item.zoneName,
                    id: item.zoneId,
                    disabled: false,
                  });
                });
              } else {
              }
              console.log("res", res);
            },
          });
        },
      });
    },
    request(options) {
      const { pageSize, pageCurrent, success, value } = options;
      let total = tableData.length;
      let data = tableData.filter((item, index) => {
        const idx = index - (pageCurrent - 1) * pageSize;
        return idx < pageSize && idx >= 0;
      });
      if (value) {
        data = [];
        tableData.forEach((item) => {
          if (item.name.indexOf(value) !== -1) {
            data.push(item);
          }
        });
        total = data.length;
      }

      setTimeout(() => {
        typeof success === "function" &&
          success({
            data: data,
            total: total,
          });
      }, 500);
    },
    goto(data) {
      uni.navigateTo({
        url: data,
      });
    },
    inputDialogToggle() {
      // || this.IDBIndexs == 4
      if (this.IDBIndexs == 1 || this.IDBIndexs == 2) {
        this.$refs.inputDialog.open();
      }
    },
    radioChange(val, row) {
      console.log("111", val, row);
      this.value3 = val;
      this.zoneId = row.id;
    },
    dialogInputConfirm(val) {
      if (this.value3 != "") {
        this.value = this.value3;
      }
      this.tableData = [];
      console.log("val", this.zoneId);
      let that = this;
      let data = {
        zoneId: that.zoneId,
        locationSceneCode: "emptyPalletInvList",
      };
      let dataobj = {};
      let name;
      if (this.IDBIndexs == 3 || this.IDBIndexs == 6 || this.IDBIndexs == 7) {
        name = "getCacheLocationInfoCacheList";
        if (this.tip == 1) {
          dataobj = {
            cacheZoneSceneCode: "bucketCacheZoneIn",
            locationSceneCode: "bucketCacheIn",
          };
        } else {
          dataobj = {
            cacheZoneSceneCode: "bucketCacheZoneOut",
            locationSceneCode: "bucketCacheOut",
          };
        }
      } else if (this.IDBIndexs == 1 || this.IDBIndexs == 2) {
        name = "getBaseLocationInfoList";
        if (this.zoneId == 4) {
          dataobj = {
            zoneId: 4,
          };
        } else {
          dataobj = {
            zoneId: 5,
          };
        }
      } else {
        name = "getBaseLocationInfoList";
        if (this.zoneId == 4 || this.IDBIndexs == 4) {
          dataobj = {
            zoneId: 4,
            locationSceneCode: this.rangeValue,
          };
        } else {
          dataobj = {
            zoneId: 5,
            locationSceneCode: this.rangeValue,
          };
        }
      }

      this.loading = false;
      uni.getStorage({
        key: "GetPhone_Token",
        success: function (res) {
          // 恢复请求
          uni.request({
            url: `http://${that.host}` + "/inv/asn/" + name,
            header: {
              Authorization: res.data,
            },
            method: "GET",
            data: dataobj,
            success(res) {
              console.log("chaoShiPanDuan", res);
              if (res.data.code == 200) {
                res.data.data.forEach((item, index) => {
                  res.data.data.forEach((item, index) => {
                    if (item.stockStatus == "00") {
                      item.stockStatus = "空闲";
                    } else if (item.stockStatus == "10") {
                      item.stockStatus = "占用";
                    } else if (item.stockStatus == "20") {
                      item.stockStatus = "完成";
                    } else if (item.stockStatus == "90") {
                      item.stockStatus = "合格";
                    } else if (item.stockStatus == "99") {
                      item.stockStatus = "不合格";
                    } else if (item.stockStatus == "80") {
                      item.stockStatus = "封存";
                    }
                  });

                  that.tableData = [...res.data.data];
                  that.total = res.data.total;
                });
              } else {
              }
              console.log("res", res);
            },
          });
        },
      });

      setTimeout(() => {
        uni.hideLoading();
        console.log(val);

        // 关闭窗口后，恢复默认内容
        this.$refs.inputDialog.close();
      }, 100);
    },
  },
};
</script>

<style scoped lang="scss">
.content {
  padding: 15px;
  .title {
    display: flex;
    justify-content: space-between;
    font-size: 17px;
    color: #525151;
    .view_icon {
      display: flex;
      align-content: center;
      align-items: center;
    }
  }
  .content_table {
    padding-top: 20px;
  }
}
</style>
