<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" label-width="100px" >

        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="报修记录号:">
              <el-input v-model="filterQuery.orderId"  style="width: 90%" placeholder="请输入报修记录号"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item class="query-form-item" label="领用部门:">
              <el-select v-model="filterQuery.deptCode"   style="width: 90%"  placeholder="请选择部门" clearable="true"
                         @change="deptChange"
              >
                <el-option
                  v-for="item in deptList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="10">
            <el-form-item class="query-form-item" label="出库仓库:">
              <el-select v-model="filterQuery.fromInvCode"   style="width: 90%" placeholder="请选择仓库" clearable="true"
              >
                <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-col>


          <el-col :span="10">
            <el-form-item class="query-form-item" label="资产编码:">
              <el-input v-model="filterQuery.code"  style="width: 90%" placeholder="请输入资产编码"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <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>
      </div>

      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="设备报修记录号" prop="orderId"></el-table-column>
        <el-table-column label="设备名称" prop="deviceName"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh"></el-table-column>
        <el-table-column label="批次号" prop="batchNo"></el-table-column>
        <el-table-column label="资产编码" prop="code"></el-table-column>
        <el-table-column label="部门" prop="deptName"></el-table-column>
        <el-table-column label="仓库" prop="invName"></el-table-column>
        <el-table-column label="创建日期" prop="createTime"></el-table-column>
        <el-table-column label="审核日期" prop="auditTime"></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="msg"></el-table-column>
        <el-table-column label="备注" prop="remark"></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>


  </div>
</template>

<script>
import {getDeptListByUser} from "@/api/auth/authDept";
import {findInvByUser} from "@/api/system/invSubWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {
  getDeviceReceiveOrderDetailList,
  getDeviceReceiveOrderList
} from "@/api/inventory/deviceReceiveOrder";
import {getRepairList} from "@/api/inventory/deviceRepairOrder";

export default {
  name: "DeviceReceiveOrderCompleted",
  data() {
    return {
      filterQuery: {
        orderId: null,
        fromDeptCode: this.$store.getters.locDeptCode,
        fromInvCode: null,
        fromInvSpaceCode: null,
        status: 2,
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      deptList: [],
      invList: [],
      spaceList: [],
      loading: false,
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核"
      },
      detailQuery: {
        orderIdFk: null,
        page: 1,
        limit: 20,
      },
      detailList: [],
      detailLoading: false,
      detailTotal: 0
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        fromDeptCode: this.$store.getters.locDeptCode,
        fromInvCode: null,
        fromInvSpaceCode: null,
        status: 2,
        page: 1,
        limit: 20,
      };
      this.spaceList = [];
      this.getList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.detailList = [];
      this.detailTotal = 0;
      getRepairList(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;
      })
    },
    deptChange() {
      this.invList = [];
      this.spaceList = [];
      this.filterQuery.fromInvCode = null;
      this.filterQuery.fromInvSpaceCode = null;
      this.getInvList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
        this.getList();
      });
    },
    invChange() {
      this.spaceList = [];
      this.filterQuery.fromInvSpaceCode = null;
      this.getSpaceList();
    },
    getInvList() {
      let params = {deptCode: this.filterQuery.fromDeptCode};
      findInvByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    getSpaceList() {
      let params = {
        invWarehouseCode: this.filterQuery.fromInvCode,
        invStorageCode: this.filterQuery.fromInvCode.fromDeptCode,
        status: 1
      };
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
        this.getList();
      })
    },
    closeDialog() {
      this.formVisible = false;
      this.getList();
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getDeptList();
  },
};
</script>

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

