<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb
        v-if="this.$route.query.type == 1"
        :crumbs="crumbs"
      ></bread-crumb>
      <bread-crumb
        v-if="this.$route.query.type == 2"
        :crumbs="crumbs2"
      ></bread-crumb>
    </div>
    <div class="app-container">
      <div class="filter-container">
        <span style="font-size: 18px;color: #606266">入库单详情</span>
        <el-row style="float:right;">
          <span style="color: #606266"
            >入库单号: {{ WarehouseInfo.warehouse.warehousing_order }}</span
          >
        </el-row>
      </div>

      <div class="cell clearfix">
        <span style="width: 300px;color: #606266"
          >单据日期:
          {{
            WarehouseInfo.warehouse.warehousing_time | parseTime("{y}-{m}-{d}")
          }}</span
        >
        <span style="width: 300px;color: #606266"
          >厂商:
          {{ WarehouseInfo.warehouse.manufacturers?  WarehouseInfo.warehouse.manufacturers.manufacturer_name:'' }}</span
        >
        <span style="width: 300px;color: #606266"
          >经销商: {{ WarehouseInfo.warehouse.dealers?WarehouseInfo.warehouse.dealers.dealer_name:''}}</span
        >
      </div>

      <div class="cell clearfix" style="margin-top: 10px">
        <el-button size="small" icon="el-icon-edit" @click="editRecord"
          >编辑</el-button
        >
        <el-button size="small" icon="el-icon-delete" @click="deleteRecord"
          >删除</el-button
        >
      </div>

      <el-row :gutter="12" style="margin-top: 10px">
        <el-table
          :data="WarehouseInfo.warehouseInfoDate"
          :class="signAndWeighBoxPatients"
          style="width: 100%"
          border
          :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)' }"
        >
          <el-table-column min-width="35" align="center">
            <template slot="header" slot-scope="scope">
              <span>药品名称</span>
            </template>

            <template slot-scope="scope">
              <span v-if="scope.row.drug_id != 0">{{
                scope.row.drug.drug_name
              }}</span>
            </template>
          </el-table-column>
          <el-table-column min-width="35" align="center">
            <template slot="header" slot-scope="scope">
              <span>规格名称</span>
            </template>
            <template slot-scope="scope">
              <span v-if="scope.row.drug_id != 0">{{
                scope.row.drug?scope.row.drug.drug_spec:''
              }}</span>
            </template>
          </el-table-column>

          <el-table-column min-width="23" align="center">
            <template slot="header" slot-scope="scope">
              <span>单价</span>
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.price }}</span>
            </template>
          </el-table-column>

          <el-table-column min-width="23" align="center">
            <template slot="header" slot-scope="scope">
              <span>入库数量</span>
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.warehousing_count }}</span>
            </template>
          </el-table-column>

          <el-table-column label="总价" min-width="20" align="center">
            <template slot-scope="scope">
              {{ calculate(scope.row.price * scope.row.warehousing_count) }}
            </template>
          </el-table-column>

          <el-table-column align="center" min-width="25">
            <template slot="header" slot-scope="scope">
              <span>批号</span>
            </template>
            <template slot-scope="scope">
              <span>{{ scope.row.number }}</span>
            </template>
          </el-table-column>
          <el-table-column label="生产日期" min-width="40" align="center">
            <template v-if="scope.row.product_date != 0" slot-scope="scope">
              {{ scope.row.product_date | parseTime("{y}-{m}-{d}") }}
            </template>
          </el-table-column>
          <el-table-column label="有效日期" min-width="40" align="center">
            <template v-if="scope.row.expiry_date != 0" slot-scope="scope">
              {{ scope.row.expiry_date | parseTime("{y}-{m}-{d}") }}
            </template>
          </el-table-column>
          <el-table-column label="备注" min-width="20" align="center">
            <template slot-scope="scope">
              <el-popover placement="top-start" width="250" trigger="hover">
                <div>{{ scope.row.remark }}</div>
                <span slot="reference" v-if="scope.row.remark.length > 20">{{
                  scope.row.remark.substr(0, 20) + "..."
                }}</span>
                <span slot="reference" v-else>{{ scope.row.remark }}</span>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </div>
  </div>
</template>

<script>
import { uParseTime } from "@/utils/tools";
import {
  GetAllConfig,
  getDrugWarehouseInfoList,
  deleteDrugWarehouseInfo
} from "@/api/drug/drug_stock";
import BreadCrumb from "../../components/bread-crumb";

export default {
  name: "stockInOrderDetail",
  components: { BreadCrumb },
  created() {
    const order_id = this.$route.query.id;
    this.GetOrderDetail(order_id);
  },
  data() {
    return {
      crumbs: [
        { path: false, name: "库存管理" },
        { path: false, name: "耗材入库单" },
        { path: false, name: "入库单详情" }
      ],
      crumbs2: [
        { path: false, name: "库存管理" },
        { path: false, name: "其他入库单" },
        { path: false, name: "入库单详情" }
      ],
      isEdit: 0,
      checked: false,
      signAndWeighBoxPatients: "sign-and-weigh-box-patients",
      goodType: [],
      goodInfo: [],
      manufacturer: [],
      dealer: [],
      Warehouse: {
        loading: false,
        warehouseDate: [],
        tableCurrentIndex: ""
      },

      WarehouseInfo: {
        loading: false,
        warehouseInfoDate: [],
        warehouse: {}
      }
    };
  },
  methods: {

    calculate: function(val) {
      if (val == 0) {
        return "";
      }
      return Math.round(parseFloat(val) * 100) / 100;
    },
    GetOrderDetail: function(order_id) {
      const params = {
        id: order_id
      };
      getDrugWarehouseInfoList(params).then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          for (let i = 0; i < response.data.data.info.length; i++) {
            this.WarehouseInfo.warehouseInfoDate.push(
              response.data.data.info[i]
            );
          }
          this.WarehouseInfo.warehouse = response.data.data.warehousing;
        }
      });
    },
    deleteRecord: function() {
      const ids = [];
      ids.push(this.WarehouseInfo.warehouse.id);
      const idStr = ids.join(",");

      const params = {
        ids: idStr
      };

      this.$confirm("确认删除入库单记录?", "删除入库单记录", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteDrugWarehouseInfo(params).then(response => {
            if (response.data.state == 0) {
              this.$message.error(response.data.msg);
              return false;
            } else {
              this.$notify({
                title: "成功",
                message: "删除成功",
                type: "success",
                duration: 2000
              });

              this.$router.back(-1);
            }
          });
        })
        .catch(() => {});
    },
    editRecord: function() {
      this.$emit("edit-record");
    }
  }
};
</script>

<style rel="stylesheet/css" lang="scss" scoped>
.information {
  border: 1px #dcdfe6 solid;
  padding: 30px 20px 30px 20px;

  .border {
    border-bottom: 1px #dcdfe6 solid;
    margin: 0px 0 20px 0;
  }
}

.edit_separater {
  border-top: 1px solid rgb(233, 233, 233);
  margin-top: 15px;
  margin-bottom: 15px;
}
</style>

<style>
.sign-and-weigh-box .sign-and-weigh-box-patients .cell {
  font-size: 12px;
}

.sign-and-weigh-box .sign-and-weigh-box-patients .current-row > td {
  background: #6fb5fa;
}

.count {
  color: #bd2c00;
}
</style>
