<template>
  <div class="app-container">
    <el-button
      icon="el-icon-back"
      size="mini"
      style="margin-bottom: 10px"
      @click="goback"
    >
      返回
    </el-button>

    <div class="top flex-b">
      <div class="l top-item bb">
        <div class="t">{{ taskDetailData.title }}</div>
        <div class="m bb">
          <span class="sl">任务起止时间</span>
          <span class="sr">{{ taskDetailData.startTime }} ~ {{ taskDetailData.endTime }}</span>
        </div>
        <div class="b bb flex">
          <div class="bl flex" v-if="taskDetailData.type == 1">
            <div class="l">安检小区或村数</div>
            <div class="r">{{ taskDetailData.coummunityNum }}</div>
          </div>
          <div class="br flex">
            <div class="l">安检总户数</div>
            <div class="r">{{ taskDetailData.totalInspectNum }}</div>
          </div>
        </div>
      </div>
      <div class="m top-item bb">
        <div class="t">任务进度</div>
        <div class="b flex-b bb">
          <div v-for="item in itemDataA" :key="item.label" class="bottom-item">
            <div
              :style="{
                background: `url(${item.src}) no-repeat center`,
                color: item.color,
              }"
              class="imgbg"
            >
              {{ item.label }}
            </div>
            <div :style="{ color: item.color }" class="num">{{ item.val }}</div>
          </div>
        </div>
      </div>
      <div class="r top-item bb">
        <div class="t">隐患信息</div>
        <div class="flex-b m bb">
          <div>存在隐患</div>
          <div class="n">{{ taskInspectDangerDetailData.totalDangerNum ? taskInspectDangerDetailData.totalDangerNum : 0 }}</div>
        </div>
        <div class="b bb flex">
          <div class="bl flex">
            <div class="l">已解决隐患数</div>
            <div class="r">{{ taskInspectDangerDetailData.dealNum ? taskInspectDangerDetailData.dealNum : 0 }}</div>
          </div>
          <div class="br flex">
            <div class="l">未解决隐患数</div>
            <div class="r">{{ taskInspectDangerDetailData.toDoNum ? taskInspectDangerDetailData.toDoNum : 0}}</div>
          </div>
        </div>
      </div>
    </div>

    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!--<el-form-item label="任务id" prop="taskId">-->
      <!--<el-input-->
      <!--v-model="queryParams.taskId"-->
      <!--placeholder="请输入任务"-->
      <!--clearable-->
      <!--size="small"-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="用户姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表具编号" prop="meternum">
        <el-input
          v-model="queryParams.meternum"
          placeholder="请输入表号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="隐患情况" prop="status">-->
      <!--<el-select v-model="queryParams.status" placeholder="请选择0未检测 1有隐患，2整改完成，3完结" clearable size="small">-->
      <!--<el-option label="请选择字典生成" value="" />-->
      <!--</el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="满意度" prop="satisfy">-->
      <!--<el-select v-model="queryParams.satisfy" placeholder="请选择满意度" clearable size="small">-->
      <!--<el-option-->
      <!--v-for="dict in satisfyOptions"-->
      <!--:key="dict.id"-->
      <!--:label="dict.name"-->
      <!--:value="dict.id"-->
      <!--/>-->
      <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="安检状态" prop="inspectStatus">
        <el-select
          v-model="queryParams.inspectStatus"
          placeholder="请选择安检状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="安检人" prop="nickName">
        <!--<el-input
          v-model="queryParams.nickName"
          placeholder="请输入安检人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />-->

        <el-autocomplete
          class="inline-input"
          v-model="queryParams.nickName"
          :fetch-suggestions="queryInspectorSearch"
          placeholder="请输入安检人"
          :trigger-on-focus="false"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        ></el-autocomplete>
      </el-form-item>

      <el-form-item label="所属村庄" prop="village">
        <el-select
          v-model="queryParams.village"
          placeholder="请选择所属村庄/小区"
          clearable
          size="small"
        >
          <el-option
            v-for="community in communityInfos"
            :key="community.id"
            :label="community.name"
            :value="community.id"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="签字，0是未签字，1是已签字" prop="sign">-->
      <!--<el-input-->
      <!--v-model="queryParams.sign"-->
      <!--placeholder="请输入签字，0是未签字，1是已签字"-->
      <!--clearable-->
      <!--size="small"-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="不遇次数" prop="missNum">
        <el-select size="small" v-model="queryParams.missNum" placeholder="请选择到访不遇次数">
          <el-option label="1次" value="1"/>
          <el-option label="2次" value="2"/>
          <el-option label="3次" value="3"/>
        </el-select>
      </el-form-item>

      <el-form-item label="安检时间" prop="inspectTimeRange">
        <el-date-picker
          v-model="inspectTimeRange"
          type="datetimerange"
          size="small"
          :clearable="false"
          value-format="yyyy-MM-dd HH:mm"
          @change="timeChange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handlejmInspectExport"
        >导出</el-button>
        <!--<el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="batchExportLoading"
          @click="handleBatchInspectExport"
        >安检单导出</el-button>-->
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="inspectList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="用户姓名" align="center" prop="username" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="表具编号" align="center" prop="meternum" />
      <!--<el-table-column label="表计类型" align="center" prop="btype" :formatter="typeFormat"/>-->
      <el-table-column label="所属区域" align="center" prop="phone">
        <template slot-scope="scope">
          {{ scope.row.countyName }}{{ scope.row.streetName
          }}{{ scope.row.villageName }}
        </template>
      </el-table-column>
      <!--<el-table-column label="具体地址" align="center" prop="address" >-->
      <!--<template slot-scope="scope">-->
      <!--<span v-if="scope.row.address">{{ scope.row.address}}</span>-->
      <!--<span v-else>-</span>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column
        label="通气状态"
        align="center"
        :formatter="statusFormat"
      />
      <el-table-column label="安检状态" align="center" prop="inspectStatus">
        <template slot-scope="scope">
          <span style="color: #f56c6c" v-if="scope.row.inspectStatus == -1"
            >未安检</span
          >
          <span style="color: purple" v-if="scope.row.inspectStatus == 0"
            >到访不遇</span
          >
          <span style="color: yellow" v-if="scope.row.inspectStatus == 1"
            >拒绝安检</span
          >
          <span style="color: #1ab394" v-if="scope.row.inspectStatus == 2"
            >已安检</span
          >
          <span style="color: #1ab394" v-if="scope.row.inspectStatus == 3">燃气停用</span>
        </template>
      </el-table-column>
      <el-table-column label="不遇次数" align="center" prop="missNum" />
      <el-table-column label="安检人" align="center" prop="nickName" />
      <el-table-column
        label="安检时间"
        align="center"
        prop="createTime"
        width="160"
      />
      <!--<el-table-column label="0未检测 1有隐患，2整改完成，3完结" align="center" prop="status" />-->
      <!--<el-table-column label="区/县" align="center" prop="countyName" />-->
      <!--<el-table-column label="街道" align="center" prop="streetName" />-->
      <!--<el-table-column label="小区" align="center" prop="villageName" />-->
      <!--<el-table-column label="是否安检" align="center" prop="inspectStatus" >-->
      <!--<template slot-scope="scope">-->
      <!--<span v-if="scope.row.inspectStatus==-1">未检测</span>-->
      <!--<span v-if="scope.row.inspectStatus==0">到访不遇</span>-->
      <!--<span v-if="scope.row.inspectStatus==1">拒绝安检</span>-->
      <!--<span v-if="scope.row.inspectStatus==2">已检测</span>-->
      <!--<span v-if="scope.row.inspectStatus==3">表前已检测</span>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="满意度" align="center" prop="satisfy" >-->
      <!--<template slot-scope="scope">-->
      <!--<span v-if="scope.row.satisfy==null">-</span>-->
      <!--<span v-if="scope.row.satisfy==1">不满意</span>-->
      <!--<span v-if="scope.row.satisfy==2">一般</span>-->
      <!--<span v-if="scope.row.satisfy==3">满意</span>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="安检时间" align="center" prop="createTime" >-->
      <!--<template slot-scope="scope">-->
      <!--<span v-if="scope.row.createTime">{{scope.row.createTime}}</span>-->
      <!--<span v-else>-</span>-->
      <!--</template>/-->
      <!--</el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            >记录</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"
    />
    <UserInspctinfo
      :taskId="queryParams.taskId"
      :userId="userId"
      :open="detailListOpen"
      @close="closeXiang"
    />

    <ExportInspect ref="exportInspect"/>
  </div>
