<template>
  <basic-container>
    <avue-crud
      ref="crud"
      :data="dataList"
      :option="option"
      v-model="form"
      :page.sync="page"
      :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="orderNo" slot-scope="{ row, index }">
        <span style="color: #409EFF;cursor: pointer" @click.stop="beforeOpenPage(row, index)">{{ row.orderNo }}</span>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import option from "./configuration/detail.json";
import {getStock} 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
    this.option = await this.getColumnData(this.getColumnName(67), option);
    this.$store.commit("DOMKC_IN_DETAIL");
  },
  activated() {
    this.params = {
      corpId: this.$route.query.corpId,
      itemId: this.$route.query.itemId,
      storageId:this.$route.query.storageId
    }
    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;
      getStock(page.currentPage, page.pageSize, params)
        .then(res => {
          this.dataList = res.data.data.records ? res.data.data.records : [];
          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(67),
        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(67),
        option
      );
      if (inSave) {
        this.$message.success("重置成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    // 跳转
    beforeOpenPage(row,index) {
      if (this.$store.getters.domSaleStatus) {
        this.$alert("销售单存在,请保存关闭销售单再进行操作", "温馨提示", {
          confirmButtonText: "确定",
          type: "warning",
          callback: action => {
            console.log(action);
          }
        });
      } else {
        this.$router.$avueRouter.closeTag("/businessManagement/salesOrder/index");
        this.$router.push({
          path: "/businessManagement/salesOrder/index",
          query: {
            id: row.id
          },
        });
      }
    },
  },
}
</script>

<style scoped>

</style>
