<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.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 type="primary" icon="el-icon-plus" @click="addRepairOrder">新增报修单</el-button>
        </el-button-group>
      </div>

      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" @current-change="handleDetail" 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="inspectUserName" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="创建人" prop="createUserName" show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="紧急程度" prop="urgencyType" show-overflow-tooltip width="120">
          <template slot-scope="scope">
            <el-tag :type="(scope.row.urgencyType) | statusFilterType">
              {{ urgencyType[scope.row.urgencyType] }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="报修状态" prop="status" show-overflow-tooltip width="100">
          <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="editOrder(scope.row)"
            >编辑
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="submitAudit(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="detailList" 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="formMap[formName]"
      :visible.sync="formVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="closeDialog"
      width="65%"
      v-if="formVisible"
    >
      <deviceRepairOrderModify
        :repairOrder="deviceRepairOrder"
        :closeDialog="closeDialog"
      ></deviceRepairOrderModify>
    </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, submitAudit, deleteRepairOrder} from "@/api/dev/deviceRepairOrder";
import deviceRepairOrderModify from "@/views/dev/DeviceRepairOrderModify.vue";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import {previewImage} from "@/api/purchase/supCompany";
import {selectDeviceInspectPlanDelect} from "@/api/dev/deviceInspectPlanDelect";
import DeviceInspectPlanProjectSet from "@/views/dev/DeviceInspectProjectSelect";
import {filterByorderIdFk} from "@/api/dev/deviceRepairOrderDetail";


export default {
  name: "DeviceRepairOrderNew",
  data() {
    return {
      filterQuery: {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        status: 0,
        page: 1,
        limit: 20
      },
      list: [],
      total: 0,
      deptList: [],
      invList: [],
      loading: false,
      formVisible: false,
      codeTableLoading: false,
      statusMap: {
        0: "草稿",
        1: "未审核",
        2: "已审核"
      },
      repairType: {
        1: "自行维修",
        2: "外部维修"
      },
      urgencyType: {
        1: "正常",
        2: "较急",
        3: "特急"
      },
      BASE_URL: process.env.VUE_APP_BASE_API,
      imgViewerVisible: false,
      imgList: [],
      formName: null,
      formMap: {
        add: "新增设备报修单",
        edit: "编辑设备报修单"
      },
      deviceRepairOrder: {
        id: null,
        orderId: null,
        deptCode: null,
        invCode: null,
        code: null,
        msg: null
      },
      showSearch: true,
      detailList: [],
      detailQuery: {},
      deviceProjectSelectVisible: false,
    };
  },
  methods: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        status: 0,
        page: 1,
        limit: 20
      };
      // this.getList();
      this.getDeptList();
    },

    onSubmit() {
      this.filterQuery.page = 1;
      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;
      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(() => {
        });
    },
    addRepairOrder() {
      this.formVisible = true;
      this.formName = 'add';
      this.deviceRepairOrder = {
        id: null,
        orderId: null,
        deptCode: null,
        invCode: null,
        code: null,
        msg: null
      };
    },
    editOrder(row) {
      this.formVisible = true;
      this.formName = 'edit';
      this.deviceRepairOrder = row;
    },
    submitAudit(row) {
      this.$confirm('是否提交审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {orderId: row.orderId};
        submitAudit(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);
        });
      });
    },
    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.detailList = [];
          } else {
            this.$message.error(res.message);
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    closeDialog() {
      this.formVisible = false;
      this.detailList = [];
      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);
    },
    datailSelect(row){
      this.detailQuery = row
      this.deviceProjectSelectVisible = true
    },
    handleDetail(row){
      this.orderIds = row.orderId;
      this.selectDeviceReceiveOrderDelect({orderIdFk:row.orderId})
    },
    selectDeviceReceiveOrderDelect(obj) {
      filterByorderIdFk(obj).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.detailList = res.data || [];
        } else {
          this.$message.error(res.message);
          this.detailList = [];
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.detailList = [];
      })
    },
    detailCloseDialogc() {
      this.deviceProjectSelectVisible = false
    },
  },
  components: {
    deviceRepairOrderModify, ElImageViewer ,DeviceInspectPlanProjectSet
  },
  filters: {
    statusFilterType(status) {
      const statusMap = {
        2: "warning",
        3: "success",
        4: "danger",
      };
      return statusMap[status];
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getDeptList();
  },
};
</script>

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

