<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-select v-model="filterQuery.invCode" placeholder="请选择仓库" clearable="true"
                     @change="invChange"
          >
            <el-option
              v-for="item in invList"
              :key="item.name"
              :label="item.name"
              :value="item.code">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="query-form-item" label="货位:">
          <el-select v-model="filterQuery.invSpaceCode" placeholder="请选择货位" clearable="true"
          >
            <el-option
              v-for="item in spaceList"
              :key="item.name"
              :label="item.name"
              :value="item.code">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </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="invName"></el-table-column>
        <el-table-column label="货位" prop="invSpaceName"></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="auditUserName"></el-table-column>
        <el-table-column label="状态" prop="status" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <el-tag>{{ statusMap[scope.row.status] }}</el-tag>
          </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="150" prop="code" show-overflow-tooltip></el-table-column>
        <el-table-column width="200" label="物资名称" prop="productName"></el-table-column>
        <el-table-column width="150" label="规格型号" prop="ggxh" show-overflow-tooltip></el-table-column>
        <el-table-column width="150" label="批次号" prop="batchNo"></el-table-column>
        <el-table-column label="养护日期" prop="createTime" width="150"></el-table-column>
        <el-table-column width="150" label="养护数量" prop="count"></el-table-column>
        <el-table-column width="150" label="养护状态" prop="maintenanceStatus">
          <template slot-scope="scope">
            <el-radio disabled="true" v-model="scope.row.maStatus"
                      :label="0">未养护
            </el-radio>
            <el-radio disabled="true" v-model="scope.row.maStatus"
                      :label="1">已养护
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column width="150" label="设备状态" prop="deviceStatus">
          <template slot-scope="scope">
            <el-radio v-model="scope.row.deviceStatus" :label="1" disabled="true">
              正常
            </el-radio>
            <el-radio v-model="scope.row.deviceStatus" :label="0" disabled="true">
              异常
            </el-radio>
          </template>
        </el-table-column>
        <el-table-column width="200" label="养护备注" prop="remark">
        </el-table-column>
        <el-table-column width="150" label="生产日期(yyMMdd)" prop="productionDate"></el-table-column>
        <el-table-column width="160" label="失效日期(yyMMdd)" prop="expireDate"
                         show-overflow-tooltip></el-table-column>
        <el-table-column width="250" label="注册/备案号" prop="zczbhhzbapzbh"
                         show-overflow-tooltip></el-table-column>
        <el-table-column width="200" 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="getDetailList"
      ></pagination>
    </el-card>

  </div>
</template>

<script>
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {getInvMAOrderList, getInvMAOrderDetailList} from "@/api/inventory/invMAOrder";

export default {
  name: "InvMAOrderCompleted",
  data() {
    return {
      filterQuery: {
        orderId: null,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        status: 2,
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      invList: [],
      spaceList: [],
      loading: false,
      codeTableLoading: false,
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核"
      },
      detailList: [],
      detailLoading: false,
      detailQuery: {
        orderIdFk: null,
        page: 1,
        limit: 20,
      },
      detailTotal: 0
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        status: 2,
        page: 1,
        limit: 20,
      };
      this.spaceList = [];
      this.getList();
      this.getInvList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.detailList = [];
      this.detailTotal = 0;
      getInvMAOrderList(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;
      })
    },
    invChange() {
      this.spaceList = [];
      this.getSpaceList();
      this.getList();
    },
    getInvList() {
      getInvListByUser()
        .then((response) => {
          this.invList = response.data || [];
          this.getSpaceList();
          this.getList();
        })
        .catch(() => {
        });
    },
    getSpaceList() {
      let params = {invWarehouseCode: this.filterQuery.invCode, status: 1};
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
      })
    },
    handleChange(row) {
      this.detailQuery.orderIdFk = row.orderId;
      this.detailQuery.page = 1;
      this.getDetailList();
    },
    getDetailList() {
      getInvMAOrderDetailList(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;
      });
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getInvList();
  },
};
</script>

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

