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

        <el-row>
          <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-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-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.repariName"
                         placeholder="请选择维修人"
                         clearable="true"
                         style="width: 90%"

              >
                <el-option
                  v-for="item in userList"
                  :key="item.id"
                  :label="item.employeeName"
                  :value="item.userid">
                  <span style="float: left">{{ item.employeeName }}</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.status"
                         placeholder="请选择维修人"
                         clearable="true"
                         style="width: 90%"
              >
                <el-option label="已审核" :value="2"></el-option>
                <el-option label="维修中" :value="4"></el-option>
                <el-option label="已处理" :value="5"></el-option>
              </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-divider style="margin: 15px"></el-divider>

      <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" show-overflow-tooltip width="140"></el-table-column>
        <el-table-column label="部门" prop="deptName" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="仓库" prop="invName" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="设备名称" prop="deviceName" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="批次号" prop="batchNo" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="资产编码" prop="code" show-overflow-tooltip width="120"></el-table-column>
        <!--<el-table-column label="创建日期" prop="createTime"></el-table-column>-->
        <el-table-column label="创建人" prop="createUserName" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="审核人" prop="auditUserName" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="维修人" prop="repName" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="开始时间" prop="repariStartTime" show-overflow-tooltip width="140"></el-table-column>
        <el-table-column label="结束日期" prop="repariEndTime" show-overflow-tooltip width="140"></el-table-column>
        <el-table-column label="报修说明" prop="msg" show-overflow-tooltip width="180"></el-table-column>
        <el-table-column label="报修照片" prop="uploadImage" show-overflow-tooltip width="250">
          <template slot-scope="scope">
            <div style="color: #1890ff;" @click="showImgViewer(scope.row.uploadImage)">{{ scope.row.uploadImage }}</div>
          </template>

        </el-table-column>
        <el-table-column label="维修说明" prop="repariRemark" show-overflow-tooltip width="180"></el-table-column>
        <el-table-column label="维修照片" prop="repariImage" show-overflow-tooltip width="250">
          <template slot-scope="scope">
            <div style="color: #1890ff;" @click="showImgViewer(scope.row.repariImage)">{{ scope.row.repariImage }}</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" 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="操作" width="200" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="startOrder(scope.row)"
              :disabled="scope.row.status != 2 "
            >开始任务
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="endOrder(scope.row)"
              :disabled="scope.row.status == 2 || scope.row.status == 5"
            >结束任务
            </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>

    <el-dialog
      :title="repairMap[formName]"
      :visible.sync="formVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="65%"
      v-if="formVisible"
    >
      <DeviceRepairOrderStart
        :repairOrder="deviceRepairOrder"
        :closeDialog="closeDialog"
        :formName="formName"
      ></DeviceRepairOrderStart>
    </el-dialog>


    <el-image-viewer v-if="imgViewerVisible" style="z-index:9999" :on-close="closeImgViewer" :url-list="imgList"/>

  </div>
</template>

<script>
import {getDeptListByUser} from "@/api/auth/authDept";
import {findInvByUser} from "@/api/system/invSubWarehouse";
import {getRepairList, updateStatus, deleteRepairOrder} from "@/api/dev/deviceRepairOrder";
import {previewImage} from "@/api/purchase/supCompany";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import DeviceRepairOrderStart from "@/views/dev/DeviceRepairOrderStart";
import {warehouseUserList} from "@/api/system/invWarehouse";

export default {
  name: "DeviceRepairOrderAudit",
  data() {
    return {
      BASE_URL: process.env.VUE_APP_BASE_API,
      imgViewerVisible: false,
      imgList: [],
      filterQuery: {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        // status: 2,
        statusList: [2, 4, 5],
        page: 1,
        limit: 20
      },
      list: [],
      total: 0,
      deptList: [],
      invList: [],
      loading: false,
      formVisible: false,
      codeTableLoading: false,
      deviceRepairOrder: {},
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核",
        3: "未处理",
        4: "维修中",
        5: "已处理"
      },
      showSearch: true,
      formName: null,
      repairMap: {
        start: "开始任务",
        end: "结束任务"
      },
      userList: [],
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        statusList: [2, 4, 5],
        page: 1,
        limit: 20
      };
      this.getList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getuserList() {
      let param = {code: this.$store.getters.locInvCode}
      warehouseUserList(param).then((res) => {
        this.userList = res.data.list || [];
      })
    },
    getList() {
      this.loading = true;
      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.filterQuery.invCode = null;
      this.getInvList();
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
        this.getInvList();
        // this.getList();
      });
    },
    invChange() {
      this.getList();
    },
    getInvList() {
      let params = {deptCode: this.filterQuery.deptCode};
      findInvByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    deleteDialog(rowId) {
      this.$confirm('此操作将永久删除该领用记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {id: rowId};
        this.loading = true;
        deleteRepairOrder(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;
        });
      });
    },
    startOrder(row) {
      this.formVisible = true;
      this.deviceRepairOrder = row;
      this.formName = "start";
    },

    endOrder(row) {
      this.formVisible = true;
      this.deviceRepairOrder = row;
      this.formName = "end";
    },

    showImgViewer(row) {
      if (row.substr(-1) == ',') {
        row = row.slice(0, row.length - 1);
      }
      this.certFileUrl = this.BASE_URL + "/udiwms/image/register/file/getImage?type=image2&name=";
      console.log(this.certFileUrl + "========");
      this.imgList = [];
      previewImage({imageUrl: row, certFileUrl: this.certFileUrl}).then(response => {
        if (response.code === 20000) {
          this.imgList = response.data;
        }
        console.log(this.imgList)
        this.imgViewerVisible = true;
      });
      const m = (e) => {
        e.preventDefault()
      };
      document.body.style.overflow = 'hidden';
      document.addEventListener("touchmove", m, false); // 禁止页面滑动

    },
    closeImgViewer() {
      this.imgViewerVisible = false;
      const m = (e) => {
        e.preventDefault()
      };
      document.body.style.overflow = 'auto';
      document.removeEventListener("touchmove", m, true);
    },
    closeDialog() {
      this.formVisible = false;
      this.getList();
    },


  },
  components: {DeviceRepairOrderStart, ElImageViewer},
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getDeptList();
    this.getuserList();
  },
};
</script>

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

