<template>
  <div class="mod-config">
    <div>
      <span @click="favoriteFunction()">
        <icon-svg :name="favorite?'xiangqufill':'xiangqu'" class="sl-svg"></icon-svg>
      </span>
    </div>

    <el-form :inline="true" label-position="top" :model="searchData" @keyup.enter.native="getDataList()">
      <el-form-item :label="'反馈单号'">
        <el-input v-model="searchData.planID" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'工单编码'">
        <el-input v-model="searchData.orderNo" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'设备编码'">
        <el-input v-model="searchData.objectID" clearable style="width: 120px"></el-input>
      </el-form-item>
<!--      <el-form-item :label="'计划执行人员编码'">-->
<!--        <el-input v-model="searchData.planOperatorName" clearable style="width: 120px"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item :label="'人员部门'">
        <el-input v-model="searchData.departmentName" readonly style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'状态'">
        <el-select v-model="searchData.status" style="width: 100px">
          <el-option label="全部" value=""></el-option>
          <el-option label="未开工" value="未开工"></el-option>
          <el-option label="待审核" value="待审核"></el-option>
          <el-option label="已完工" value="已完工"></el-option>
          <el-option label="已取消" value="已取消"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'单据来源'">
        <el-select v-model="searchData.documentSource" style="width: 100px">
          <el-option label="全部" value=""></el-option>
          <el-option label="设备点检" value="设备点检"></el-option>
          <el-option label="设备维修" value="设备维修"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="'计划执行日期：'">
        <el-date-picker
          style="width: 120px"
          v-model="searchData.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item style="margin-top: 23px;">
        <laber style="margin-left: -9px;font-size: 19px">&#10142</laber>
      </el-form-item>
      <el-form-item :label="' '">
        <el-date-picker
          style="width: 120px"
          v-model="searchData.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="' '">
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="changeModel()">更改执行人员</el-button>
        <download-excel
          :fields="fields()"
          :data="exportData"
          type="xls"
          :name="exportName"
          :header="exportHeader"
          :footer="exportFooter"
          :fetch="createExportData"
          :before-generate="startDownload"
          :before-finish="finishDownload"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          {{ "导出" }}
        </download-excel>
      </el-form-item>
    </el-form>

    <el-table
      :height="height"
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      :row-style="controlRowStyle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        :selectable="selectFlag"
        width="50">
      </el-table-column>
      <el-table-column
        prop="urgency"
        header-align="center"
        align="center"
        label="紧急程度"
        width="90">
      </el-table-column>
      <el-table-column
        prop="result"
        header-align="center"
        align="center"
        label="维修结论"
        width="80">
        <template slot-scope="scope">
          <div :style="{fontWeight:'bold', color: scope.row.result =='维修失败' ? 'red' : scope.row.result =='维修完成' ? '#3ac252' : ''}">
            {{ scope.row.result }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item,index) in columnList" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed==''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden">{{scope.row[item.columnProp]}}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
