<template>
  <div class="app-container rw-dilog">
    <el-dialog
      title="安检记录"
      :visible.sync="open"
      width="1400px"
      append-to-body
      destroy-on-close
      :before-close="handleClose"
    >
      <el-row>
        <div class="top-title">{{ $parent.taskDetailData.title }}</div>
        <div class="user-title">用户信息</div>
        <el-col>
          <el-row class="container flex bb">
            <el-form v-if="userType == 2" label-width="100px">
              <el-col :span="8">
                <el-form-item label="用户姓名">
                  <span>{{ detailInfo.username || "-" }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系电话">
                  <span>{{ detailInfo.phone || "-" }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="表具编号">
                  <span>{{ detailInfo.usernum || "-" }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="用户类型">
                  <!--<span>{{ typeFormat(this.detailInfo) || "-" }}</span>-->
                  <span v-if="detailInfo.contract == '1'">小商业</span>
                  <span v-else-if="detailInfo.contract == '2'">大商业</span>
                  <span v-else>-</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="所属区域">
                  <span
                    >{{ detailInfo.countyName }}{{ detailInfo.streetName
                    }}{{ detailInfo.villageName }}</span
                  >
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="通气日期">
                  <span v-if="detailInfo.starttime">{{
                    detailInfo.starttime
                  }}</span>
                  <span v-else>-</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="开通状态">
                  <span>{{ statusFormat(this.detailInfo) || "-" }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="表具类型">
                  <span v-if="detailInfo.meterType">{{ typeFormatter(detailInfo) }}</span>
                  <span>-</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="表具品牌">
                  <span v-if="detailInfo.meterCompany > -1">{{ meterModelCompanyFormatter(detailInfo) }}</span>
                  <span v-else>-</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="表具型号">
                  <span v-if="detailInfo.meterModel">{{ meterModelFormatter(detailInfo,null) }}</span>
                  <span v-else>-</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="进气方向">
                  <span v-if="detailInfo.direction == 1">左</span>
                  <span v-if="detailInfo.direction == 2">右</span>
                  <span v-if="detailInfo.direction == null">-</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="具体地址">
                  <span>{{ detailInfo.address || "-" }}</span>
                </el-form-item>
              </el-col>

              <!--<el-col :span="24">
                <el-form-item label="用气设施">
                  <span>{{ sheshiFormat(this.detailInfo) || "-" }}</span>
                </el-form-item>
              </el-col>-->
            </el-form>

            <el-form v-if="userType == 1" label-width="100px">
              <el-col :span="8">
                <el-form-item label="用户姓名">
                  <span>{{ detailInfo.username || "-" }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="用户编号">
                  <span>{{ detailInfo.usernum || "-" }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="合同号">
                  <span>{{ detailInfo.contract || "-" }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="单位名称">
                  <span>{{ detailInfo.company || "-" }}</span>
                </el-form-item>
              </el-col>

              <!--<el-row>-->
              <!--<el-col :span="8">-->
              <!--<el-form-item label="所属市">-->
              <!--<span>{{ $parent.getAddressFormatter(detailInfo.city) }}</span>-->
              <!--</el-form-item>-->
              <!--</el-col>-->
              <!--<el-col :span="8">-->
              <!--<el-form-item label="所属区/县">-->
              <!--<span>{{ $parent.getAddressFormatter(detailInfo.county) }}</span>-->
              <!--</el-form-item>-->
              <!--</el-col>-->
              <!--</el-row>-->

              <el-col :span="8">
                <el-form-item label="所属区域">
                  <span>
                    {{ getAddressFormatter(detailInfo.county) }}
                    {{ getAddressFormatter(detailInfo.street) }}
                  </span>
                  <!--<span>{{ $parent.getAddressFormatter(detailInfo.street) }}</span>-->
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="联系电话">
                  <span>{{ detailInfo.phone || "-" }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="表计类型">
                  <span>{{ typeFormat(this.detailInfo) || "-" }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="通气日期">
                  <span>{{ detailInfo.starttime || "-" }}</span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="开通状态">
                  <span>{{ statusFormat(this.detailInfo) || "-" }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="表具型号">
                  <span v-if="detailInfo.meterModel == 1">G1.5</span>
                  <span v-if="detailInfo.meterModel == 2">G2.5</span>
                  <span v-else-if="detailInfo.meterModel == 3">G4</span>
                  <span v-else> - </span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="表具类型">
                  <span v-if="detailInfo.meterType == 1">皮膜</span>
                  <span v-else-if="detailInfo.meterType == 2">超声</span>
                  <span v-else> - </span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="表具厂家">
                  <span v-if="detailInfo.meterCompany == 1">金卡</span>
                  <span v-else-if="detailInfo.meterCompany == 2">秦川</span>
                  <span v-else> - </span>
                </el-form-item>
              </el-col>

              <el-col :span="8">
                <el-form-item label="进气方向" prop="direction">
                  <span v-if="detailInfo.direction == 1">左</span>
                  <span v-else-if="detailInfo.direction == 2">右</span>
                  <span v-else> - </span>
                </el-form-item>
              </el-col>
              <el-col :span="16">
                <el-form-item label="具体地址">
                  <span>{{ detailInfo.address || "-" }}</span>
                </el-form-item>
              </el-col>
            </el-form>
          </el-row>

          <!-- <el-col :span="8">
            <el-row :span="10">
              <div
                id="map"
                style="width: 100%; height: 300px; border: 1px solid"
              ></div>
            </el-row>
          </el-col> -->
        </el-col>
      </el-row>

      <div class="table flex-b">
        <div class="l">
          <el-table
            v-loading="loading"
            :data="inspectList"
            :height="240"
            @selection-change="handleSelectionChange"
          >
            <!--<el-table-column type="selection" width="55" align="center" />-->
            <el-table-column
              label="序号"
              type="index"
              width="50"
              align="center"
            ></el-table-column>
            <el-table-column
              label="安检时间"
              align="center"
              prop="createTime"
              width="200"
              :show-overflow-tooltip="true"
            >
              <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" prop="danger">
              <template slot-scope="scope">
                <span v-if="scope.row.danger == 0">无</span>
                <span v-else-if="scope.row.danger == 1">有</span>
                <span v-else>-</span>
              </template>
            </el-table-column>
            <!--<el-table-column label="表号" align="center" prop="usernum" />-->
            <!--<el-table-column label="姓名" align="center" prop="username" />-->
            <!-- <el-table-column
              label="是否安检"
              align="center"
              prop="inspectStatus"
            >
              <template slot-scope="scope">
                <span v-if="scope.row.status == 0">到访不遇</span>
                <span v-if="scope.row.status == 1">拒绝安检</span>
                <span v-if="scope.row.status == 2">已安检</span>
                <span v-if="scope.row.status == 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="sign">
              <template slot-scope="scope">
                <span v-if="scope.row.sign == 0">未签字</span>
                <span v-else-if="scope.row.sign == 1">已签字</span>
                <span v-else>-</span>
              </template>
            </el-table-column>
            <el-table-column label="安检人" align="center" prop="username" />

            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
              width="250"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  v-if="scope.row.status == 2"
                  @click="handleDetail(scope.row)"
                  >检查表</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  v-if="scope.row.status == 0"
                  @click="handlePicture(scope.row)"
                  >不遇图片</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  @click="handleLocation(scope.row)"
                  >位置</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  v-if="scope.row.danger == 1"
                  @click="handleDanger(scope.row)"
                  >隐患</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  v-if="scope.row.status == 1 ||scope.row.status == 2 || scope.row.status == 3"
                  @click="handleCover(scope.row)"
                  >图片</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-show="total > 0"
            :total="total"
            :pageSizes="[5, 10]"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
        <div class="r">
          <div id="map"></div>
        </div>
      </div>

      <CanyinRecordDetail ref="cyRecordDetail" />

      <!-- 隐患信息 -->
      <el-dialog
        title="隐患记录"
        :visible.sync="dangerDetailOpen"
        width="1280px"
        append-to-body
        destroy-on-close
      >
        <DangerDetailInfo ref="dangerDetail" v-if="dangerDetailOpen" />
      </el-dialog>
    </el-dialog>

    <!-- 到访不遇图片 -->
    <el-image
      ref="viewImage"
      :src="imageUrl"
      v-show="false"
      :preview-src-list="imageUrlList"
      :z-index="9999"
    ></el-image>

    <!-- 表封照片 -->
    <CoverPhotoView ref="cover" />
  </div>
</template>

<script>
import {
  listInspect,
  getInspect,
  delInspect,
  addInspect,
  updateInspect,
  exportInspect,
  juminlist,
} from "@/api/checktask/inspect";
import { EditorMap } from "@/utils/mapClass/map";
import { mapGetters } from "vuex";
import CanyinRecordDetail from "./cyRecordDetail";
import DangerDetailInfo from "../../task/components/DangerDetailInfo";
import { getUser } from "@/api/baseinfo/user";
import { getBusiness } from "@/api/baseinfo/businessInfo";
import { aLLAreaListInfo } from "@/api/baseinfo/area";
import CoverPhotoView from "../../task/components/CoverPhotoView";
import { listDanger } from "@/api/checktask/danger";
export default {
  name: "InspectDetail",
  components: {
    CanyinRecordDetail,
    DangerDetailInfo,
    CoverPhotoView,
  },
  props: {
    open: {
      type: Boolean,
      default: false,
    },
    taskId: {
      type: String,
    },
    userId: {
      type: Number,
    },
  },
  computed: {
    ...mapGetters(["systemSetting"]),
  },
  data() {
    return {
      // cyRecordDetail: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安检任务执行表格数据
      inspectList: [],
      // 弹出层标题
      title: "",
      detailInfo: {},
      gasUseDevice: [],
      userType: 1,
      // 是否显示弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        taskId: "",
        receiveId: "",
      },
      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: "燃气停用",
        },
      ],
      dangerDetailOpen: false,
      // 开通状态字典
      statusOptions: [],
      typeOptions: [],
      sceneOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      imageUrl: null,
      imageUrlList: [],

      addressInfo: [],
      deviceName: {
        bothEyes: "双眼灶",
        singleEyes: "单眼灶",
        manyEyes: "多眼灶",
        cauldron: "大锅灶",
        civil: "民用灶",
        barbecue: "烧烤灶",
        lowSoup: "低汤灶",
        clayPot: "煲仔炉",
        dwarf: "矮仔炉",
        oven: "烤箱/炉",
        steamer: "蒸汽炉/箱",
        barrel: "煮面桶",
        wall: "壁挂炉",
      },
      meterCompany: [],
      meterType: [],
      meterModelOptional: []
    };
  },
  watch: {
    open(newVal) {
      if (newVal) {
        this.queryParams.taskId = this.taskId;
        this.queryParams.receiveId = this.userId;
        this.getList();
        this.getAddressInfo();
      }
    },
  },
  created() {
    this.getDicts("t_user_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("t_meter_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDicts("t_user_scene").then((response) => {
      this.sceneOptions = response.data;
    });
    this.getDicts("t_business_factory_meter_type").then((response) => {
        this.meterCompany = response.data;
    });
    this.getMeterType();
    this.getMeterCompany();
    this.getDicts("t_business_meter_model").then((response) => {
        this.meterModelOptional = response.data;
    });
  },
  methods: {
    getDetailInfo(userId) {
      getUser(userId).then((response) => {
        this.detailInfo = response.data;
        this.addUserLocation({
          longitude: response.data.longitude,
          latitude: response.data.latitude,
          nickName: response.data.username,
          iconType: "19",
        });
      });
    },
    getDetailInfotwo(businessId) {
      getBusiness(businessId).then((response) => {
        this.detailInfo = response.data;
        this.addUserLocation({
          longitude: response.data.longitude,
          latitude: response.data.latitude,
          nickName: response.data.username,
          iconType: "19",
        });
        //this.gasUseDevice = JSON.parse(this.detailInfo.device);
      });
    },
    //获取地址信息
    getAddressInfo() {
      aLLAreaListInfo().then((res) => {
        if (res.code == 200) {
          this.addressInfo = res.data;
        }
      });
    },
    //市格式化
    getAddressFormatter(addressId) {
      let address = this.addressInfo.find((item) => item.id == addressId);
      return address ? address.areaName : "";
    },
    // 开通状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    sheshiFormat(row, column) {
      if (row.device == undefined) {
        return "";
      }
      var str = "";
      /*for (var tp of row.device.split(",")) {
        str += this.selectDictLabel(this.sceneOptions, tp) + "、";
      }*/
      const deviceInfo = JSON.parse(row.device);
      for (var tp in deviceInfo) {
        str += this.deviceName[tp] + ": " + deviceInfo[tp] + "、";
      }
      return str.substr(0, str.length - 1);
      //return this.selectDictLabel(this.sceneOptions, row.status);
    },
    /** 查询安检任务执行列表 */
    getList() {
      this.loading = true;
      juminlist(this.queryParams).then((response) => {
        this.inspectList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(() => {
          this.initMap();
          this.addLocation({
            longitude: this.inspectList[0].longitude,
            latitude: this.inspectList[0].latitude,
            nickName: this.inspectList[0].createTime,
            iconType: "industryTaskInspect",
          });
        });
        if (this.inspectList[0].type == 1) {
          this.userType = 1;
          this.getDetailInfo(this.userId);
        } else {
          this.userType = 2;
          console.log(this.userType);
          this.getDetailInfotwo(this.userId);
        }
      });
    },
    handleClose() {
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //详情
    handleDetail(row) {
      console.log(row);
      this.$refs.cyRecordDetail.open = true;
      this.$refs.cyRecordDetail.checkType = row.type;
      this.$refs.cyRecordDetail.map = row;
      this.$refs.cyRecordDetail.jsondata = JSON.parse(row.content);
      this.$refs.cyRecordDetail.detailInfo = this.detailInfo;
      /*if (row.type == 2) {
        this.$refs.cyRecordDetail.device = JSON.parse(this.detailInfo.device);
      }*/
    },
    // 表单重置
    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 = "修改安检任务执行";
      });
    },
    //隐患
    handleDanger(row) {
      listDanger({ pageNum: 1, pageSize: 99999, inspectId: row.id }).then(
        (response) => {
          if (
            response.code != 200 ||
            response.rows == null ||
            response.rows.length == 0
          ) {
            this.$message.error("未查到隐患信息！");
            return;
          }
          this.dangerDetailOpen = true;
          this.$nextTick(() => {
            this.$refs.dangerDetail.getList(response);
          });
        }
      );
      /* this.dangerDetailOpen = true;
      this.$nextTick(() => {
        this.$refs.dangerDetail.queryParams.inspectId = row.id;
        this.$refs.dangerDetail.getList();
      });*/
    },
    /** 提交按钮 */
    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(() => {});
    },
    //位置
    handleLocation(row) {
      this.addLocation({
        longitude: row.longitude,
        latitude: row.latitude,
        nickName: row.createTime,
        iconType: "industryTaskInspect",
      });
    },
    //地图初始化
    initMap() {
      const path = eval(this.systemSetting.map_center);
      this.gaoMap = new EditorMap(
        "map",
        {
          center: path,
          zoom: 12,
        },
        this
      );
    },
    //位置上图
    addLocation(data) {
      this.gaoMap.allDevice["industryTaskInspect"]?.forEach((item) => {
        this.gaoMap.map.remove(item);
      });
      this.gaoMap.control = 2;
      this.gaoMap.addDevice(data, null);
      this.gaoMap.map.panTo([data.longitude, data.latitude]);
    },
    addUserLocation(data) {
      this.gaoMap.control = 2;
      this.gaoMap.addDevice(data, null);
      this.gaoMap.map.panTo([data.longitude, data.latitude]);
    },
    /** 导出按钮操作 */
    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(() => {});
    },
    //查看图片
    handlePicture(row) {
      if (!row.meterData) return this.$message.error("未查询到到访不遇图片");
      this.imageUrl = row.meterData.split(",")[0];
      this.imageUrlList = row.meterData.split(",");
      console.log(this.imageUrl);
      this.$refs.viewImage.showViewer = true;
    },
    //表封照片
    handleCover(row) {
      if (!row.meterImg&&!row.meterData) return this.$message.error("未查询到图片");

      this.$refs.cover.viewCoverPhoto(row.meterImg?row.meterImg:row.meterData);
    },
    //表计类型
    typeFormatter(row){
      return this.selectDictLabel(
        this.meterType,
        row.type
      );
    },
    meterModelFormatter(row){
      return this.selectDictLabel(
        this.meterModelOptional,
        row.meterModel
      );
    },
    meterModelCompanyFormatter(row){
      return this.selectDictLabel(
        this.meterCompany,
        row.meterCompany
      );
    },
    getMeterType(){
      this.getDicts("t_meter_type").then((response) => {
          this.meterType = response.data;
      });
    },
    getMeterCompany(){
      this.getDicts("t_business_factory_meter_type").then((response) => {
          this.meterCompany = response.data;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-col-8 {
  height: 40px;
}
::v-deep .el-form-item {
  margin-bottom: 0 !important;
}
::v-deep .el-table::before {
  height: 0;
}
.top-title {
  text-align: center;
  font-size: 20px;
  margin-bottom: 14px;
}
.user-title {
  color: #226aed;
  font-size: 16px;
  margin-bottom: 16px;
}
.container {
  background: #f9f9f9;
  margin-bottom: 32px;
  padding: 10px 0;
}
.table {
  height: 300px;
  .l {
    width: 850px;
    margin-right: 20px;
  }
  .r {
    flex: 1;
    height: 100%;
    #map {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
