<template>
    <basic-container>
      <avue-crud ref="crud" :data="dataList" :option="option" v-model="form" :search.sync="search" :table-loading="loading"
        @saveColumn="saveColumn" @resetColumn="resetColumn" @search-change="searchChange" @current-change="currentChange"
        @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
        <template slot="billno" slot-scope="{ row, index }">
          <span style="color: #409EFF;cursor: pointer" @click.stop="beforeOpenPage(row, index)">{{ row.billno }}</span>
        </template>
      </avue-crud>
    </basic-container>
  </template>
  
  <script>
  import option from "./config/detail.json";
  import LTdetail from './config/LTdetail.json'
  import {AppPartsStockDataDetails, stockDataDetails} from "@/api/basicData/inventoryAccount";
  
  export default {
    name: "detail",
    data() {
      return {
        option: {},
        dataList: [],
        form: {},
        page: {
          pageSize: 10,
          pagerCount: 5,
          total: 0,
        },
        search: {},
        loading: false,
        params: null,
      }
    },
    async created() {
      // this.option = option
      console.log('打开了');
      if (JSON.parse(localStorage.getItem('saber-tenantId')).content == 883868){
          this.option = LTdetail;
      }else {
          this.option = await this.getColumnData(this.getColumnName(279), option);
      }
      this.$store.commit("DOMAS_IN_DETAIL");
    },
    activated() {
      this.$store.commit("DOMAS_IN_DETAIL");
      if (this.$route.query.type == 'JL'){
          this.params = {
              goodsId: this.$route.query.goodsId,
              storageId: this.$route.query.stockId
          }
      }else {
          this.params = {
              goodsId: this.$route.query.goodsId,
              stockId: this.$route.query.stockId,
              dot:this.$route.query.dot
          }
      }
        this.onLoad(this.page)
    },
    methods: {
      onLoad(page, params) {
        if (!this.params) return
        params = { ...params, ...this.params }
        this.dataList.forEach(item => {
          this.$refs.crud.toggleRowExpansion(item, false)
        })
        this.loading = true;
        if (this.$route.query.type == 'JL') {
            AppPartsStockDataDetails(params).then(res=>{
                console.log(res)
                this.dataList = res.data.data ? res.data.data : [];
                this.page.total = res.data.data.total;
                if (this.page.total) {
                    this.option.height = window.innerHeight - 260;
                }
            }).finally(() => {
                this.loading = false;
            });
        }else {
            stockDataDetails(params)
                .then(res => {
                    console.log('res', res);
                    this.dataList = res.data.data ? res.data.data : [];
                    this.page.total = res.data.data.total;
                    if (this.page.total) {
                        this.option.height = window.innerHeight - 260;
                    }
                })
                .finally(() => {
                    this.loading = false;
                });
        }
      },
      searchChange(params, done) {
        this.onLoad(this.page, params);
        done();
      },
      currentChange(val) {
        this.page.currentPage = val;
      },
      sizeChange(val) {
        this.page.currentPage = 1;
        this.page.pageSize = val;
      },
      refreshChange() {
        this.dataList.forEach(item => {
          this.$refs.crud.toggleRowExpansion(item, false)
        })
        this.page.currentPage = 1;
        this.onLoad(this.page, this.search);
      },
      async saveColumn() {
        const inSave = await this.saveColumnData(
          this.getColumnName(279),
          this.option
        );
        if (inSave) {
          this.$message.success("保存成功");
          //关闭窗口
          this.$refs.crud.$refs.dialogColumn.columnBox = false;
        }
      },
      async resetColumn() {
        this.option = option;
        const inSave = await this.delColumnData(
          this.getColumnName(279),
          option
        );
        if (inSave) {
          this.$message.success("重置成功");
          //关闭窗口
          this.$refs.crud.$refs.dialogColumn.columnBox = false;
        }
      },
      // 跳转
      beforeOpenPage(row, index) {
          if(row.bizTypeName == '盘点') {
              console.log(this.$store.getters,139)
              if (this.$store.getters.Inventory) {
                  this.$alert("明细账已存在,请保存关闭明细账再进行操作", "温馨提示", {
                      confirmButtonText: "确定",
                      type: "warning",
                      callback: action => {
                          console.log(action);
                      }
                  });
              }else {
                  this.$router.$avueRouter.closeTag("/tirePartsMall/salesManagement/Inventory/index");
                  this.$router.push({
                      path: "/tirePartsMall/salesManagement/Inventory/index",
                      query: {
                          orderId: row.id
                      },
                  });
              }
          }else {
              if (this.$store.getters.inboundAndOutbound) {
                  this.$alert("明细账已存在,请保存关闭明细账再进行操作", "温馨提示", {
                      confirmButtonText: "确定",
                      type: "warning",
                      callback: action => {
                          console.log(action);
                      }
                  });
              } else {
                  if (row.bizTypeName == '出库') {
                      this.$router.$avueRouter.closeTag("/tirePartsMall/salesManagement/outboundWorkOrder/index");
                      this.$router.push({
                          path: "/tirePartsMall/salesManagement/outboundWorkOrder/index",
                          query: {
                              id: row.id
                          },
                      });
                  }else if (row.bizTypeName == '销售') {
                      this.$router.$avueRouter.closeTag("/salesOrder/index");
                      this.$router.push({
                          path: "/salesOrder/index",
                          query: {
                              orderId: row.id
                          },
                      });
                  } else if (row.bizTypeName == '采购') {
                      this.$router.$avueRouter.closeTag("/purchaseOrder/index");
                      this.$router.push({
                          path: "/purchaseOrder/index",
                          query: {
                              orderId: row.id
                          },
                      });
                  } else if (row.bizTypeName == '调出') {
                      if (this.$store.getters.Allocateandtransfer) {
                          this.$alert("调拨明细已存在,请保存关闭明细账再进行操作", "温馨提示", {
                              confirmButtonText: "确定",
                              type: "warning",
                              callback: action => {
                                  console.log(action);
                              }
                          });
                      }else {
                          this.$router.$avueRouter.closeTag("/tirePartsMall/salesManagement/outboundWorkOrderL/index");
                          this.$router.push({
                              path: "/tirePartsMall/salesManagement/outboundWorkOrderL/index",
                              query: {
                                  id: row.id
                              },
                          });
                      }
                  } else {
                      this.$router.$avueRouter.closeTag("/tirePartsMall/purchasingManagement/warehouseEntryOrder/index");
                      this.$router.push({
                          path: "/tirePartsMall/purchasingManagement/warehouseEntryOrder/index",
                          query: {
                              id: row.id
                          },
                      });
                  }
              }
          }
      },
    },
  }
  </script>
  
  <style scoped></style>
  