<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 }}</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="companyName">
        <el-input
          v-model="queryParams.companyName"
          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="usernum">
        <el-input
          v-model="queryParams.usernum"
          placeholder="请输入安检人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="安检人" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          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="签字，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>
        <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="['checktask:inspect:add']"-->
    <!--&gt;新增</el-button>-->
    <!--</el-col>-->
    <!--<el-col :span="1.5">-->
    <!--<el-button-->
    <!--type="success"-->
    <!--plain-->
    <!--icon="el-icon-edit"-->
    <!--size="mini"-->
    <!--:disabled="single"-->
    <!--@click="handleUpdate"-->
    <!--v-hasPermi="['checktask:inspect:edit']"-->
    <!--&gt;修改</el-button>-->
    <!--</el-col>-->
    <!--<el-col :span="1.5">-->
    <!--<el-button-->
    <!--type="danger"-->
    <!--plain-->
    <!--icon="el-icon-delete"-->
    <!--size="mini"-->
    <!--:disabled="multiple"-->
    <!--@click="handleDelete"-->
    <!--v-hasPermi="['checktask:inspect:remove']"-->
    <!--&gt;删除</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="['checktask:inspect:export']"-->
    <!--&gt;导出</el-button>-->
    <!--</el-col>-->
    <!--<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--</el-row>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <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 type="selection" width="55" align="center" />-->
      <el-table-column
        label="商户名称"
        align="center"
        prop="companyName"
        width="140"
        :show-overflow-tooltip="true"
      />
      <!--<el-table-column label="合同号" align="center" prop="contract" />-->
      <el-table-column label="联系人" align="center" prop="username" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="表号" align="center" prop="usernum" />
      <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: yellow" 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="nickName" />
      <el-table-column
        label="安检时间"
        align="center"
        prop="createTime"
        width="150"
      />
      <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,
  exportInspect,
  cylist,
} from "@/api/checktask/inspect";
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 ExportInspect from "./ExportInspect";
export default {
  name: "cyInspect",
  components: {
    UserInspctinfo,
    ExportInspect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安检任务执行表格数据
      inspectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      batchExportLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        usernum: "",
        username: "",
        companyName: "",
        satisfy: "",
        inspectStatus: "",
        nickName: 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: "燃气停用",
        },
      ],
      itemDataA: [
        {
          label: "未安检",
          val: 2109,
          src: imgA,
          color: "#2167E5",
        },
        {
          label: "到访不遇",
          val: 21,
          src: imgA,
          color: "#2167E5",
        },
        {
          label: "拒绝安检",
          val: 3,
          src: imgB,
          color: "#E22121",
        },
        {
          label: "表前安检",
          val: 4,
          src: imgA,
          color: "#2167E5",
        },
        {
          label: "已安检",
          val: 1209,
          src: imgC,
          color: "#08932D",
        },
      ],
      //
      userId: 0,
      detailListOpen: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      typeOptions: [],
      ustatusOptions: [],
      taskDetailData:{},
      taskInspectDangerDetailData:{}
    };
  },
  created() {
    this.queryParams.taskId = this.$route.query.taskId;
    this.queryParams.type = this.$route.query.type;
    this.queryParams.taskType = this.$route.query.taskType;
    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();
    this.taskDetail();
    this.taskInspectDetail();
    this.taskInspectDangerDetail();
  },
  methods: {
    /** 查询安检任务执行列表 */
    getList() {
      console.log(this.queryParams);
      this.loading = true;
      cylist(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.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;
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.btype);
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.ustatusOptions, row.ustatus);
    },
    closeXiang() {
      this.detailListOpen = false;
    },
    /** 提交按钮 */
    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.push({
      //   path: "task",
      // });
        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;
        }
      })
    },
    taskInspectDetail(){
      taskInspectDetailInfo({taskId:this.$route.query.taskId,type:this.$route.query.type,taskType:this.$route.query.taskType}).then(res =>{
        if(res.code == 200){
          let data = res.data;
          this.itemDataA[0].val = data.noInspectNum;
          this.itemDataA[1].val = data.missNum;
          this.itemDataA[2].val = data.refuseNum;
          this.itemDataA[3].val = data.beforeNum;
          this.itemDataA[4].val = data.inspectNum;
        }
      })
    },
    taskInspectDangerDetail(){
      taskInspectDangerDetailInfo({taskId:this.$route.query.taskId}).then(res =>{
          if(res.code == 200){
          this.taskInspectDangerDetailData = res.data;
        }
      })
    },
    handleBatchInspectExport(){
      this.$refs.exportInspect.init(this.queryParams);
    }
  },
};
</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: 570px;
      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>
