<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-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.repairType" placeholder="请选择维修类型" style="width: 90%" clearable>
                <el-option label="自行维修" :value=1></el-option>
                <el-option label="外部维修" :value=2></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.urgencyType" placeholder="请选择紧急程度" style="width: 90%" clearable>
                <el-option label="正常" :value=1></el-option>
                <el-option label="较急" :value=2></el-option>
                <el-option label="特急" :value=3></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" show-overflow-tooltip width="140"></el-table-column>-->

        <el-table-column label="创建人" prop="createUserName" 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="planTime" show-overflow-tooltip width="140"></el-table-column>-->
        <!--<el-table-column label="备注" prop="remark"  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)">{{ scope.row.uploadImage }}</div>-->
        <!--  </template>-->

        <!--</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="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>

    <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"
      :before-close="closeDialog"
      width="65%"
      v-if="formVisible"
    >
      <DeviceRepairOrderAduitModify
        :repairOrder="deviceRepairOrder"
        :closeDialog="closeDialog"
      ></DeviceRepairOrderAduitModify>
    </el-dialog>

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

  </div>
</template>

<script>
import {getDeptListByUser} from "@/api/auth/authDept";
import {findInvByUser} from "@/api/system/invSubWarehouse";
import {getRepairList, updateStatus, deleteRepairOrder} from "@/api/dev/deviceRepairOrder";
import DeviceRepairOrderAduitModify from "@/views/dev/DeviceRepairOrderAuditModify";
import {previewImage} from "@/api/purchase/supCompany";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import {selectDeviceInspectPlanDelect} from "@/api/dev/deviceInspectPlanDelect";
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: 1,
        page: 1,
        limit: 20
      },
      list: [],
      total: 0,
      deptList: [],
      delectList:[],
      detailQuery:{},
      deviceProjectSelectVisible:false,
      invList: [],
      loading: false,
      formVisible: false,
      codeTableLoading: false,
      deviceRepairOrder:{},
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核"
      },
      repairType:{
        1:"自行维修",
        2:"外部维修"
      },
      urgencyType:{
        1:"正常",
        2:"较急",
        3:"特急"
      },
      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();
    },
    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 = [];
      })
    },

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


    detailCloseDialogc() {
      this.formVisible = false;
      this.deviceProjectSelectVisible = false
      this.selectDeviceInspectPlanDelect({orderIdFk:this.orderIds})
      this.getList()
    },
    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;
      console.log(this.filterQuery.invCode)
      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();
            this.delectList = [];
          } else {
            this.$message.error(res.message);
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    auditOrder(row) {
      this.formVisible = true;
      this.deviceRepairOrder = 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;
      //   }
      // });
    },
    closeDialog() {
      this.formVisible = false;
      this.delectList = [];
      this.getList();
    },
    showImgViewer(row){
      if(row.uploadImage.substr(-1) == ','){
        row.uploadImage=row.uploadImage.slice(0, row.uploadImage.length-1);
      }
      this.certFileUrl = this.BASE_URL +"/udiwms/image/register/file/getImage?type=image2&name=";
      console.log(this.certFileUrl + "========");
      this.imgList = [];
      previewImage({imageUrl: row.uploadImage, 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);
    },

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

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

