<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="0"></el-option>
                <el-option label="未审核" :value="1"></el-option>
                <el-option label="已审核" :value="2"></el-option>
                <el-option label="未处理" :value="3"></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  @current-change="handleDetail">
        <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="repairType" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <el-tag >
              {{ repairType[scope.row.repairType] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="紧急程度" prop="urgencyType" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <el-tag>
              {{ urgencyType[scope.row.urgencyType] }}
            </el-tag>
          </template>
        </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="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="审核日期" prop="auditTime"></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="操作" width="120" fixed="right">

          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="startOrder(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>

    <el-card class="el-card">
      <el-table v-loading="loading" :data="delectList" style="width: 100%" border highlight-current-row>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="项目内容" prop="content" width="300"></el-table-column>
        <el-table-column label="备注" prop="remark" show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="datailSelect(scope.row)"
            >详情
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="详情"
      :visible.sync="formVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="65%"
      v-if="formVisible"
    >
      <DeviceRepairOrderDetails
        :repairOrder="deviceRepairOrder"
        :closeDialog="closeDialog"
      ></DeviceRepairOrderDetails>
    </el-dialog>

    <el-dialog
      title="报修配置详情"
      :visible.sync="deviceProjectSelectVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="40%"
      v-if="deviceProjectSelectVisible"
    >
      <DeviceInspectPlanProjectSet
        :detailQuery="detailQuery"
        :closeDialog="detailCloseDialogc"
      >
      </DeviceInspectPlanProjectSet>
    </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 {warehouseUserList} from "@/api/system/invWarehouse";
import {previewImage} from "@/api/purchase/supCompany";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import DeviceRepairOrderDetails from "@/views/dev/DeviceRepairOrderDetails";
import {filterByorderIdFk} from "@/api/dev/deviceRepairOrderDetail";
import DeviceInspectPlanProjectSet from "@/views/dev/DeviceInspectProjectSelect";



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,
        page: 1,
        limit: 20
      },
      list: [],
      total: 0,
      deptList: [],
      delectList:[],
      deviceProjectSelectVisible:false,
      detailQuery:{},
      invList: [],
      loading: false,
      formVisible: false,
      codeTableLoading: false,
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核",
        3: "未处理",
        4: "维修中",
        5: "已处理"
      },
      repairType:{
        1:"自行维修",
        2:"外部维修"
      },
      urgencyType:{
        1:"正常",
        2:"较急",
        3:"特急"
      },
      formName: null,
      showSearch: true,
      userList: [],
      deviceRepairOrder: {},
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        page: 1,
        limit: 20
      };
      // this.getList();
      this.getDeptList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },

    datailSelect(row){
      this.detailQuery = row
      this.deviceProjectSelectVisible = true
    },

    detailCloseDialogc() {
      this.formVisible = false;
      this.deviceProjectSelectVisible = false
      this.selectDeviceInspectPlanDelect({orderIdFk:this.orderIds})
      this.getList()
    },
    handleDetail(row){
      this.selectDeviceInspectPlanDelect({orderIdFk:row.orderId})
    },
    selectDeviceInspectPlanDelect(obj) {
      filterByorderIdFk(obj).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.delectList = res.data || [];
        } else {
          this.$message.error(res.message);
          this.delectList = [];
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.delectList = [];
      })
    },

    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(() => {
        });
    },
    getuserList() {
      let param = {code: this.$store.getters.locInvCode}
      warehouseUserList(param).then((res) => {
        this.userList = res.data.list || [];
      })
    },
    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();
            this.delectList = [];
          } else {
            this.$message.error(res.message);
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    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);
    },

    startOrder(row) {
      this.formVisible = true;
      this.deviceRepairOrder = row;

    },
    closeDialog() {
      this.formVisible = false;
      this.getList();
    },


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

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

