<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="8">
            <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-col :span="8">
            <el-form-item class="query-form-item" label="仓库:">
              <el-select v-model="filterQuery.invCode" 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="8">
            <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-row>

        <el-row>
          <el-col :span="8">
            <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-col :span="8">
            <el-form-item class="query-form-item" label="维保类型:">
              <el-select v-model="filterQuery.type" style="width: 90%" placeholder="请选择维保类型" clearable="true"
              >
                <el-option :value="1" label="报修"/>
                <el-option :value="2" label="计划"/>
                <el-option :value="3" label="手动"/>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item class="query-form-item" label="设备状态:">
              <el-select v-model="filterQuery.deviceStatus" style="width: 90%" placeholder="请选择设备状态"
                         clearable="true"
              >
                <el-option :value="0" label="异常"/>
                <el-option :value="1" label="正常"/>
              </el-select>
            </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-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="deptName"></el-table-column>
        <el-table-column label="仓库" prop="invName"></el-table-column>
        <el-table-column label="设备名称" prop="deviceName"></el-table-column>
        <el-table-column label="资产编码" prop="code"></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="lastOrderId"></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="type">
          <template slot-scope="scope">
            <el-tag>{{ typeMap[scope.row.type] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="养护说明" prop="remark"></el-table-column>
        <el-table-column label="设备状态" prop="status" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <el-tag>{{ deviceStatusMap[scope.row.deviceStatus] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <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="auditOrder(scope.row)"
            >审核
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteDialog(scope.row.id)"
            >删除
            </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>

  </div>
</template>

<script>
import {getDeptListByUser} from "@/api/auth/authDept";
import {findInvByUser} from "@/api/system/invSubWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {getDeviceInspectOrderList, updateStatus, deleteDeviceInspectOrder} from "@/api/inventory/deviceInspectOrder";

export default {
  name: "DeviceReceiveOrderNew",
  data() {
    return {
      filterQuery: {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        code: null,
        status: 1,
        type: 1,
        deviceStatus: 1,
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      deptList: [],
      invList: [],
      spaceList: [],
      loading: false,
      formVisible: false,
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核 ",
      },
      deviceStatusMap: {
        0: "异常",
        1: "正常",
      },
      typeMap: {
        1: "报修",
        2: "计划",
        3: "手动"
      },
      formName: null,
      formMap: {
        add: "新增维保记录",
        edit: "编辑维保记录"
      },
      showSearch: true
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        code: null,
        status: 1,
        page: 1,
        limit: 20,
      };
      // this.getList();
      this.getDeptList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getDeviceInspectOrderList(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.invCode = 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.deptCode};
      findInvByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    getSpaceList() {
      let params = {
        invWarehouseCode: this.filterQuery.fromInvCode,
        invStorageCode: this.filterQuery.fromInvCode.deptCode,
        status: 1
      };
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
        this.getList();
      })
    },
    deleteDialog(rowId) {
      this.$confirm('此操作将永久删除该维保单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {id: rowId};
        this.loading = true;
        deleteDeviceInspectOrder(params).then((res) => {
          this.loading = false;
          if (res.code === 20000) {
            this.$message.success("删除成功！");
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    closeDialog() {
      this.formVisible = false;
      this.getList();
    },
    auditOrder(row) {
      this.$confirm('请选择审核意见', '提示', {
        confirmButtonText: '通过',
        cancelButtonText: '驳回',
        type: 'warning',
        center: true,
        closeOnPressEscape: false,//按下 ESC 键关闭弹窗
        closeOnClickModal: false,//点击遮罩关闭弹窗
        distinguishCancelAndClose: true,//区分取消与关闭
      }).then(() => {
        let params = {
          orderId: row.orderId,
          status: 2
        };
        updateStatus(params).then((res) => {
          if (res.code === 20000) {
            this.$message.success("审核通过！");
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        }).catch((error) => {
          this.$message.error(error.message);
        })
      }).catch((action) => {
        if (action === "cancel") {
          let params = {
            orderId: row.orderId,
            status: 0
          };
          updateStatus(params).then((res) => {
            if (res.code === 20000) {
              this.$message.success("已驳回！")
              this.getList();
            } else {
              this.$message.error(res.message);
            }
          }).catch((error) => {
            this.$message.error(error.message);
          });
        } else {
          return;
        }
      });
    },
  },
  components: {
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getDeptList();
  },
};
</script>

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

