<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="登记记录号:">
              <el-input v-model="filterQuery.orderId" placeholder="请输入登记记录号" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="登记部门:">
              <el-select v-model="filterQuery.fromDeptCode" placeholder="请选择登记部门" clearable="true"
                         style="width: 90%"
                         @change="getDeptList"
              >
                <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-col :span="8">
            <el-form-item class="query-form-item" label="登记人:">
              <el-input v-model="filterQuery.receiveUserName" placeholder="请输入登记人" style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="登记时间:">
              <el-date-picker
                  :picker-options="pickerOptions"
                  v-model="actDateRange"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  style="width: 90%"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
              >
              </el-date-picker>
            </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 icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <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-divider style="margin: 15px"></el-divider>

      <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="fromDeptName"></el-table-column>
        <el-table-column label="登记人" prop="receiveUserName"></el-table-column>
        <el-table-column label="审核人" prop="auditUser"></el-table-column>
        <!--        <el-table-column label="登记货位" prop="fromInvSpaceName"></el-table-column>-->
        <el-table-column label="登记时间" prop="createTime"></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-column label="操作">
          <template slot-scope="scope">
            <el-button
                type="text"
                size="small"
                @click.native.stop="detailOrder(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;" border>
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="设备名称" prop="productName" width="200"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label=DI/物资编码 prop="nameCode" width="200" show-overflow-tooltip></el-table-column>
        <el-table-column label="批次号" prop="batchNo" width="200"></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="ylqxzcrbarmc" show-overflow-tooltip></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" show-overflow-tooltip></el-table-column>
        <el-table-column label="供应商" prop="supName" 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-dialog
          title = "设备登记-详情"
          :visible.sync="deviceReceiveOrderAuditMoifyVisible"
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          width="80%"
          v-if="deviceReceiveOrderAuditMoifyVisible"
      >
        <DeviceReceiveOrderAuditMoify
            :receiveQuery="receiveQuery"
            :closeDialog="closeDialog"
            :formName="formName"
        >
        </DeviceReceiveOrderAuditMoify>
      </el-dialog>


    </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 DeviceReceiveOrderAuditMoify from "@/views/dev/DeviceReceiveOrderAuditMoify"

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,
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      detailList: [],
      actDateRange:[],
      showSearch:true,
      detailLoading: false,
      receiveQuery:{},
      formName:null,
      deviceReceiveOrderAuditMoifyVisible: false,
      detailTotal: 0
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    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.actDateRange = []
      // this.getList();
      this.getDeptList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      this.detailList = [];
      this.detailTotal = 0;
      if (this.actDateRange !== null) {
        this.filterQuery.startTime = this.actDateRange[0];
        this.filterQuery.endTime = this.actDateRange[1];
      } else {
        this.filterQuery.startTime = null;
        this.filterQuery.endTime = null;
      }
      getDeviceReceiveOrderList(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();
      })
    },
    handleChange(row) {
      this.detailQuery.orderIdFk = row.orderId;
      this.detailQuery.page = 1;
      this.getDetailList();
    },
    getDetailList() {
      getDeviceReceiveOrderDetailList(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;
      });
    },
    closeDialog() {
      this.deviceReceiveOrderAuditMoifyVisible = false;
      this.getList();
    },
    detailOrder(row) {
      this.formName = "detail"
      this.receiveQuery = row
      this.deviceReceiveOrderAuditMoifyVisible = true
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  components:{
    DeviceReceiveOrderAuditMoify
  },
  created() {
    this.getDeptList();
  },
};
</script>

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

