<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务标题" prop="workTitle">
        <el-input
          v-model="queryParams.workTitle"
          placeholder="请输入任务标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="workType">
        <el-select v-model="queryParams.workType" placeholder="请选择任务类型" clearable size="small">
          <el-option label="入户安检" value="1" />
          <el-option label="巡检" value="2" />
          <!--<el-option label="报警巡查" value="3" />-->
          <el-option label="其他" value="4" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务状态" prop="workStatus">
        <el-select v-model="queryParams.workStatus" placeholder="请选择任务状态" clearable size="small">
          <el-option label="派发中" value="0" />
          <el-option label="已接单" value="1" />
          <el-option label="已反馈" value="2" />
          <el-option label="已归档" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:order:add']"
          v-if="roleType != 'inpector'"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:order:export']"
          v-if="roleType != 'inpector'"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="任务标题" align="center" prop="workTitle">
        <template slot-scope="scope">
          <span style="margin-left: 10px;display: inline-block">{{ scope.row.workTitle }}</span>
          <el-popover
            placement="right"
            title="督办信息"
            width="350"
            trigger="click"
            v-if="null!=scope.row.supervisorInfoList">
            <div class="timeline">
              <el-timeline>
                <el-timeline-item
                  v-for="(activity, index) in scope.row.supervisorInfoList"
                  :key="index"
                  :timestamp="activity.createTime">
                  {{activity.supervisorContent}}
                </el-timeline-item>
              </el-timeline>
            </div>
            <!--<el-button slot="reference">click 激活</el-button>-->
            <div style="display:inline-block;position:relative" slot="reference">
              <img src="@/assets/image/start.svg" style="position:absolute;top:-15px;left:0;width: 20px;height: 20px"/>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="任务类型" align="center" prop="workType">
        <template slot-scope="scope">
          <span v-if="scope.row.workType == 1">入户安检</span>
          <span v-if="scope.row.workType == 2">巡检</span>
         <!-- <span v-if="scope.row.workType == 3">报警巡查</span>-->
          <span v-if="scope.row.workType == 4">其他</span>
        </template>
      </el-table-column>
     <!-- <el-table-column label="任务内容" align="center" prop="workContent" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="创建单位" align="center" prop="workCreateEnterpriseName" />
      <el-table-column label="执行单位" align="center" prop="workAssignEnterproseName" />
      <el-table-column label="执行人" align="center" prop="workAssignMan" />
      <el-table-column label="任务状态" align="center" prop="workStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.workStatus == 0" style="color: #ff7272">派发中</span>
          <span v-if="scope.row.workStatus == 1" style="color: #ffba00">已接单</span>
          <span v-if="scope.row.workStatus == 2" style="color: #1890ff">已反馈</span>
          <span v-if="scope.row.workStatus == 3" style="color: #30B46B">已归档</span>
        </template>
      </el-table-column>>
      <!--<el-table-column label="巡检时间" align="center" prop="inspectionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inspectionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡检路线" align="center" prop="inspectionRoute" />-->
      <!--<el-table-column label="问题描述" align="center" prop="problemDescription" />
      <el-table-column label="问题图片" align="center" prop="pictureUrl" style="text-align:center;">
        <template slot-scope="scope">
          <img :src="scope.row.iconUrl" style="width: 20%;vertical-align:middle;cursor:pointer;" @click="showPicture(scope.row)"/>
          <el-image :ref="'a'+scope.row.workId" :src="scope.row.iconUrl" v-show="false" :preview-src-list="[scope.row.iconUrl]" v-if="scope.row.iconUrl != '' && scope.row.iconUrl != null"></el-image>
        </template>
      </el-table-column>-->
      <!--<el-table-column label="图片路径" align="center" prop="iconUrl" />-->
     <!-- <el-table-column label="整改方案" align="center" prop="rectificationPlan" />
      <el-table-column label="整改结果" align="center" prop="rectificationResult" />-->
      <!--<el-table-column label="责任单位" align="center" prop="responsibleUnit" />
      <el-table-column label="责任人员" align="center" prop="responsiblePerson" />-->
      <el-table-column label="截止日期" align="center" prop="expiryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="150%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="workDetail(scope.row)"
            v-hasPermi="['system:order:edit']"
            key="detail"
          >详情</el-button>
          <el-button v-if="('zhengfu'== roleType || (('enterprise'== roleType ||  'doubleRole' == roleType) && scope.row.workCreateEnterpriseId == $store.state.user.enterpriseId))&& scope.row.workStatus == '0'" key="edit"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order:remove']"
            v-if="('zhengfu'== roleType || (('enterprise'== roleType ||  'doubleRole' == roleType) && scope.row.workCreateEnterpriseId == $store.state.user.enterpriseId))&& scope.row.workStatus == '0'"
            key="detele"
          >删除</el-button>
          <el-button v-if="'inpector'!= roleType && (scope.row.workAssignManId == '' || scope.row.workAssignManId == null) && scope.row.workStatus == '0' && scope.row.workCreateEnterpriseId == $store.state.user.enterpriseId"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="workIssue(scope.row)"
                     v-hasPermi="['system:order:edit']"
                     key="work"
          >任务下发</el-button>
          <el-button v-if=" ('inpector'== roleType || 'doubleRole' == roleType) && (scope.row.workAssignManId == $store.state.user.userId) && scope.row.workStatus == '0'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="takingOrder(scope.row)"
                     v-hasPermi="['system:order:edit']"
                     key="recieve"
          >接单</el-button>
          <el-button v-if="'zhengfu'!= roleType && (scope.row.workStatus == '0' || scope.row.workStatus == '1')"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="feedbookWork(scope.row)"
                     v-hasPermi="['system:order:edit']"
                     key="feedbook"
          >反馈</el-button>
          <el-button v-if="'inpector'!= roleType && scope.row.workStatus == '2' && scope.row.workCreateEnterpriseId == $store.state.user.enterpriseId"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="sortWork(scope.row)"
                     v-hasPermi="['system:order:edit']"
                     key="sort"
          >归档</el-button>
          <el-button v-if="'zhengfu'!= roleType && scope.row.workStatus != '3'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="hiddenFeed(scope.row)"
                     v-hasPermi="['system:order:edit']"
                     key="hidden"
          >隐患反馈</el-button>
          <el-button v-if="'zhengfu'== roleType && scope.row.workStatus != '2' && scope.row.workStatus != '3' && scope.row.workCreateEnterpriseId == $store.state.user.enterpriseId"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="supervisor(scope.row)"
                     v-hasPermi="['system:order:edit']"
                     key="supervisor"
          >督办</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改燃气任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" @closed="dialogClose" @open="dialogOpen" :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="11">
          <el-form-item label="任务标题" prop="workTitle">
            <el-input v-model="form.workTitle" placeholder="请输入任务标题" :disabled="isDetail"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务类型" prop="workType">
            <el-select v-model="form.workType" placeholder="请选择任务类型" style="width: 100%" :disabled="isDetail">
              <el-option label="入户安检" value="1" />
              <el-option label="巡检" value="2" />
              <!--<el-option label="报警巡查" value="3" />-->
              <el-option label="其他" value="4" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="任务内容" prop="workContent">
              <!--<editor v-model="form.workContent" :min-height="192" :readOnly="!s"/>-->
            <editor v-if="isOpen" v-model="form.workContent" :min-height="192" :readOnly="isDetail"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="执行单位" prop="workAssignEnterproseId">
            <!--<el-input v-model="form.workAssignEnterproseName" placeholder="请输入执行单位名称" />-->
            <el-select v-model="form.workAssignEnterproseId" placeholder="请选择执行单位名称" style="width: 100%" @change="selectworkAssignEnterprose($event)" :disabled="isDetail || isEnterproser">
              <el-option
                v-for="item in enterprises"
                :key="item.enterpriseId"
                :label="item.enterpriseName"
                :value="item.enterpriseId"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="执行人" prop="workAssignMan">
            <!--<el-input v-model="form.workAssignManId" placeholder="请输入执行人" />-->
            <el-select v-model="form.workAssignManId" placeholder="请输入执行人" style="width: 100%" @change="selectInspection($event,'edit')" :disabled="isDetail" clearable>
              <el-option
                v-for="item in inspectors"
                :key="item.userId "
                :label="item.nickName"
                :value="item.userId "
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11" v-show="isDetail">
          <el-form-item label="创建单位" prop="workCreateEnterpriseName">
            <el-input v-model="form.workCreateEnterpriseName" placeholder="请输入创建单位名称" :disabled="isDetail"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="巡检时间" prop="inspectionDate" v-show="form.workType=='2'">
            <el-date-picker clearable size="small"
                            v-model="form.inspectionDate"
                            type="date"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择巡检时间" style="width: 100%" :disabled="isDetail">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="巡检路线" prop="inspectionRoute" v-show="form.workType=='2'">
            <el-input v-model="form.inspectionRoute" type="textarea" placeholder="请输入巡检路线" :disabled="isDetail"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="问题描述" prop="problemDescription" v-show="form.workType=='3'">
            <el-input v-model="form.problemDescription" type="textarea" placeholder="请输入问题描述" :disabled="isDetail"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="整改方案" prop="rectificationPlan" v-show="form.workType=='3' && isDetail && (form.workStatus =='2' || form.workStatus =='3')">
             <el-input v-model="form.rectificationPlan" type="textarea" placeholder="请输入整改方案" :disabled="isDetail"/>
           </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
          <el-form-item label="反馈图片" prop="iconUrl" v-show="isDetail && (form.workStatus =='2' || form.workStatus =='3') && form.iconUrl !='' && form.iconUrl != null">
            <!--<MyFileUpload
              listType="picture-card"
              @resFun="getFileInfo"
              @remove="listRemove"
              :fileArr="fileList"
              :readOnly= "isDetail"
            />
            <el-input v-show="false" disabled v-model="form.iconUrl"></el-input>-->
            <div style="width: 20%;">
              <el-image :src="form.iconUrl" :preview-src-list="[form.iconUrl]" style="width: auto;height: auto;" :z-index= "3000"></el-image>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="23">
         <el-form-item label="反馈信息" prop="rectificationResult" v-show="isDetail && (form.workStatus =='2' || form.workStatus =='3')">
           <el-input v-model="form.rectificationResult" type="textarea" placeholder="请输入整改结果" :disabled="isDetail"/>
         </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="11">
          <el-form-item label="责任单位" prop="responsibleUnit">
            <el-input v-model="form.responsibleUnit" placeholder="请输入责任单位" :disabled="isDetail"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="责任人员" prop="responsiblePerson">
            <el-input v-model="form.responsiblePerson" placeholder="请输入责任人员" :disabled="isDetail"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="截止日期" prop="expiryDate">
            <el-date-picker clearable size="small"
                            v-model="form.expiryDate"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss"
                            placeholder="选择截止日期" style="width: 100%" :disabled="isDetail">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-if="supervisorList.length > 0 && isDetail">
      <div class="hiddenInfo">督办信息</div>
      <el-timeline ref="timeline">
        <el-timeline-item
          v-for="(activity, index) in supervisorList"
          :key="index"
          :timestamp="activity.createTime"
          v-show='index>1?false:true'>
          <el-card>
            {{activity.supervisorContent}}
          </el-card>
        </el-timeline-item>
      </el-timeline>
      <div style="width: 95%;position: relative;" @click="changeDisplay()" v-if="supervisorList.length >2">
        <!-- <el-button type="primary"> -->
        <i class="el-icon-arrow-down" v-if="isDisplay==false"
           style="margin-left: 100px;color: #909399;font-size: 14px;">
          <span style="text-decoration:underline">显示全部</span>
        </i>
        <i class="el-icon-arrow-up" v-else style="color: #909399;font-size: 14px;margin-left: 100px;">
          <span style="text-decoration:underline">收起</span>
        </i>
        <!-- </el-button> -->

      </div>
    </div>
    <div v-if="hiddenInfoList.length > 0">
      <div class="hiddenInfo">隐患信息</div>
      <HiddenTrouble
        :hiddenList="hiddenInfoList"/>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" v-show="!isDetail">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
    <!-- 任务下发 -->
    <el-dialog :title="title" :visible.sync="workOpen" width="900px" append-to-body>
      <el-form ref="workForm" :model="workForm" :rules="workRules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务标题" prop="workTitle">
             <!-- <el-input v-model="workForm.workTitle" placeholder="请输入任务标题"/>-->
              <span>{{ workForm.workTitle }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="workType">
              <span v-if="workForm.workType == '1'">入户安检</span>
              <span v-if="workForm.workType == '2'">巡检</span>
              <!--<span v-if="workForm.workType == '3'">报警巡查</span>-->
              <span v-if="workForm.workType == '4'">其他</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="执行单位" prop="workAssignEnterproseId">
              <!--<el-input v-model="form.workAssignEnterproseName" placeholder="请输入执行单位名称" />-->
              <el-select v-model="workForm.workAssignEnterproseId" placeholder="请选择执行单位名称" style="width: 350px" @change="selectworkAssignEnterprose($event)" :disabled="isEnterproser">
                <el-option
                  v-for="item in enterprises"
                  :key="item.enterpriseId"
                  :label="item.enterpriseName"
                  :value="item.enterpriseId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行人" prop="workAssignMan">
              <!--<el-input v-model="form.workAssignManId" placeholder="请输入执行人" />-->
              <el-select v-model="workForm.workAssignManId" placeholder="请输入执行人" style="width: 350px" @change="selectInspection($event,'work')">
                <el-option
                  v-for="item in inspectors"
                  :key="item.userId "
                  :label="item.nickName"
                  :value="item.userId "
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitWorkForm">确 定</el-button>
        <el-button @click="cancelWorkForm">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 反馈 -->
    <el-dialog :title="title" :visible.sync="feedBookOpen" width="900px" append-to-body @close="cancelFeedBook">
      <el-form ref="feedBookForm" :model="feedBookForm" :rules="feedBookRules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务标题" prop="workTitle">
              <!--<el-input v-model="feedBookForm.workTitle" placeholder="请输入任务标题"/>-->
              <span>{{feedBookForm.workTitle}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="workType">
              <span v-if="feedBookForm.workType == '1'">入户安检</span>
              <span v-if="feedBookForm.workType == '2'">巡检</span>
             <!-- <span v-if="feedBookForm.workType == '3'">报警巡查</span>-->
              <span v-if="feedBookForm.workType == '4'">其他</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="整改方案" prop="rectificationPlan" v-if="feedBookForm.workType=='3'">
          <el-input v-model="feedBookForm.rectificationPlan" type="textarea" placeholder="请输入整改方案" />
        </el-form-item>

        <el-form-item label="反馈图片" prop="iconUrl">
          <MyFileUpload
            listType="picture-card"
            @resFun="getFileInfo"
            @remove="listRemove"
            :fileArr="fileList"
          />
          <el-input v-show="false" disabled v-model="feedBookForm.iconUrl"></el-input>
        </el-form-item>

         <el-form-item label="反馈信息" prop="rectificationResult">
           <el-input v-model="feedBookForm.rectificationResult" type="textarea" placeholder="反馈信息" />
         </el-form-item>
        <el-form-item label="隐患信息" prop="hiddenInfo">
          <HiddenTrouble
            :hiddenList="feedBookForm.hiddenList"
            :workId="feedBookForm.workId"
            :isView="false"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFeedBookForm">确 定</el-button>
        <el-button @click="cancelFeedBook">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 隐患反馈信息 -->
    <el-dialog :title="title" :visible.sync="hiddenFeedOpen" width="900px" append-to-body @close="cancelHiddenFeedForm">
      <el-form ref="hiddenFeedForm" :model="hiddenFeedForm" :rules="hiddenFeedRules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务标题" prop="workTitle">
              <span>{{hiddenFeedForm.workTitle}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="workType">
              <span v-if="hiddenFeedForm.workType == '1'">入户安检</span>
              <span v-if="hiddenFeedForm.workType == '2'">巡检</span>
              <!-- <span v-if="feedBookForm.workType == '3'">报警巡查</span>-->
              <span v-if="hiddenFeedForm.workType == '4'">其他</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="隐患信息" prop="hiddenInfo">
          <HiddenFeedTrouble
            :workId="hiddenFeedForm.workId"
            :isView="false"
             ref="hiddenChild"
            v-if="hiddenFeedOpen"/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHiddenFeedForm">确 定</el-button>
        <el-button @click="cancelHiddenFeedForm">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 督办 -->
    <el-dialog :title="title" :visible.sync="supervisorOpen" width="900px" append-to-body @close="cancelSupervisor">
      <el-form ref="supervisorForm" :model="supervisorForm" :rules="supervisorRules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务标题" prop="workTitle">
              <!--<el-input v-model="feedBookForm.workTitle" placeholder="请输入任务标题"/>-->
              <span>{{supervisorForm.workTitle}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="workType">
              <span v-if="supervisorForm.workType == '1'">入户安检</span>
              <span v-if="supervisorForm.workType == '2'">巡检</span>
              <span v-if="supervisorForm.workType == '4'">其他</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="督办意见" prop="supervisorContent">
          <el-input v-model="supervisorForm.supervisorContent" type="textarea" placeholder="督办意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSupervisorForm">确 定</el-button>
        <el-button @click="cancelSupervisor">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/operationMonitor/order";
import { hiddenTroubleList } from "@/api/operationMonitor/hiddenTrouble"
import { addSupervisorInfo, supervisorInfoList } from "@/api/operationMonitor/supervisor"
import Editor from '@/components/Editor';
import MyFileUpload from '@/components/MyFileUpload';
import { enterpriseLists } from "@/api/regulation/info";
import { getInspectionUsers } from "@/api/system/user";
import ScrollPane from "../../../layout/components/TagsView/ScrollPane";
import HiddenTrouble from "./components/HiddenTrouble";
import HiddenFeedTrouble from "./components/HiddenFeedTrouble"
export default {
  name: "Order",
  components: {
    ScrollPane,
    Editor,
    MyFileUpload,
    HiddenTrouble,
    HiddenFeedTrouble
  },
  data() {
    const validateHiddenFeedInfo = (rule, value, callback) => {
      if (this.$refs.hiddenChild.checkChildData()) {
        callback(new Error("隐患信息不能为空！"));
      } else {
        callback();
      }
    };
    return {
      isOpen:false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      detailDialogVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 燃气任务表格数据
      orderList: [
        {activities:[]}
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //企业选项
      enterprises: [],
      //巡检员
      inspectors:[],
      // 上传文件列表
      fileList: [],
      //详情
      isDetail: false,
      //是否为企业用户
      isEnterproser: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workTitle: null,
        workType: null,
        workContent: null,
        workCreateEnterpriseName: null,
        workCreateEnterpriseId: null,
        workAssignEnterproseName: null,
        workAssignEnterproseId: null,
        workAssignManId: null,
        workAssignMan: null,
        workStatus: null,
        inspectionDate: null,
        inspectionRoute: null,
        problemDescription: null,
        iconUrl: null,
        rectificationPlan: null,
        rectificationResult: null,
        responsibleUnit: null,
        responsiblePerson: null,
        expiryDate: null,
        isDel: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workTitle: [
          { required: true, message: "任务标题", trigger: "blur" }
        ],
        workType: [
          { required: true, message: "任务类型", trigger: "blur" }
        ],
        workContent: [
          { required: true, message: "任务内容", trigger: "blur" }
        ],
        workAssignEnterproseId: [
          { required: true, message: "执行单位", trigger: "blur" }
        ],
        expiryDate: [
          { required: true, message: "截止时间", trigger: "blur" }
        ]
      },
      //任务下发表单校验
      workRules: {
        workAssignEnterproseId: [
          { required: true, message: "执行单位", trigger: "blur" }
        ],
        workAssignMan: [
          { required: true, message: "执行人", trigger: "blur" }
        ]
      },
      //反馈校验
      feedBookRules: {
        iconUrl: [
          { required: true, message: "反馈图片", trigger: "change" }
        ],
        rectificationResult:[
          { required: true, message: "反馈信息", trigger: "blur" }
        ]
      },
      roleType: "zhengfu",
      workForm:{},
      workOpen: false,
      feedBookForm: {
        iconUrl: "",
        hiddenList:[]
      },
      feedBookOpen: false,
      //详情隐患信息
      hiddenInfoList:[],
      hiddenFeedOpen:false,
      hiddenFeedForm:{
        hiddenList:[]
      },
      //反馈校验
      hiddenFeedRules: {
        hiddenInfo: [
          { type: "array", validator: validateHiddenFeedInfo, required: true }
        ]
      },
      //督办form
      supervisorForm: {},
      //督办弹框
      supervisorOpen: false,
      //督办校验
      supervisorRules:{
        supervisorContent: [
          { required: true, message: "督办内容", trigger: "blur" }
        ]
      },
      supervisorList:[],
      isDisplay: false,
      showAndHide: false
    };
  },
  created() {
    window.func=()=>{
      this.isDetail =!this.isDetail;
    }
    this.getList();
    let enterpriseId = this.$store.state.user.enterpriseId;
    let post = this.$store.state.user.posts.find(item => item.postCode === "se");
    if(-2 != enterpriseId){
      if(post && this.$store.state.user.posts.length <=1){
        this.roleType = "inpector";
      }else if(post && this.$store.state.user.posts.length >1){
        this.roleType = "doubleRole";
      }else{
        this.roleType = "enterprise";
      }
    }

  },
  methods: {

    dialogClose(){
      this.isOpen=false;
    },
    dialogOpen(){
      this.isOpen=true;
    },
    /** 查询燃气任务列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
       /* const param = response.rows;
        param.forEach(item =>{
          this.supervisorData(item);
        })
        this.orderList = param;
        console.log(this.orderList,"fdsfdsfdfdsfd========")*/
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workId: null,
        workTitle: null,
        workType: null,
        workContent: null,
        workCreateEnterpriseName: null,
        workCreateEnterpriseId: null,
        workAssignEnterproseName: null,
        workAssignEnterproseId: null,
        workAssignManId: null,
        workAssignMan: null,
        workStatus: "0",
        inspectionDate: null,
        inspectionRoute: null,
        problemDescription: null,
        iconUrl: null,
        rectificationPlan: null,
        rectificationResult: null,
        responsibleUnit: null,
        responsiblePerson: null,
        expiryDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.workId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isDetail = false;
      this.title = "添加巡检巡查任务";
      this.getEnterpriseLists();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workId = row.workId || this.ids
      this.isDetail = false;
      getOrder(workId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改燃气任务";
        this.getHiddenInfos({workId : workId});
        this.getEnterpriseLists();
        this.getInspectionUserList(response.data.workAssignEnterproseId);
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.workId != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workIds = row.workId || this.ids;
      this.$confirm('是否确认删除燃气任务名称为"' + row.workTitle + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          //return delOrder(workIds);
          updateOrder({workId: workIds,isDel: '1'})
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有燃气任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    getFileInfo(res){
      this.feedBookForm.iconUrl = res.url;
      //this.$set(this.feedBookForm,'iconUrl',res.url)
    },
    listRemove(e) {
      this.form.iconUrl = "";
      this.fileList = [];
    },
    showPicture(row){
      this.$refs['a'+row.workId].showViewer = true;
    },
    //执行单位
    getEnterpriseLists(){
      const param = {};
      this.judgeOperateType(param);
      enterpriseLists(param).then(response => {
         this.enterprises = response.rows;
      });
    },
    //根据用户角色和操作类型设置执行人是否可选
    judgeOperateType(param){
      if(this.roleType != "zhengfu"){
        param.enterpriseId = this.$store.state.user.enterpriseId;
        if(this.title == "添加巡检巡查任务"){
          this.form.workAssignEnterproseId = this.$store.state.user.enterpriseId;
          this.isEnterproser = true;
        }
        if(this.title == "任务下发"){
          this.isEnterproser = true;
        }
        this.getInspectionUserList(this.$store.state.user.enterpriseId);
      }
    },
    //获取巡检员
    getInspectionUserList(enterpriseId){
      getInspectionUsers(enterpriseId,"se").then(response =>{
        this.inspectors = response.data;
      })
    },
    //选择执行单位
    selectworkAssignEnterprose(enterpriseId){
      this.form.workAssignManId = "";
      this.workForm.workAssignManId = "";
      this.workForm.workAssignMan="";
      this.form.workAssignMan = "";
      this.getInspectionUserList(enterpriseId);
      let enterpriseName = this.enterprises.find(val=>val.enterpriseId == enterpriseId).enterpriseName;
      this.form.workAssignEnterproseName = enterpriseName;
    },
    //获取执行人
    selectInspection(userId,type){
      if(userId){
        let nickName = this.inspectors.find(val=>val.userId == userId).nickName;
        if("edit" == type){
          this.form.workAssignMan = nickName;
        }else{
          this.workForm.workAssignMan = nickName;
        }
      }else{
        if("edit" == type){
          this.form.workAssignMan = "";
        }else{
          this.workForm.workAssignMan = "";
        }
      }
    },
    /*//选择任务类型
    selectWorkType(workType){
      if("2" == workType){
        this.inspectorInfoIsShow = true;
      }else{
        this.inspectorInfoIsShow = false;
      }
    },*/
    //详情
    workDetail(row){
      getOrder(row.workId).then(response => {
        this.isDetail=true;
        this.open = true;
        this.title = "燃气任务详情";
        this.form = response.data;
        /*if(this.form.workStatus == '2' || this.form.workStatus == '3'){
          const param = {};
          param.workId = this.form.workId;

        }*/
        //查询隐患信息
        this.getHiddenInfos({workId : this.form.workId});
        this.getEnterpriseLists();
        this.getInspectionUserList(response.data.workAssignEnterproseId);
        this.supervisorData(this.form.workId);
      });
    },
    //任务下发
    workIssue(row){
      this.title = "任务下发";
      this.workForm = {};
      this.workForm.workId = row.workId;
      this.workForm.workTitle = row.workTitle;
      this.workForm.workType = row.workType;
      if(row.workAssignEnterproseId){
        this.workForm.workAssignEnterproseId = row.workAssignEnterproseId;
        this.workForm.workAssignEnterproseName = row.workAssignEnterproseName;
      }
     /* if(row.workAssignManId){
        this.workForm.workAssignManId =  row.workAssignManId;
        this.workForm.workAssignMan = row.workAssignMan;
      }*/
      //this.workForm = row;
      //this.workForm.workStatus = '1';
      this.workOpen = true;
      this.getEnterpriseLists();
      this.getInspectionUserList(row.workAssignEnterproseId);
    },
    //接单
    takingOrder(row){
      this.$confirm('是否确认接单?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
        this.workForm = {};
        this.workForm.workId = row.workId;
        this.workForm.workStatus = '1';
        if (this.workForm.workId != null) {
          updateOrder(this.workForm);
        }
      }).then(response => {
        this.getList();
        this.msgSuccess("接单成功");
      }).catch(() => {});
    },
    //反馈
    feedbookWork(row){
      this.title = "反馈信息";
      this.feedBookOpen = true;
      //this.feedBookForm = {};
      this.feedBookForm.workId = row.workId;
      this.feedBookForm.workTitle = row.workTitle;
      this.feedBookForm.workType = row.workType;
      /*if(row.iconUrl){
        this.feedBookForm.iconUrl = row.iconUrl;
      }*/
      /*if(row.rectificationPlan){
        this.feedBookForm.rectificationPlan =  row.rectificationPlan;
      }
      if(row.rectificationResult){
        this.feedBookForm.rectificationResult =  row.rectificationResult;
      }*/
      //this.feedBookForm = row;
      this.getEnterpriseLists();
    },
    //归档
    sortWork(row){
      this.$confirm('是否确认归档?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.workForm = {};
      this.workForm.workId = row.workId;
      this.workForm.workStatus = '3';
      if (this.workForm.workId != null) {
        updateOrder(this.workForm);
      }
      }).then(response => {
        this.getList();
        this.msgSuccess("归档成功");
      }).catch(() => {});
    },
    //任务下发提交
    submitWorkForm() {
      this.$refs["workForm"].validate(valid => {
        if (valid) {
          if (this.workForm.workId != null) {
            updateOrder(this.workForm).then(response => {
              this.msgSuccess("任务下发成功");
              this.workOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    //任务下发取消
    cancelWorkForm(){
      this.workOpen = false;
      this.workForm = {};
    },
    //反馈提交
    submitFeedBookForm(){
      this.$refs["feedBookForm"].validate(valid => {
        if (valid) {
          if (this.feedBookForm.workId != null) {
            this.feedBookForm.workStatus = '2';
            /*const param ={...this.feedBookForm};
            param.workStatus = '2';*/
            updateOrder(this.feedBookForm).then(response => {
              if(response.code = 200){
                this.msgSuccess("反馈成功");
                this.feedBookOpen = false;
                this.getList();
              }
              /*if(!this.feedBookOpen){
                this.getList();
              }*/
              //this.getList();
            });
          }
        }
      });
    },
    //反馈取消
    cancelFeedBook(){
      this.feedBookOpen = false;
      this.feedBookForm = {};
      this.fileList = [];
    },
    //获取隐患信息
    getHiddenInfos(param){
      hiddenTroubleList(param).then(res =>{
        this.hiddenInfoList = res.data;
        /*this.hiddenInfoList.forEach(item=>{
          item.reportPersonName = item.reportPerson;
        })*/
      });
    },
    //隐患信息反馈
    hiddenFeed(row){
      this.title = "隐患反馈信息";
      this.hiddenFeedOpen = true;
      this.hiddenFeedForm.workId = row.workId;
      this.hiddenFeedForm.workTitle = row.workTitle;
      this.hiddenFeedForm.workType = row.workType;
    },
    //隐患反馈提交
    submitHiddenFeedForm(){
      this.$refs["hiddenFeedForm"].validate(valid => {
        if (valid) {
          this.msgSuccess("隐患信息反馈成功");
          this.hiddenFeedOpen = false;
          this.getList();
        }
      });
    },
    //隐患反馈取消
    cancelHiddenFeedForm(){
      this.hiddenFeedOpen = false;
      this.hiddenFeedForm = {};
      this.fileList = [];
    },
    //督办
    supervisor(row){
      this.title = "任务督办";
      this.supervisorOpen = true;
      this.supervisorForm.workId = row.workId;
      this.supervisorForm.workTitle = row.workTitle;
      this.supervisorForm.workType = row.workType;
    },
    //督办提交
    submitSupervisorForm(){
      this.$refs["supervisorForm"].validate(valid => {
        if (valid) {
          this.supervisorForm.createBy = this.$store.state.user.userId;
          addSupervisorInfo(this.supervisorForm).then(res =>{
            this.msgSuccess("督办成功");
            this.supervisorOpen = false;
            this.getList();
          });
        }
      });
    },
    //督办取消
    cancelSupervisor(){
      this.supervisorOpen = false;
      this.supervisorForm = {};
    },
    //查询督办时间线
    supervisorData(workId){
      supervisorInfoList({workId: workId}).then(res =>{
          this.supervisorList = res.data;
      });
    },
    // 点击按钮显示隐藏
    changeDisplay() {
      this.isDisplay = !this.isDisplay
      let $timeline = this.$refs.timeline;
      if (!this.showAndHide) {
        for (let i = 0; i < $timeline.$children.length; i++) {
          if (i > 1) {
            $timeline.$children[i].$el.style.display = "block";
          }
        }
        this.showAndHide = true;
      } else {
        for (let i = 0; i < $timeline.$children.length; i++) {
          if (i > 1) {
            $timeline.$children[i].$el.style.display = "none";
          }
        }
        this.showAndHide = false;
      }


      //$timeline.toggleRowExpansion(row,true)
    }
  }
};
</script>
<style lang="scss" scoped>
  .hiddenInfo{
    font-weight: 700;
    font-size: 18px;
    margin-left: 12px;
    margin-bottom: 9px;
  }
  .timeline{
    width: 330px;
    height: 240px;
    padding: 15px;
    overflow-y: auto;
    &::-webkit-scrollbar {
      width: 5px;
      background:  #dfe4ed;
      position: absolute;
      top: 0;
    }
    &::-webkit-scrollbar-thumb {
      /*滚动条里面小方块*/
      // border-radius: 10px;
      // box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      background: #cccccc;
      border-radius: 5px;
    }
  }
</style>
