<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="140px" v-if="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="部门:">
              <el-select v-model="filterQuery.deptCode" placeholder="请选择部门" clearable="true"
                         @change="deptChange"
                         style="width: 90%"
              >
                <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" placeholder="请选择仓库" clearable="true"
                         style="width: 90%"
              >
                <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-col :span="8">
            <el-form-item class="query-form-item" label="任务单号:">
              <el-input v-model="filterQuery.orderId" placeholder="请输入任务单号" style="width: 90%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="预计结束时间时间:">
              <el-date-picker
                  :picker-options="pickerEdOptions"
                  v-model="edDateRange"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width: 90%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="开始时间:">
              <el-date-picker
                  :picker-options="pickerOptions"
                  v-model="actDateRange"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width: 90%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item class="query-form-item" label="结束时间:">
              <el-date-picker
                  :picker-options="pickerOptions"
                  v-model="endDateRange"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width: 90%"
              >
              </el-date-picker>
            </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" placeholder="请输入资产编码" style="width: 90%"
                        clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务状态:" class="query-form-item">
              <el-select v-model="filterQuery.status" style="width: 90%" placeholder="请选择任务状态" clearable>
                <el-option label="草稿" :value=0></el-option>
                <el-option label="未分配" :value=1></el-option>
                <el-option label="未巡检" :value=2></el-option>
                <el-option label="巡检中" :value=3></el-option>
                <el-option label="已巡检" :value=4></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
        </el-button-group>
      </div>

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

      <el-table v-loading="loading" :data="list" style="width: 100%" @current-change="handleDetail"   highlight-current-row border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="部门" prop="deptName" width="150"></el-table-column>
        <el-table-column label="仓库" prop="invName" width="150"></el-table-column>
        <el-table-column label="任务编号" prop="orderId" width="150"></el-table-column>
        <el-table-column label="资产编码" prop="code" width="150"></el-table-column>
        <el-table-column label="设备名称" prop="deviceName" width="150"></el-table-column>
        <el-table-column label="任务状态" prop="status" width="150">
          <template slot-scope="scope">
            <el-tag :type="statusFilterType(scope.row.status)">{{ statusMap[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="开始时间" prop="strartTime" width="150"></el-table-column>
        <el-table-column label="结束时间" prop="endTime" width="150"> </el-table-column>
        <el-table-column label="提醒时间" prop="remindTime" width="150"></el-table-column>
        <el-table-column label="巡检人" prop="inspectName" width="150"></el-table-column>
        <el-table-column label="预计结束时间时间" prop="expectedTime" width="150"></el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button
                type="text"
                size="small"
                @click.native.stop="startOrder(scope.row,5)"
            >详情
            </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-dialog
        title="巡检任务确认"
        :visible.sync="formVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="65%"
        v-if="formVisible"
    >
      <deviceInspectTakeAuditModify
          :inspectTake="deviceInspectTake"
          :type="type"
          :closeDialog="closeDialog"
      >
      </deviceInspectTakeAuditModify>
    </el-dialog>

    <el-card class="el-card">
      <el-table v-loading="loading" :data="delectList" style="width: 100%"  border highlight-current-row>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="巡检项目编码" prop="projectCode" width="300"></el-table-column>
        <el-table-column label="项目内容" prop="projectContent" width="500"></el-table-column>
        <el-table-column label="状态" prop="status" width="100">
          <template slot-scope="scope">
            <el-tag>{{ statusdetailMap[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="巡检图片" prop="inspectImage" width="300">
          <template slot-scope="scope">
            <div style="color: #1890ff;" @click="showImgViewer(scope.row)">{{ scope.row.inspectImage }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="datailSelect(scope.row)"
            >详情
            </el-button
            >
          </template>
        </el-table-column>


      </el-table>
    </el-card>

    <el-dialog
      title="巡检配置详情"
      :visible.sync="deviceProjectSelectVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="40%"
      v-if="deviceProjectSelectVisible"
    >
      <DeviceInspectTakeDetail
        :detailQuery="detailQuery"
        :closeDialog="closeDialog"
      >
      </DeviceInspectTakeDetail>
    </el-dialog>

    <el-image-viewer v-if="imgViewerVisible" style="z-index:9999" :on-close="closeImgViewer" :url-list="imgList"/>
  </div>
</template>

<script>
import {getDeptListByUser} from "@/api/auth/authDept";
import {findInvByUser} from "@/api/system/invSubWarehouse";
import deviceInspectTakeAuditModify from "@/views/dev/DeviceInspectTakeAuditModify"
import {getDeviceInspectTakeList,selectDeviceInspecTakeDetail} from "@/api/dev/deviceInspectTake";
import {previewImage, previewFile} from "@/api/purchase/supCompany";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import DeviceInspectTakeDetail  from "@/views/dev/DeviceInspectTakeDetail"


export default {
  name: "deviceInspectTake",
  data() {
    return {
      filterQuery: {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        code: null,
        page: 1,
        limit: 20,
      },
      imgViewerVisible:false,
      imgList: [],
      BASE_URL: process.env.VUE_APP_BASE_API,
      type:null,
      delectList:[],
      list: [],
      total: 0,
      deptList: [],
      invList: [],
      loading: false,
      formVisible: false,
      statusMap: {
        0: "草稿",
        1: "未分配",
        2: "未巡检",
        3: "巡检中",
        4: "已巡检",
      },
      statusdetailMap: {
        1: "未开始",
        2: "已检查",
        3: "异常",
        4: "不巡检",
      },
      formName: null,
      formMap: {
        add: "新增设备巡检任务",
        edit: "编辑设备巡检任务"
      },
      deviceInspectTake: {
        id: null,
        code: null,
        deptCode: null,
        invCode: null,
        deviceName: null,
        orderId: null,
        endTime: '',
        startTime: '',
      },
      deviceProjectSelectVisible: false,
      detailQuery: null,
      showSearch: true,
      actDateRange: [],
      endDateRange: [],
      edDateRange: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },

      pickerAuditOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      pickerEdOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  methods: {
    statusFilterType(status) {
      const statusMap = {
        0: "",
        1: "success",
        2: "warning",
        3: "danger",
      };
      return statusMap[status];
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        code: null,
        page: 1,
        limit: 20,
      };
      this.endDateRange = [];
      this.actDateRange = [];
      this.edDateRange = [];
      this.spaceList = [];
      this.getDeptList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      if (this.actDateRange != null) {
        this.filterQuery.ksStartDate = this.actDateRange[0];
        this.filterQuery.ksEndDate = this.actDateRange[1];
      } else {
        this.filterQuery.ksStartDate = null;
        this.filterQuery.ksEndDate = null;
      }
      if (this.endDateRange != null) {
        this.filterQuery.jsStartDate = this.endDateRange[0];
        this.filterQuery.jsEndDate = this.endDateRange[1];
      } else {
        this.filterQuery.jsStartDate = null;
        this.filterQuery.jsEndDate = null;
      }
      if (this.edDateRange != null) {
        this.filterQuery.yjjsStartDate = this.edDateRange[0];
        this.filterQuery.yjjsEndDate = this.edDateRange[1];
      } else {
        this.filterQuery.yjjsStartDate = null;
        this.filterQuery.yjjsEndDate = null;
      }
      getDeviceInspectTakeList(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
          this.delectList = []
        } 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.spaceList = [];
      this.filterQuery.fromInvSpaceCode = null;
      this.getSpaceList();
    },
    getInvList() {
      let params = {deptCode: this.filterQuery.deptCode};
      findInvByUser(params)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    startOrder(row,type) {
      this.formVisible = true;
      this.type = type
      this.deviceInspectTake = row;
    },
    handleDetail(row){
      this.selectDeviceInspecTakeDetail({taskOrderIdFk:row.orderId})
    },
    selectDeviceInspecTakeDetail(obj) {
      selectDeviceInspecTakeDetail(obj).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.delectList = res.data || [];
        } else {
          this.$message.error(res.message);
          this.delectList = [];
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.delectList = [];
      })
    },
    closeDialog() {
      this.formVisible = false;
      this.getList();
    },
    datailSelect(row){
      this.detailQuery = row
      this.deviceProjectSelectVisible = true
    },

    showImgViewer(row) {
      if (row.inspectImage.substr(-1) == ',') {
        row.inspectImage = row.inspectImage.slice(0, row.inspectImage.length - 1);
      }
      this.certFileUrl = this.BASE_URL + "/udiwms/image/register/file/getImage?type=image2&name=";
      this.imgList = [];
      previewImage({imageUrl: row.inspectImage, certFileUrl: this.certFileUrl}).then(response => {
        if (response.code === 20000) {
          this.imgList = response.data;
        }
        console.log(this.imgList)
        this.imgViewerVisible = true;
      });
      const m = (e) => {
        e.preventDefault()
      };
      document.body.style.overflow = 'hidden';
      document.addEventListener("touchmove", m, false); // 禁止页面滑动

    },
    closeImgViewer() {
      this.imgViewerVisible = false;
      const m = (e) => {
        e.preventDefault()
      };
      document.body.style.overflow = 'auto';
      document.removeEventListener("touchmove", m, true);
    },

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

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