</template>

<script>
import {
  listInspect,
  getInspect,
  delInspect,
  addInspect,
  updateInspect,
  exportInspect,
  jmlist,
  juminlist,
  jmInspectExport
} from "@/api/checktask/inspect";

import {
  getTask,
  taskInfoList

} from "@/api/checktask/task";

import UserInspctinfo from "./jumInspect/index";
import imgA from "@/assets/ajImg/imgA.png";
import imgB from "@/assets/ajImg/imgB.png";
import imgC from "@/assets/ajImg/imgC.png";
import { taskDetailInfo, taskInspectDetailInfo, taskInspectDangerDetailInfo } from "@/api/checktask/task";
import { communityList } from "@/api/baseinfo/community";
import ExportInspect from "./ExportInspect";
import { userInfoList } from "@/api/system/user";
export default {
  name: "Inspect",
  components: {
    UserInspctinfo,
    ExportInspect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      batchExportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安检任务执行表格数据
      inspectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        meternum: "",
        username: "",
        satisfy: "",
        inspectStatus: "",
        nickName: null,
        village: null,
        beginDate: null,
        endDate : null
      },
      satisfyOptions: [
        {
          id: 1,
          name: "不满意",
        },
        {
          id: 2,
          name: "一般",
        },
        {
          id: 3,
          name: "满意",
        },
      ],
      statusOptions: [
        {
          id: -1,
          name: "未安检",
        },
        {
          id: 0,
          name: "到访不遇",
        },
        {
          id: 1,
          name: "拒绝安检",
        },
        {
          id: 2,
          name: "已安检",
        },
        {
          id: 3,
          name: "燃气停用",
        },
      ],
      //
      userId: 0,
      detailListOpen: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      typeOptions: [],
      ustatusOptions: [],
      itemDataA: [
        {
          label: "未安检",
          val: 0,
          src: imgA,
          color: "#2167E5",
        },
        {
          label: "到访不遇1",
          val: 0,
          src: imgA,
          color: "#2167E5",
        },
        {
          label: "到访不遇2",
          val: 0,
          src: imgA,
          color: "#2167E5",
        },
        {
          label: "到访不遇3",
          val: 0,
          src: imgA,
          color: "#2167E5",
        },
        {
          label: "拒绝安检",
          val: 0,
          src: imgB,
          color: "#E22121",
        },
        {
          label: "燃气停用",
          val: 0,
          src: imgA,
          color: "#2167E5",
        },
        {
          label: "已安检",
          val: 0,
          src: imgC,
          color: "#08932D",
        },
      ],
      taskDetailData:{},
      taskInspectDangerDetailData:{},
      communityInfos: [],
      inspectTimeRange: []
    };
  },
  created() {
    this.queryParams.taskId = this.$route.query.taskId;
    this.queryParams.taskType = this.$route.query.taskType;
    this.queryParams.type = this.$route.query.type;
    //console.log(this.$route.query.taskId)
    this.getDicts("t_meter_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDicts("t_user_status").then((response) => {
      this.ustatusOptions = response.data;
    });
    this.getList();
    // getTask(this.queryParams.taskId).then(res=>{
    //   console.log("res",res);
    // })
    // getInspect(this.queryParams.taskId ).then(res=>{
    //   console.log('任务执行详情',res)
    // })
    this.taskDetail();
    this.taskInspectDetail();
    this.taskInspectDangerDetail();
  },
  methods: {
    /** 查询安检任务执行列表 */
    getList() {
      console.log(this.queryParams);
      this.loading = true;
      jmlist(this.queryParams).then((response) => {
        this.inspectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskId: null,
        longitude: null,
        latitude: null,
        content: null,
        memberId: null,
        type: null,
        receiveId: null,
        status: 0,
        satisfy: null,
        sign: null,
        createTime: null,
        meterData: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.inspectTimeRange = [];
      this.queryParams.beginDate = null;
      this.queryParams.endDate = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安检任务执行";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInspect(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改安检任务执行";
      });
    },
    //详情
    handleDetail(row) {
      if (row.inspectStatus == -1) {
        this.$message.error("该用户未进行安检");
        return;
      }
      this.taskId = this.queryParams.taskId;
      this.userId = row.userId;
      this.detailListOpen = true;
    },
    closeXiang() {
      this.detailListOpen = false;
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.btype);
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.ustatusOptions, row.ustatus);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInspect(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspect(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除安检任务执行编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delInspect(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    goback() {
      this.$router.back();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有安检任务执行数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportInspect(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    taskDetail(){
      taskDetailInfo({taskId:this.$route.query.taskId}).then(res =>{
          if(res.code == 200){
          this.taskDetailData = res.data;
          this.getCommunityInfo();
        }
      })
    },
    taskInspectDetail(){
      taskInspectDetailInfo({taskId:this.$route.query.taskId,type:this.$route.query.type,taskType:this.$route.query.taskType}).then(res =>{
        if(res.code == 200 && res.data){
          let data = res.data;
          this.itemDataA[0].val = data.noInspectNum;
          this.itemDataA[1].val = data.firstMissNum;
          this.itemDataA[2].val = data.secondeMissNum;
          this.itemDataA[3].val = data.thirdMissNum;
          this.itemDataA[4].val = data.refuseNum;
          this.itemDataA[5].val = data.beforeNum;
          this.itemDataA[6].val = data.inspectNum;
        }
      })
    },
    taskInspectDangerDetail(){
      taskInspectDangerDetailInfo({taskId:this.$route.query.taskId}).then(res =>{
          if(res.code == 200){
          this.taskInspectDangerDetailData = res.data;
        }
      })
    },
    getCommunityInfo(){
      communityList().then(res =>{
        if(res.code == 200 && res.data){
          this.communityInfos = this.taskDetailData.relationInfo ? res.data.filter(community => this.taskDetailData.relationInfo.find(belongArea => belongArea.type == '1' && belongArea.relationId == community.id)) : [];
           //this.communityInfos = res.data;
        }
      })
    },
    /** 导出按钮操作 */
    handlejmInspectExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有居民安检任务数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
        return jmInspectExport(queryParams);
      })
      .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
      })
      .catch(() => {});
    },
    handleBatchInspectExport(){
      this.$refs.exportInspect.init(this.queryParams);
    },
    timeChange(val){
      this.queryParams.beginDate = val[0];
      this.queryParams.endDate = val[1];
    },
    queryInspectorSearch(queryString, cb){
      userInfoList({nickName:queryString}).then(res =>{
        if(res.code == 200 && res.data){
          let result = res.data.map(item => ({value: item.nickName}));
          cb(result);
        }
      })
    },
  },
};
</script>
<style lang="scss" scoped>
.top {
  height: 200px;
  .top-item {
    width: 490px;
    height: 180px;
    border-radius: 3px;
    border: 1px solid #eeeeee;
    box-shadow: 0px 0px 24px 0px rgba(0, 0, 0, 0.08);
    padding-top: 32px;
    padding-left: 30px;
    color: #1b63df;

    .t {
      font-size: 20px;
      margin-bottom: 21px;
      font-weight: 800;
    }
    &.l {
      .m {
        height: 40px;
        border-radius: 20px;
        background: #eef5ff;
        line-height: 42px;
        padding-left: 38px;
        font-size: 16px;
        margin-right: 34px;

        margin-bottom: 19px;
        .sl {
          font-size: 14px;
          margin-right: 40px;
        }
        .sr {
          font-size: 16px;
          letter-spacing: 1px;
        }
      }
      .b {
        padding-left: 17px;

        .bl,
        .br {
          margin-right: 72px;
          align-items: center;
          .l {
            font-size: 14px;
            margin-right: 16px;
          }
          .r {
            font-size: 16px;
            font-weight: 800;
          }
        }
        .br {
          margin-right: 0;
        }
      }
    }
    &.m {
      width: 620px;
      padding-right: 34px;
      .t {
        margin-bottom: 17px;
      }
      .b {
        .bottom-item {
          .imgbg {
            width: 80px;
            height: 56px;
            text-align: center;
            line-height: 56px;
            font-weight: 600;
            font-size: 14px;
            margin-bottom: 8px;
          }
          .num {
            text-align: center;
            font-weight: 800;
          }
        }
      }
    }
    &.r {
      padding-right: 34px;
      .m {
        height: 40px;
        background: rgba(255, 214, 214, 0.24);
        margin-bottom: 18px;
        color: #e22121;
        line-height: 40px;
        padding: 0 25px;
        .n {
          font-weight: 800;
        }
      }
      .b {
        padding-left: 17px;
        color: #e22121;

        .bl,
        .br {
          margin-right: 72px;
          align-items: center;
          .l {
            font-size: 14px;
            margin-right: 16px;
          }
          .r {
            font-size: 16px;
            font-weight: 800;
          }
        }
        .br {
          margin-right: 0;
        }
      }
    }
  }
}
</style>
