<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" :inline="true">
        <el-form-item class="query-form-item" label="养护记录号:">
          <el-input v-model="filterQuery.orderId" placeholder="请输入养护记录号"
                    clearable="true"></el-input>
        </el-form-item>
        <el-form-item class="query-form-item" label="设备编号:">
          <el-input v-model="filterQuery.code" placeholder="请输入设备编号"
                    clearable="true"></el-input>
        </el-form-item>
        <el-form-item class="query-form-item" label="领用记录号:">
          <el-input v-model="filterQuery.receiveOrderId" placeholder="请输入领用记录号"
                    clearable="true"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button-group style="margin-left: 10px;display:flex;">
            <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                @current-change="handleChange"
                border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="养护记录号" prop="orderId"></el-table-column>
        <el-table-column label="领用记录号" prop="receiveOrderId"></el-table-column>
        <el-table-column label="养护日期" prop="createTime"></el-table-column>
        <el-table-column label="养护人" prop="createUserName"></el-table-column>
        <el-table-column label="养护备注" prop="remark" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="print(scope.row)"
            >打印
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>

    <el-card>
      <el-table v-loading="detailLoading" :data="detailList" style="width: 100%; margin-top: 10px;">
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="条码" width="200" prop="code" show-overflow-tooltip></el-table-column>
        <el-table-column label="产品通用名" prop="productName" width="200"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="批次号" prop="batchNo" width="150"></el-table-column>
        <el-table-column label="序列号" prop="serialNo" width="150"></el-table-column>
        <el-table-column label="领用日期" prop="createTime" width="150"></el-table-column>
        <el-table-column label="生产日期" prop="productionDate" width="150"></el-table-column>
        <el-table-column label="失效日期" prop="expireDate" width="150"></el-table-column>
        <el-table-column label="注册/备案凭证号" prop="zczbhhzbapzbh"
                         show-overflow-tooltip></el-table-column>
        <el-table-column label="生产企业" prop="ylqxzcrbarmc"
                         show-overflow-tooltip></el-table-column>
      </el-table>

      <pagination
        v-show="detailTotal>0"
        :total="detailTotal"
        :limit.sync="detailQuery.limit"
        :page.sync="detailQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>

  </div>
</template>

<script>
import {
  getDeviceMAOrderList,
  getDeviceMAOrderDetailList,
  verifyTemplateFile,
  printOrder
} from "@/api/inventory/deviceMAOrder";

export default {
  name: "DeviceMAOrderCompleted",
  data() {
    return {
      filterQuery: {
        orderId: null,
        code: null,
        receiveOrderId: null,
        status: 1,
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      loading: false,
      detailQuery: {
        orderIdFk: null,
        page: 1,
        limit: 20,
      },
      detailList: [],
      detailLoading: false,
      detailTotal: 0
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        code: null,
        receiveOrderId: null,
        status: 1,
        page: 1,
        limit: 20,
      };
      this.spaceList = [];
      this.getList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.detailList = [];
      this.detailTotal = 0;
      getDeviceMAOrderList(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.list = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.list = [];
        this.total = 0;
      })
    },
    handleChange(row) {
      this.detailQuery.orderIdFk = row.orderId;
      this.detailQuery.page = 1;
      this.getDetailList();
    },
    getDetailList() {
      getDeviceMAOrderDetailList(this.detailQuery).then((res) => {
        if (res.code === 20000) {
          this.detailList = res.data.list || [];
          this.detailTotal = res.data.total || 0;
        } else {
          this.detailList = [];
          this.detailTotal = 0;
        }
      }).catch(() => {
        this.detailList = [];
        this.detailTotal = 0;
      });
    },
    print(row) {
      let query = {moduleId: 7};
      this.loading = true;
      verifyTemplateFile(query).then((res) => {
        if (res.code === 20000) {
          let printParams = {
            templateId: res.data,
            orderId: row.orderId
          };
          printOrder(printParams).then((response) => {
            //将pdf文件转换为url。
            const binaryData = [];
            binaryData.push(response);
            //获取blob链接。
            let url = window.URL.createObjectURL(
              new Blob(binaryData, {type: "application/pdf"})
            );
            this.loading = false;
            window.open(url);//打开新标签页，预览pdf。
          })
        } else {
          this.loading = false;
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
      })
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>

