<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="140px" v-show="showSearch">
        <el-row>

          <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-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.type" style="width: 90%" placeholder="请选择计划级别" clearable>
                <el-option label="巡检" :value=1></el-option>
                <el-option label="养护" :value=2></el-option>
                <el-option label="报修" :value=3></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-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-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 type="primary" icon="el-icon-plus" @click="addTake">新增</el-button>
        </el-button-group>
      </div>

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

      <el-table v-loading="loading" :data="list" style="width: 100%" @current-change="handleDetail"   highlight-current-row border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="任务编号" prop="orderId"></el-table-column>
        <el-table-column label="资产编码" prop="code"></el-table-column>
        <el-table-column label="设备名称" prop="deviceName"></el-table-column>
        <el-table-column label="任务类型" prop="repairType">
          <template slot-scope="scope">
            <el-tag>{{ repairTypeMap[scope.row.type] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="任务状态" prop="status">
          <template slot-scope="scope">
            <el-tag>{{ statusMap[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="开始时间" prop="strartTime"></el-table-column>
        <el-table-column label="结束时间" prop="endTime"></el-table-column>
        <el-table-column label="提醒时间" prop="remindTime"></el-table-column>
        <el-table-column label="巡检人" prop="inspectName"></el-table-column>
        <el-table-column label="预计结束时间时间" prop="expectedTime"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="startOrder(scope.row,2)"
            >编辑
            </el-button
            >
            <el-button
                type="text"
                size="small"
                @click.native.stop="startOrder(scope.row,3)"
            >详情
            </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"-->
    <!--&gt;-->
    <!--  <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-dialog
      :title="formMap[formName]"
      :visible.sync="addTakeDetial"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="65%"
      v-if="addTakeDetial"
    >
      <deviceInspectTakeAdd
        :inspectTake="deviceInspectTake"
        :type="type"
        :formName="formName"
        :closeDialog="closeDialogTake"
      >
      </deviceInspectTakeAdd>
    </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/inventory/DeviceInspectTakeAuditModify"
import {getDeviceInspectTakeList,selectDeviceInspecTakeDetail} from "@/api/inventory/deviceInspectTake";
import {previewImage, previewFile} from "@/api/purchase/supCompany";
import ElImageViewer from "element-ui/packages/image/src/image-viewer";
import DeviceInspectTakeDetail  from "@/views/inventory/DeviceInspectTakeDetail"
import deviceInspectTakeAdd from "@/views/inventory/DeviceInspectTakeAdd"

export default {
  name: "deviceInspectTake",
  data() {
    return {
      filterQuery: {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        code: null,
        statusList: [0,1],//0草稿 1未分配
        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: "未分配",
      },
      repairTypeMap: {
        1: "巡检",
        2: "养护",
        3: "报修",
      },
      statusdetailMap: {
        1: "未开始",
        2: "已检查",
        3: "异常",
        4: "不巡检",
      },
      formName: null,
      formMap: {
        add: "新增设备巡检任务",
        edit: "编辑设备巡检任务",
        datail:"详情"
      },
      deviceInspectTake: {
        id: null,
        code: null,
        deptCode: null,
        invCode: null,
        deviceName: null,
        orderId: null,
        endTime: '',
        startTime: '',
      },
      deviceProjectSelectVisible: false,
      detailQuery: null,
      addTakeDetial:false,
      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: {
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        orderId: null,
        deptCode: this.$store.getters.locDeptCode,
        invCode: null,
        code: null,
        statusList: [0,1,2],
        page: 1,
        limit: 20,
      };
      this.endDateRange = [];
      this.actDateRange = [];
      this.edDateRange = [];
      this.spaceList = [];
      this.getList();
    },
    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;
      })
    },
    addTake(){
      this.formName="add"
      this.addTakeDetial = true;
    },

    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.formName="edit"
      this.addTakeDetial = true;
      this.deviceInspectTake = row;
      if(type == 3){
        this.formName="datail"
      }
      this.type = type
    },
    deleteDialog(rowId) {
      this.$confirm('此操作将永久删除该巡检任务, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let params = {id: rowId};
        this.loading = true;
        deleteInspectPlan(params).then((res) => {
          this.loading = false;
          if (res.code === 20000) {
            this.$message.success("删除成功！");
            this.getList();
          } else {
            this.$message.error(res.message);
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    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.deviceProjectSelectVisible = false;
    },
    closeDialogTake(){
      this.addTakeDetial = 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,deviceInspectTakeAdd},
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getDeptList();
  },
};
</script>

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