<!--          <a type="text" size="small" v-if="scope.row.status === '待审核'" @click="checkModal(scope.row)">审核</a>-->
          <a type="text" size="small" v-if="scope.row.status === '待审核' || scope.row.status === '已完工'" @click="reportModal(scope.row)">详情</a>
          <a type="text" size="small" v-if="scope.row.status === '未开工'" @click="cancelOrder(scope.row)">取消工单</a>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100, 200, 500]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog title="批量修改计划人员" :close-on-click-modal="false" v-drag :visible.sync="changeModelFlag" width="300px">
      <el-form :inline="true" label-position="top"  >
        <el-form-item style="margin-left: 10px" >
          <span slot="label" style="" @click="getBaseList(201)"><a herf="#">计划执行人员</a></span>
          <el-input v-model="planOperator" style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item :label="'人员名称'">
          <el-input v-model="planOperatorName" disabled style="width: 120px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 10px;text-align:center">
        <el-button type="primary" @click="changeOrderOperator()">保存</el-button>
        <el-button type="primary" @click="changeModelFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <el-dialog title="审核" :close-on-click-modal="false" v-drag :visible.sync="submitModelFlag" width="375px">
      <el-form :inline="true" label-position="top">
        <el-form-item :label="'难度等级'">
          <el-select filterable v-model="submitData.difficultyLevel" style="width: 130px" placeholder="请评估难度等级">
            <el-option label="难度一" value="难度一"></el-option>
            <el-option label="难度二" value="难度二"></el-option>
            <el-option label="难度三" value="难度三"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="'备注'">
          <el-input v-model="submitData.difficultyRemark" style="width: 200px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 10px;text-align:center">
        <el-button type="primary" @click="submitModel()">确定</el-button>
        <el-button type="primary" @click="submitModelFlag = false">取消</el-button>
      </el-footer>
    </el-dialog>

    <el-dialog title="执行结果" :close-on-click-modal="false" v-drag :visible.sync="detailModelFlag" width="960px">
      <el-form :inline="true" label-position="top">
        <el-form-item :label="'设备编码'">
          <el-input v-model="detailData.objectID" disabled style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'设备名称'">
          <el-input v-model="detailData.objectDesc" disabled style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'机台'">
          <el-input v-model="detailData.resourceDesc" disabled style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'反馈描述'">
          <el-input v-model="detailData.feedBackDesc" disabled style="width: 221px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top">
        <el-form-item label="故障编码">
          <el-input v-model="detailData.defectID" disabled style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item label="故障名称">
          <el-input v-model="detailData.defectDesc" disabled style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'维修结果'">
          <el-input v-model="detailData.result" disabled style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'处置措施'">
          <el-input v-model="detailData.disposalMeasures" disabled style="width: 221px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top">
        <el-form-item :label="'计划执行日期'">
          <el-date-picker style="width: 221px" v-model="detailData.planDate" type="datetime" disabled value-format='yyyy-MM-dd' format='yyyy-MM-dd'></el-date-picker>
        </el-form-item>
        <el-form-item :label="'到达时间'">
          <el-date-picker style="width: 221px" v-model="detailData.reachDate" type="datetime" value-format='yyyy-MM-dd HH:mm:ss' format='yyyy-MM-dd HH:mm:ss'></el-date-picker>
        </el-form-item>
        <el-form-item :label="'实际执行时间'">
          <el-date-picker style="width: 221px" v-model="detailData.actualDate" type="datetime" value-format='yyyy-MM-dd HH:mm:ss' format='yyyy-MM-dd HH:mm:ss'></el-date-picker>
        </el-form-item>
        <el-form-item :label="'工作时长(m)'">
          <el-input v-model="detailData.workTime" type="number" disabled :min="0" style="width: 221px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top">
        <el-form-item label="执行人员">
          <el-input v-model="detailData.actualOperatorName" disabled style="width: 673px"></el-input>
        </el-form-item>
        <el-form-item :label="' '">
          <el-button v-if="detailData.status === '已完工'" type="primary" @click="checkModal()">取消审核</el-button>
          <el-button v-if="detailData.status === '待审核'" type="primary" @click="checkModal()">审核</el-button>
        </el-form-item>
        <el-form-item :label="' '">
          <el-button type="primary" @click="checkFaultImageModal()">故障图片</el-button>
        </el-form-item>
        <el-form-item :label="' '">
          <el-button type="primary" @click="checkSparPartImageModal()">备件图片</el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top">
        <el-form-item :label="'故障原因'">
          <el-input v-if="detailData.status === '已完工'" readonly type="textarea" v-model="detailData.faultReason" :rows="3" resize='none' show-word-limit style="width: 456px"></el-input>
          <el-input v-if="detailData.status === '待审核'" type="textarea" v-model="detailData.faultReason" :rows="3" resize='none' show-word-limit style="width: 456px"></el-input>
        </el-form-item>
        <el-form-item :label="'处理方式'">
          <el-input v-if="detailData.status === '已完工'" readonly type="textarea" v-model="detailData.handlingMethod" :rows="3" resize='none' show-word-limit style="width: 456px"></el-input>
          <el-input v-if="detailData.status === '待审核'" type="textarea" v-model="detailData.handlingMethod" :rows="3" resize='none' show-word-limit style="width: 456px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" style="margin-top: 45px">
        <el-form-item :label="'预防措施'">
          <el-input v-if="detailData.status === '已完工'" readonly type="textarea" v-model="detailData.preventiveMeasure" :rows="3" resize='none' show-word-limit style="width: 456px"></el-input>
          <el-input v-if="detailData.status === '待审核'" type="textarea" v-model="detailData.preventiveMeasure" :rows="3" resize='none' show-word-limit style="width: 456px"></el-input>
        </el-form-item>
        <el-form-item :label="'执行结果备注'">
          <el-input v-if="detailData.status === '已完工'" readonly type="textarea" v-model="detailData.remark" :rows="3" resize='none' show-word-limit style="width: 456px"></el-input>
          <el-input v-if="detailData.status === '待审核'" type="textarea" v-model="detailData.remark" :rows="3" resize='none' show-word-limit style="width: 456px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:40px;margin-top: 65px;text-align:center">
        <el-button style="margin-left: -12px" type="primary" @click="updateResult()">保存</el-button>
        <el-button type="primary" @click="detailModelFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <!-- 部门 -->
    <el-dialog title="部门清单" :close-on-click-modal="false" v-drag :visible.sync="departmentModelFlag" width="520px">
      <div class="rq">
        <el-form :inline="true" label-position="top" :model="departmentData">
          <el-form-item :label="'部门编码'">
            <el-input v-model="departmentData.deptId" style="width: 120px"></el-input>
          </el-form-item>
          <el-form-item :label="'部门名称'">
            <el-input v-model="departmentData.deptName" style="width: 120px"></el-input>
          </el-form-item>
          <el-form-item :label="' '">
            <el-button type="primary" @click="getDepartmentList()">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :height="300"
          :data="departmentList"
          ref="departmentTable"
          @row-click="departmentClickRow"
          @selection-change="selectionDepartment"
          :row-key="getRowKeys"
          border
          v-loading="dataListLoading"
          style="width: 100%;">
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            :reserve-selection="true"
            width="50">
          </el-table-column>
          <el-table-column
            v-for="(item,index) in departmentDetailList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="!item.columnHidden">{{scope.row[item.columnProp]}}</span>
              <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="confirmDepartment()">确认</el-button>
        <el-button type="primary" @click="departmentModelFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <el-dialog title="图片查看" :close-on-click-modal="false" v-drag :visible.sync="imageModalFlag" width="390px" style="height: 550px;">
      <div v-viewer>
        <img v-for="(item, index) in descImages" :src="item" :key="index" style="width: 100px;height: 100px"/>
      </div>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="imageModalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>

  </div>
