<template>
  <div class="app-container">
    <div class="head-container">
      <el-form :model="query" inline>
        <el-form-item>
          <el-input v-model="query.conName" placeholder="耗材信息名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getTableData">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="head-container">
      <el-table
        ref="table"
        :data="clickseetableData.data"
        v-loading="clickseetableData.loading"
        row-key="consId"
        :expand-row-keys="expands"
        @expand-change="expandChange"
        height="66vh"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-table 
             ref="recodersTable"
            :data="props.row.recoders" row-key="id" height="66vh">
              <el-table-column label="耗材名称" prop="conName" />
              <el-table-column label="变动库存" prop="amount">
                <template v-slot="scope">
                  <span :class="{ red: scope.row.bizType == '-' }"
                    >{{ scope.row.bizType }}{{ scope.row.amount }}</span
                  >
                </template>
              </el-table-column>
              <el-table-column label="现有库存" prop="balance" />
              <el-table-column label="业务说明" prop="bizName" />
              <el-table-column label="商品名称" prop="productName">
                <template v-slot="scope">
                  {{ scope.row.productName }}
                </template>
              </el-table-column>
              <el-table-column
                label="创建时间"
                prop="createTime"
              ></el-table-column>
            </el-table>

            <div class="head-container">
              <el-pagination
                :total="props.row.total"
                :current-page="props.row.page + 1"
                :page-size="props.row.size"
                layout="total, sizes, prev, pager, next, jumper"
                @current-change="recodersPageChange($event, props.row)"
                @size-change="recodersSizeChange($event, props.row)"
              />
            </div>
          </template>
        </el-table-column>

        <el-table-column label="耗材名称" prop="conName" />
        <el-table-column label="变动库存" prop="amount">
          <template v-slot="scope">
            <span :class="{ red: scope.row.bizType == '-' }"
              >{{ scope.row.bizType }}{{ scope.row.amount }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="现有库存" prop="balance" />
        <!-- <el-table-column label="业务编码" prop="bizCode" /> -->
        <el-table-column label="业务说明" prop="bizName" />
        <el-table-column label="商品名称" prop="productName" />
        <!-- <el-table-column label="正负号标识" prop="bizType" /> -->
        <!-- <el-table-column label="耗材id" prop="consId" /> -->
        <el-table-column label="创建时间" prop="createTime"></el-table-column>
        <!-- <el-table-column label="更新时间" prop="updateTime">
          <template v-slot="scope">
            {{ dayjs(scope.row.createdAt).format('YYYY-MM-DD HH:mm:ss') }}
          </template>
        </el-table-column> -->
      </el-table>
    </div>
    <div class="head-container">
      <el-pagination
        :total="clickseetableData.total"
        :current-page="clickseetableData.page + 1"
        :page-size="clickseetableData.size"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="paginationChangetype"
        @size-change="pageSizeChange "
      />
    </div>
  </div>
</template>

<script>
import { gettbConsInfo } from "@/api/consumable";
export default {
  data() {
    return {
      expands:[],
      query: {
        conName: "",
      },
      clickseetableData: {
        data: [],
        page: 0,
        size: 30,
        loading: false,
        total: 0,
      },
    };
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    resetRecodetsTable(){
      this.expands=[];
    },
    recodersPageChange(page,row) {
      // this.clickseetableData.page = e - 1;
      row.page=page-1;
      this.getGroupConInfoFlow(row);
    },
    recodersSizeChange(size,row){
      const index = this.clickseetableData.data.findIndex(
          (v) => v.consId == row.consId
        );
        if(index!=-1){
          row.size=size;
          row.page=0;
        }
      this.getGroupConInfoFlow(row);
    },
    // 获取分组内的耗材具体记录
    async getGroupConInfoFlow(row) {
      try {
        row.loading = true;
        const res = await gettbConsInfo({
          page: row.page,
          size: row.size,
          consId: row.consId,
          conName: row.conName,
          shopId: localStorage.getItem("shopId"),
        });
        row.loading = false;
        const index = this.clickseetableData.data.findIndex(
          (v) => v.consId == row.consId
        );
        if (index != -1) {
          this.$set(this.clickseetableData.data, index, {
            ...this.clickseetableData.data[index],
            recoders: res.content,
            total: res.totalElements,
          });
        }
        // const res = await gettbConsInfo({
        //   page: this.clickseetableData.page,
        //   size: this.clickseetableData.size,
        //   consId: "",
        //   conName: this.query.conName,
        //   shopId: localStorage.getItem("shopId")
        // });
        console.log(res);
        this.clickseetableData.loading = false;
      } catch (error) {
        console.log(error);
      }
    },
    expandChange(row, expandedRows) {
      //展开
      if (expandedRows.length) {
        this.expands=[];
        if(row){
          this.expands.push(row.consId);
        }
        this.getGroupConInfoFlow(row);
      } else {
        this.expands=[];

      }
    },
    resetHandle() {
      this.clickseetableData.page = 0;
      this.clickseetableData.size = 30;
      this.query.conName = "";
      this.getTableData();
      this.resetRecodetsTable()

    },
    pageSizeChange(e) {
      this.clickseetableData.size = e;
      this.clickseetableData.page = 0;
      this.resetRecodetsTable()
      this.getTableData();
    },
    paginationChangetype(e) {
      this.clickseetableData.page = e - 1;
      this.resetRecodetsTable()
      this.getTableData();
    },
    // 查看查询耗材流水信息
    async getTableData() {
      try {
        this.clickseetableData.loading = true;
        const res = await gettbConsInfo({
          page: this.clickseetableData.page,
          size: this.clickseetableData.size,
          consId: "",
          conName: this.query.conName,
          shopId: localStorage.getItem("shopId"),
        });
        // const res = await gettbConsInfo({
        //   page: this.clickseetableData.page,
        //   size: this.clickseetableData.size,
        //   consId: "",
        //   conName: this.query.conName,
        //   shopId: localStorage.getItem("shopId")
        // });
        console.log(res);
        this.clickseetableData.loading = false;
        this.clickseetableData.data = res.content.map((v) => {
          return { ...v, recoders: [], total: 0,
              page: 0,
              size: 30,
              loading: false
           };
        });
        this.clickseetableData.total = res.totalElements;
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>
