<template>
  <div>
    <div class="common-card">
      <h3>基本信息</h3>
      <el-form
        :model="tempInfo"
        ref="queryForm"
        label-width="237px"
        class="grid-form-class"
      >
        <div class="form-body">
          <div class="flex-css">
            <el-form-item label="采购单号">
              <div class="content">
                {{ $route.query.purcOrdId }}
              </div>
            </el-form-item>
            <el-form-item label="配送单号">
              <div class="content">
                {{ $route.query.delvOrdId }}
              </div>
            </el-form-item>
          </div>
          <div class="flex-css">
            <el-form-item label="供应商">
              <div class="content">
                {{ $route.query.splerName }}
              </div>
            </el-form-item>
            <el-form-item label="单据状态">
              <div class="content">
                <span v-if="$route.query.purcOrdStas == 'A'">未出库</span>
                <span v-if="$route.query.purcOrdStas == 'C'">已出库</span>
                <span v-if="$route.query.purcOrdStas == 'R'">已验收</span>
                <span v-if="$route.query.purcOrdStas == 'Y'">结束订单</span>
              </div>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>

    <!--列表-->
    <div class="common-card">
      <h3>明细信息</h3>
      <div class="right-type">
        <el-button type="success" icon="el-icon-box" @click="checkAccept()"
          >验收
        </el-button>
      </div>
      <el-form :model="listForm" ref="listForm" class="grid-form-class">
        <el-table
          id="listId"
          v-loading="listLoading"
          element-loading-text="加载中"
          :data="listForm.list"
          fit
          border
          :span-method="objectSpanMethod"
        >
          <template slot="empty">
            <img src="@/assets/nopage.png" alt />
            <p>暂无数据</p>
          </template>
          <el-table-column
            fixed
            type="index"
            label="序号"
            width="60"
          ></el-table-column>
          <el-table-column label="药品编码" prop="drugListId" width="150" />
          <el-table-column label="药品名称" min-width="300">
            <template slot-scope="scope">
              <div class="drug-name">
                <el-tag v-if="scope.row.drugName">
                  {{ scope.row.drugName }}
                </el-tag>
                {{ "【" + scope.row.dosformName + "】" }}
                <el-tag v-if="scope.row.essdrugType">
                  {{
                    scope.row.essdrugType === "0"
                      ? "非基药"
                      : scope.row.essdrugType === "1"
                      ? "基药"
                      : scope.row.essdrugType
                  }}
                </el-tag>
                <br />
                {{ "生产厂家 【" + scope.row.prodentpName + "】" }}
                <br />
                {{
                  "药品规格 【" +
                  scope.row.specName +
                  "*" +
                  scope.row.convrat +
                  scope.row.prepunt +
                  "/" +
                  scope.row.pacUnt +
                  "】"
                }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="批准文号" prop="aprvNo" width="150" />
          <el-table-column label="批号" prop="lotNum" width="150" />
          <el-table-column label="订货数量" prop="ordCnt" width="150" />
          <el-table-column label="到货数量" prop="delvCnt" width="150" />
          <el-table-column label="收货数量" width="150">
            <template #header>
              <span style="color: red; margin-right: 4px">*</span>
              <span>收货数量</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                label-width="0"
                :prop="'list.' + scope.$index + '.shppCnt'"
                :rules="rules.natureNum"
              >
                <el-input
                  @change="blurChange(scope.$index, scope.row)"
                  v-model="scope.row.shppCnt"
                  clearable
                >
                </el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column label="生产日期" prop="manuDate" width="150" />
          <el-table-column label="有效期至" prop="prodExpy" width="150" />
          <el-table-column label="采购价" prop="delvPric" width="150" />
           <el-table-column label="采购总价" prop="delvAmt" width="150" />
          <el-table-column label="收货总价" prop="delvPrics" width="150" />
          <el-table-column label="发票" width="100" prop="invoFileId">
            <template slot-scope="scope">
              <yl-upload
                :fileId="scope.row.invoFileId"
                readonly
                v-if="scope.row.invoFileId"
              >
              </yl-upload>
            </template>
          </el-table-column>
          <el-table-column label="随货同行单" width="100" prop="dyntFileId">
            <template slot-scope="scope">
              <yl-upload
                :fileId="scope.row.dyntFileId"
                readonly
                v-if="scope.row.dyntFileId"
              >
              </yl-upload>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
    <div class="foot-button">
      <el-button type="info" @click="goback">返回</el-button>
    </div>
  </div>
</template>

<script>
import rules from "@/utils/rules";
import ylUpload from "@/components/yl-upload";
import { getDetlList, saveStoIn } from "@/api/storeroom/check-warehouse";
import { countNumPrice, getTotalAmount } from "@/utils/utils";
export default {
  components: { ylUpload },
  data() {
    return {
      rules,
      listQuery: {
        //条件查询
        current: 1,
        size: 10
      },
      total: 0, //分页
      listForm: {
        list: []
      },

      tempInfo: {},
      cellList: []
    };
  },

  created() {
    this.getData();
  },

  methods: {
    numOrPriceChange(index, row) {
      this.$set(
        this.listForm.list[index],
        "delvPrics",
        getTotalAmount(row.shppCnt, row.delvPric)
      );
    },

    blurChange(index, row) {
      if (row.shppCnt > row.delvCnt) {
        this.$message({
          message: "收货数量不能大于到货数量",
          type: "warning"
        });
        row.shppCnt = row.delvCnt;
        this.numOrPriceChange(index, row);
        return false;
      } else {
        this.numOrPriceChange(index, row);
      }
    },

    goback() {
      this.$router.go(-1);
    },

    // 筛选合并项
    computeCell(tableData) {
      this.cellList = [];
      this.count = null;
      for (let i = 0; i < tableData.length; i++) {
        if (i == 0) {
          // 先设置第一项
          this.cellList.push(1); // 初为1，若下一项和此项相同，就往cellList数组中追加0
          this.count = 0;
        } else {
          // 判断当前项与上项的设备类别是否相同，因为是合并这一列的单元格
          if (tableData[i].lotNum == tableData[i - 1].lotNum) {
            this.cellList[this.count] += 1; // 增加计数
            this.cellList.push(0); // 相等就往cellList数组中追加0
          } else {
            this.cellList.push(1); // 不等就往cellList数组中追加1
            this.count = i; // 将索引赋值为计数
          }
        }
      }
    },

    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (
        columnIndex === 0 ||
        columnIndex === 1 ||
        columnIndex === 2 ||
        columnIndex === 3 ||
        columnIndex === 5 ||
        columnIndex === 10
      ) {
        // console.log("单元格数组，若下一项为0，则代表合并上一项", this.cellList);
        const rowCell = this.cellList[rowIndex];
        if (rowCell > 0) {
          const colCell = 1;
          // console.log(`动态竖向合并单元格, 第${colCell}列，竖向合并${rowCell}个单元格 `);
          return {
            rowspan: rowCell,
            colspan: colCell
          };
        } else {
          // 清除原有的单元格，必须要加，否则就会出现单元格会被横着挤到后面了！！！
          // 本例中数据是写死的不会出现，数据若是动态后端获取的，就会出现了！！！
          return {
            rowspan: 0,
            colspan: 0
          };
        }
      }
    },

    //获取列表数据
    getData() {
      this.listLoading = true;
      getDetlList(this.$route.query)
        .then((response) => {
          this.listForm.list = response.data;
          this.listForm.list.forEach((item, index) => {
            if (!item.shppCnt) {
              item.shppCnt = item.delvCnt;
            }
            this.numOrPriceChange(index, item);
          });
          this.total = response.data.total;
          this.listLoading = false;
          this.computeCell(this.listForm.list);
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },

    // 出库
    checkAccept() {
      this.$refs["listForm"].validate((valid) => {
        if (valid) {
          this.$confirm("确认验收", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              this.listLoading = true;
              let data = {
                delvOrdId: this.$route.query.delvOrdId,
                detlList: []
              };
              data.detlList = this.listForm.list.map((item) => {
                return {
                  drugListId: item.drugListId,
                  lotNum: item.lotNum,
                  shppCnt: item.shppCnt
                };
              });
              saveStoIn(data)
                .then((res) => {
                  if (res.success === true) {
                    this.$message({
                      message: "验收成功",
                      type: "success"
                    });
                    this.listLoading = false;
                    this.$router.push({ path: "/storeroom/check-warehouse" });
                  }
                })
                .catch(() => {});
            })
            .catch(() => {
              this.listLoading = false;
            });
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
::v-deep #listId {
  th.el-table__cell.is-leaf,
  td.el-table__cell {
    border-bottom: 1px solid #dfe6ec;
  }
}
</style>