</template>

<script>
  import {
    eamWorkOrderSearchForDefect,
    cancelOrder,
    changeOrderOperator,
    submitDefect,
    searchFileUrl,
    getEmpyDeptList,
    getDeptList,
    updateReportResult, // 修改执行结果
  } from "@/api/eam/eam.js"
  import {userFavoriteList, saveUserFavorite, removeUserFavorite} from '@/api/userFavorite.js'
  import Chooselist from '@/views/modules/common/Chooselist_eam'
  import {isAuth} from '../../../utils'
  export default {
    components: {
      Chooselist
    },
    watch: {
      searchData: {
        deep: true,
        handler: function (newV, oldV) {
          this.searchData.groupID = this.searchData.groupID.toUpperCase()
        }
      },
      modalData: {
        deep: true,
        handler: function (newV, oldV) {
          this.modalData.groupID = this.modalData.groupID.toUpperCase()
        }
      },
      // detailData: {
      //   deep: true,
      //   handler: function (newV, oldV) {
      //     this.detailData.workTime = Math.ceil(Math.floor(this.dayjs(this.detailData.actualDate).diff(this.detailData.reachDate, 'seconds') / 30) / 2)
      //   }
      // }
    },
    data () {
      return {
        // 是否收藏
        favorite: false,
        // 导出 start
        exportData: [],
        exportName: "维修工单" + this.dayjs().format('YYYYMMDDHHmmss'),
        exportHeader: ["维修工单"],
        exportFooter: [],
        exportList:[],
        // 导出 end
        tagNo:'',
        searchData: {
          site: this.$store.state.user.site,
          orderNo: '',
          planID: '',
          objectID: '',
          planOperator: '',
          planOperatorName: '',
          functionType: 'C',
          status: '',
          startDate:'',
          endDate:'',
          departmentName: '',
          documentSource: '',
          page: 1,
          limit: 10,
        },
        height: 200,
        pageIndex: 1,
        pageSize: 50,
        totalPage: 0,
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        modalFlag:false,
        modalDisableFlag:false,
        modalData:{
          flag:'',
          site: this.$store.state.user.site,
          groupID:'',
          groupDesc:'',
          active:'',
        },
        departmentList:[],
        // 展示列集
        columnList: [
          {
            userId: this.$store.state.user.name,
            functionId: 101014,
            serialNumber: '101014TableResult',
            tableId: "101014Table",
            tableName: "common",
            columnProp: 'disposalMeasures',
            headerAlign: "center",
            align: "center",
            columnLabel: '处置措施',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableOrderNo',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'orderNo',
            headerAlign: "center",
            align: "center",
            columnLabel: '工单编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TablePlanID',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'planID',
            headerAlign: "center",
            align: "center",
            columnLabel: '反馈单号',
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TablePlanID',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'feedBackDesc',
            headerAlign: "center",
            align: "center",
            columnLabel: '反馈描述',
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TablePropertiesCode',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'defectID',
            headerAlign: "center",
            align: "center",
            columnLabel: '故障编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TablePropertiesCode',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'defectDesc',
            headerAlign: "center",
            align: "center",
            columnLabel: '故障名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableObjectID',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'objectID',
            headerAlign: "center",
            align: "center",
            columnLabel: '设备编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableObjectID',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'objectDesc',
            headerAlign: "center",
            align: "center",
            columnLabel: '设备名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 120,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableObjectID',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'resourceDesc',
            headerAlign: "center",
            align: "center",
            columnLabel: '机台名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 120,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableObjectID',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'departmentName',
            headerAlign: "center",
            align: "center",
            columnLabel: '设备部门',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableObjectID',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'createBy',
            headerAlign: "center",
            align: "center",
            columnLabel: '报修人员名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableStatus',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'status',
            headerAlign: "center",
            align: "center",
            columnLabel: '状态',
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TablePlanOperatorName',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'planOperatorName',
            headerAlign: "center",
            align: "center",
            columnLabel: '计划执行人员',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableActualOperatorName',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'actualOperatorName',
            headerAlign: "center",
            align: "center",
            columnLabel: '实际执行人员',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableActualPlanDate',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'planDate',
            headerAlign: "center",
            align: "center",
            columnLabel: '计划执行日期',
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableCreatedDate',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'createdDate',
            headerAlign: "center",
            align: "center",
            columnLabel: '故障时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 130,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableActualPlanDate',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'reachDate',
            headerAlign: "center",
            align: "center",
            columnLabel: '到达时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 130,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableActualActualDate',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'actualDate',
            headerAlign: "center",
            align: "center",
            columnLabel: '实际执行时间',
            columnHidden: false,
            columnImage: false,
            columnSortable: true,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 130,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableResponseTime',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'responseTime',
            headerAlign: "center",
            align: "right",
            columnLabel: '维修响应时长(m)',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 110,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableWorkTime',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'workTime',
            headerAlign: "center",
            align: "right",
            columnLabel: '工作时长(m)',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableActualRemark',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'remark',
            headerAlign: "center",
            align: "center",
            columnLabel: '工单备注',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 120,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableActualRemark',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'difficultyLevel',
            headerAlign: "center",
            align: "center",
            columnLabel: '难度等级',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 80,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableActualRemark',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'difficultyRemark',
            headerAlign: "center",
            align: "center",
            columnLabel: '难度备注',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 120,
          },
          {
            userId: this.$store.state.user.name,
            functionId: 101018,
            serialNumber: '101018TableActualRemark',
            tableId: "101018Table",
            tableName: "common",
            columnProp: 'documentSource',
            headerAlign: "center",
            align: "center",
            columnLabel: '单据来源',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
            columnWidth: 100,
          },
        ],
        changeModelFlag:false,
        planOperator:'',
        planOperatorName:'',
        submitData:{
          site: this.$store.state.user.site,
          orderNo: '',
          planID: '',
          objectID: '',
          checker: '',
          checkerName: '',
          difficultyLevel: '',
          difficultyRemark: '',
          status: '',
          updateBy: this.$store.state.user.name,
          createBy: this.$store.state.user.name,
          planDesc: '',
          disposalMeasures: '',
          defectID: '',
          remark: ''
        },
        submitModelFlag: false,
        detailData: {},
        detailModelFlag: false,
        descImages: [],
        imageModalFlag: false,
        departmentModelFlag: false,
        departmentData: {
          site: this.$store.state.user.site,
          deptId: '',
          deptName: '',
        },
        departmentListSelections: [],
        departmentDetailList: [
          {
            columnProp: 'deptId',
            headerAlign: "center",
            align: "center",
            columnLabel: '部门编码',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
          {
            columnProp: 'deptName',
            headerAlign: "center",
            align: "center",
            columnLabel: '部门名称',
            columnHidden: false,
            columnImage: false,
            columnSortable: false,
            sortLv: 0,
            status: true,
            fixed: '',
          },
        ],
      }
    },
    mounted () {
      this.$nextTick(() => {
        this.height = window.innerHeight - 210
      })
    },

    created () {
      this.favoriteIsOk()
      // 查询登陆人员的部门,并过滤出相应工单
      this.getUserDept()
    },

    methods: {

      // 控制单行的背景颜色
      controlRowStyle({row, rowIndex}) {
        // 区分不同的样式对应不同的颜色
        if (row.status === '待审核') {
          return "background-color: #D8ECF1"
        }
      },

      // 查询登陆人员的部门
      getUserDept () {
        let tempDate = {
          site: this.$store.state.user.site,
          username: this.$store.state.user.name,
        }
        getDeptList(tempDate).then(({data}) => {
          if (data.code === 0) {
            data.rows.forEach(val => {
              this.searchData.departmentName = this.searchData.departmentName + ";" + val
            })
            this.searchData.departmentName = this.searchData.departmentName.substring(1)
            this.getDataList()
          }
        })
      },

      // 校验用户是否收藏
      favoriteIsOk() {
        let userFavorite = {
          userId: this.$store.state.user.id,
          languageCode: this.$i18n.locale
        }
        userFavoriteList(userFavorite).then(({data}) => {
          for (let i = 0; i < data.list.length; i++) {
            if(this.$route.meta.menuId==data.list[i].menuId){
              this.favorite = true
            }
          }
        })
      },

      // 收藏 OR 取消收藏
      favoriteFunction() {
        let userFavorite = {
          userId: this.$store.state.user.id,
          functionId: this.$route.meta.menuId,
        }
        if (this.favorite) {
          removeUserFavorite(userFavorite).then(({data}) => {
            this.$message.success(data.msg)
            this.favorite = false
          })
        } else {
          // 收藏
          saveUserFavorite(userFavorite).then(({data}) => {
            this.$message.success(data.msg)
            this.favorite = true
          })
        }
      },

      // 获取基础数据列表S
      getBaseList (val,type) {
        this.tagNo = val
        this.$nextTick(() => {
          let strVal = ''
          if (val === 201) {
            strVal = this.planOperator
            this.$refs.baseList.init(val, strVal)
          }
        })
      },

      /* 列表方法的回调 */
      getBaseData (val) {
        if (this.tagNo === 201) {
          this.planOperator = val.AdminID
          this.planOperatorName = val.AdminName
        }
      },

      //导出excel
      async createExportData() {
        this.searchData.limit = -1
        this.searchData.page = 1
        await eamWorkOrderSearchForDefect(this.searchData).then(({data}) => {
          this.exportList= data.page.list;
        })
        return this.exportList;
      },

      startDownload() {},

      finishDownload() {},

      fields() {
        let json = "{"
        this.columnList.forEach((item, index) => {
          if (index == this.columnList.length - 1) {
            json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\""
          } else {
            json += "\"" + item.columnLabel + "\"" + ":" + "\"" + item.columnProp + "\"" + ","
          }
        })
        json += "}"
        let s = eval("(" + json + ")")
        return s
      },

      // 获取数据列表
      getDataList () {
        this.searchData.limit = this.pageSize
        this.searchData.page = this.pageIndex
        eamWorkOrderSearchForDefect(this.searchData).then(({data}) => {
          if (data.code === 0) {
            this.dataList = data.page.list
            this.pageIndex = data.page.currPage
            this.pageSize = data.page.pageSize
            this.totalPage = data.page.totalCount
          }
          this.dataListLoading = false
        })
      },

      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

      cancelOrder(row){
        this.$confirm(`是否取消选定工单?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          cancelOrder(row).then(({data}) => {
            if (data && data.code === 0) {
              this.getDataList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                }
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      },

      selectFlag(row,index){
        if(row.status != '未开工'){
          return false;
        }else {
          return true;
        }
      },

      changeModel(){
        if(this.dataListSelections.length === 0){
          this.$alert('请勾选工单！', '错误', {
            confirmButtonText: '确定'
          })
          return false
        }
        this.planOperator=''
        this.planOperatorName=''
        this.changeModelFlag=true;
      },

      changeOrderOperator(){
        let inList=JSON.parse(JSON.stringify(this.dataListSelections));
        for (let i = 0; i <inList.length ; i++) {
          inList[i].planOperator=this.planOperator
        }
        changeOrderOperator(inList).then(({data}) => {
          if (data && data.code === 0) {
            this.changeModelFlag=false;
            this.getDataList()
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
              }
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },

      // 审核按钮
      checkModal(){
        if (!this.isAuth(this.$route.meta.menuId+":check")) {
          this.$message.warning('无审核权限！')
          return
        }
        this.submitData.site = this.detailData.site
        this.submitData.orderNo = this.detailData.orderNo
        this.submitData.planID = this.detailData.planID
        this.submitData.objectID = this.detailData.objectID
        this.submitData.planDesc = this.detailData.planDesc
        this.submitData.disposalMeasures = this.detailData.disposalMeasures
        this.submitData.defectID = this.detailData.defectID
        this.submitData.remark = this.detailData.remark
        this.submitData.difficultyLevel = ''
        this.submitData.difficultyRemark = ''
        if (this.detailData.status === '已完工') {
          this.$confirm(`是否取消审核?`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.submitData.status = '待审核'
            submitDefect(this.submitData).then(({data}) => {
              if (data && data.code === 0) {
                this.getDataList()
                this.detailData.status = '待审核'
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {}
                })
              } else {
                this.$alert(data.msg, '错误', {
                  confirmButtonText: '确定'
                })
              }
            })
          }).catch(() => {
          })
        } else {
          this.submitData.status = '已完工'
          this.submitModelFlag = true
        }
      },

      // 确认审核
      submitModel(){
        if(this.submitData.difficultyLevel === ''||this.submitData.difficultyLevel == null){
          this.$alert('请选择难度等级！', '提示', {
            confirmButtonText: '确定'
          })
          return false
        }
        submitDefect(this.submitData).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.detailData.status = '已完工'
            this.submitModelFlag = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
              }
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },

      // 详情
      reportModal (row) {
        this.detailData = {
          site: this.$store.state.user.site,
          orderNo: row.orderNo,
          planID: row.planID,
          objectID: row.objectID,
          objectDesc: row.objectDesc,
          status: row.status,
          remark: row.remark,
          workTime: row.workTime,
          result: row.result,
          defectID: row.defectID,
          defectDesc: row.defectDesc,
          actualOperatorName: row.actualOperatorName,
          planDate: row.planDate,
          reachDate: row.reachDate,
          actualDate: row.actualDate,
          planDesc: row.planDesc,
          disposalMeasures: row.disposalMeasures,
          handlingMethod: row.handlingMethod,
          functionType: row.functionType,
          resourceDesc: row.resourceDesc,
          feedBackDesc: row.feedBackDesc,
          faultReason: row.faultReason,
          preventiveMeasure: row.preventiveMeasure
        }
        this.detailModelFlag = true
      },

      updateResult () {
        if (!this.isAuth(this.$route.meta.menuId+":check")) {
          this.$message.warning('无审核权限！')
          return
        }
        if(this.detailData.reachDate === '' || this.detailData.reachDate == null){
          this.$message.warning('请选择到达时间！')
          return
        }
        if(this.detailData.actualDate === '' || this.detailData.actualDate == null){
          this.$message.warning('请选择实际执行时间！')
          return
        }
        if (this.detailData.reachDate > this.detailData.actualDate) {
          this.$message.warning('到达时间不能大于实际执行时间！')
          return
        }
        updateReportResult(this.detailData).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.detailModelFlag = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },

      /**
       * 查看故障图片
       */
      checkFaultImageModal (){
        this.descImages = [];
        let tempData = {
          site: this.$store.state.user.site,
          orderNo: this.detailData.orderNo,
          folder: 'reportFault',
        };
        searchFileUrl(tempData).then(({data}) => {
          if (data.code === 0) {
            for (let i = 0; i < data.rows.length; i++) {
              this.descImages.push(data.rows[i].url)
            }
          }else {
            this.$message.warning(data.msg);
          }
        })
        this.imageModalFlag = true
      },

      /**
       * 查看备件图片
       */
      checkSparPartImageModal (){
        this.descImages = [];
        let tempData = {
          site: this.$store.state.user.site,
          orderNo: this.detailData.orderNo,
          folder: 'reportSparPart',
        };
        searchFileUrl(tempData).then(({data}) => {
          if (data.code === 0) {
            for (let i = 0; i < data.rows.length; i++) {
              this.descImages.push(data.rows[i].url)
            }
          }else {
            this.$message.warning(data.msg);
          }
        })
        this.imageModalFlag = true
      },

      // 获取部门列表
      getDepartmentList(){
        // 先清空缓存选中
        this.$nextTick(() => this.$refs.departmentTable.clearSelection())
        // 拿到选中的部门编号
        let tempDataList = this.searchData.departmentName.split(';')
        // 查询部门
        getEmpyDeptList(this.departmentData).then(({data}) => {
          if (data && data.code === 0) {
            this.departmentList = data.rows
            this.departmentList.forEach(val => {
              // 回显选中的部门
              if (tempDataList.includes(val.deptName)) {
                this.$nextTick(() => this.$refs.departmentTable.toggleRowSelection(val, true))
              }
            })
            this.departmentModelFlag = true;
          }else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      },

      // 部门
      departmentClickRow(row){
        this.$refs.departmentTable.toggleRowSelection(row);
      },

      // 多选
      selectionDepartment (val) {
        this.departmentListSelections = val
      },

      getRowKeys(row) {
        //唯一值，一般都为id
        return row.deptId;
      },

      // 确认多选部门
      confirmDepartment(){
        this.searchData.departmentName = ''
        for (let i = 0; i < this.departmentListSelections.length; i++) {
          this.searchData.departmentName = this.searchData.departmentName + ";" + this.departmentListSelections[i].deptName
        }
        this.searchData.departmentName = this.searchData.departmentName.substring(1)
        this.departmentModelFlag = false
      },

      // 清空搜索框部门
      clearDepartmentName(){
        this.searchData.departmentName = ''
      }
    }
  }
</script>
