<template>
  <div class="container-main" ref="main">
    <div class="search-hearder" ref="topSearch">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="auto"
      >
        <el-form-item label="点位名称" prop="pointName">
          <el-input
            v-model="queryParams.pointName"
            placeholder="请输入点位名称"
            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>
    </div>
    <div class="layui-table-tool" ref="tableTool">
      <el-button type="primary" size="mini" @click="handleAdd">新增</el-button>
      <el-button
        type="warning"
        size="mini"
        :loading="exportLoading"
        @click="handleExport"
        >导出</el-button
      >
    </div>

    <el-table
      v-loading="loading"
      :data="pointList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="55">
        <template slot-scope="scop">
          {{
            scop.$index + 1 + (queryParams.pageNum - 1) * queryParams.pageSize
          }}
        </template>
      </el-table-column>
      <el-table-column label="点位名称" align="center" prop="pointName" />
      <el-table-column label="点位类型" align="center" prop="pointType" />
      <el-table-column label="归属" align="center" prop="area" />
      <el-table-column label="点位地址" align="center" prop="pointAddress" />
      <el-table-column label="经纬度" align="center" prop="latlng" />
      <el-table-column label="点位负责人" align="center" prop="pointPeople" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-view"
            @click="handleInfo(scope.row)"
          >
            查看
          </el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(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"
    />

    <!-- 添加或修改点位对话框 -->
    <el-dialog
      :visible.sync="open"
      width="1200px"
      append-to-body
      custom-class="dialog-box"
    >
      <div slot="title" class="dialog-title">{{ title }}</div>

      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="180px"
        custom-class="dialog-from"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="点位名称" prop="pointName">
              <el-input v-model="form.pointName" placeholder="请输入点位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="归属" prop="area">
              <el-input v-model="form.area" placeholder="请输入点位名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="点位类型" prop="pointType">
              <el-input v-model="form.pointType" placeholder="请输入点位类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位地址" prop="pointAddress">
              <el-input
                v-model="form.pointAddress"
                placeholder="请输入点位地址"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经纬度" prop="latlng">
              <el-input v-model="form.latlng" placeholder="请输入经纬度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位负责人" prop="pointPeople">
              <el-input
                v-model="form.pointPeople"
                placeholder="请输入点位负责人"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="pointPhone">
              <el-input
                v-model="form.pointPhone"
                placeholder="请输入联系电话"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开始运营时间" prop="startTime">
              <el-input
                v-model="form.startTime"
                placeholder="请输入开始运营时间"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="点位介绍" prop="pointIntro">
              <el-input
                v-model="form.pointIntro"
                type="textarea"
                autosize
                placeholder="请输入点位介绍"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="点位图片" prop="pointPic">
              <ImageUpload v-model="form.pointPic" :limit="8"></ImageUpload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="工作人员情况" prop="workerNumb">
              <el-input
                v-model="form.workerNumb"
                type="textarea"
                autosize
                placeholder="请输入工作人员情况"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="场所情况" prop="locationSituation">
              <el-input
                v-model="form.locationSituation"
                type="textarea"
                autosize
                placeholder="请输入场所情况"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="截止今日入住老人情况" prop="checkInNumb">
          <el-input
            v-model="form.checkInNumb"
            type="textarea"
            autosize
            placeholder="请输入截止今日入住老人情况"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--查看点位详情页-->
    <el-dialog
      :visible.sync="infoOpen"
      width="1200px"
      custom-class="dialog-box"
      append-to-body
      :close-on-click-modal="false"
      :destroy-on-close="true"
    >
      <div slot="title" class="dialog-title">{{ infoTitle }}</div>

      <div class="info_box">
        <el-descriptions :column="2" border>
          <el-descriptions-item label="点位名称">{{
            form.pointName
          }}</el-descriptions-item>

          <el-descriptions-item label="点位类型">{{
            form.pointType
          }}</el-descriptions-item>

          <el-descriptions-item label="点位名称">{{
            form.area
          }}</el-descriptions-item>

          <el-descriptions-item label="点位地址">{{
            form.pointAddress
          }}</el-descriptions-item>

          <el-descriptions-item label="经纬度">{{
            form.latlng
          }}</el-descriptions-item>

          <el-descriptions-item label="点位负责人">{{
            form.pointPeople
          }}</el-descriptions-item>

          <el-descriptions-item label="联系电话" :span="2">{{
            form.pointPhone
          }}</el-descriptions-item>

          <el-descriptions-item label="点位图片" :span="2">
            <div class="imgs">
              <el-image
                v-for="(item, index) in filters(form.pointPic)"
                :key="index"
                style="width: 100px; height: 100px"
                :src="item"
                :preview-src-list="imgs"
              >
              </el-image>
            </div>
          </el-descriptions-item>

          <el-descriptions-item label="点位介绍" :span="2">{{
            form.pointIntro
          }}</el-descriptions-item>

          <el-descriptions-item label="开始运营时间" :span="2">{{
            form.startTime
          }}</el-descriptions-item>

          <el-descriptions-item label="工作人员情况" :span="2">{{
            form.workerNumb
          }}</el-descriptions-item>

          <el-descriptions-item label="场所情况" :span="2">{{
            form.locationSituation
          }}</el-descriptions-item>

          <el-descriptions-item label="截止今日入住老人情况">{{
            form.checkInNumb
          }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPoint,
  getPoint,
  delPoint,
  addPoint,
  updatePoint,
  exportPoint,
} from "@/api/mudu/point";
export default {
  name: "Point",
  //注释字典
  data() {
    return {
      //查看详情
      infoOpen: false,
      //详情标题
      infoTitle: "",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点位表格数据
      pointList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pointName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pointName: [
          { required: true, message: "点位名称不能为空", trigger: "change" },
        ],
        area: [
          { required: true, message: "点位名称不能为空", trigger: "change" },
        ],
        pointType: [
          { required: true, message: "点位类型不能为空", trigger: "change" },
        ],
        pointAddress: [
          { required: true, message: "点位地址不能为空", trigger: "change" },
        ],
        latlng: [
          { required: true, message: "经纬度不能为空", trigger: "change" },
        ],
        pointPeople: [
          { required: true, message: "点位负责人不能为空", trigger: "change" },
        ],
        pointPhone: [
          { required: true, message: "联系电话不能为空", trigger: "change" },
        ],
        startTime: [
          {
            required: true,
            message: "开始运营时间不能为空",
            trigger: "change",
          },
        ],
        pointIntro: [
          { required: true, message: "点位介绍不能为空", trigger: "change" },
        ],
        pointPic: [
          { required: true, message: "请选择点位图片", trigger: "change" },
        ],
        workerNumb: [
          {
            required: true,
            message: "工作人员情况不能为空",
            trigger: "change",
          },
        ],
        locationSituation: [
          { required: true, message: "场所情况不能为空", trigger: "change" },
        ],
        checkInNumb: [
          {
            required: true,
            message: "截止今日入住老人情况不能为空",
            trigger: "change",
          },
        ],
      },
      urls: process.env.VUE_APP_BASE_API,
      imgs: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    filters(e) {
      if (!e) return;
      var new1 = [];
      e.split(",").forEach((value) => {
        new1.push(this.urls + value);
      });
      return new1;
    },
    /** 查询点位列表 */
    getList() {
      this.loading = true;
      listPoint(this.queryParams).then((response) => {
        this.pointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        pointName: null,

        pointType: null,

        area: null,

        pointAddress: null,

        latlng: null,

        pointPeople: null,

        pointPhone: null,

        pointPic: null,

        pointIntro: null,

        startTime: null,

        workerNumb: null,

        locationSituation: null,

        checkInNumb: null,

        createId: null,

        createBy: null,

        createTime: null,

        updateId: null,

        updateBy: null,

        updateTime: null,

        remark: null,

        userId: null,

        deptId: 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;
    },
    /**查看按钮操作 */
    handleInfo(row) {
      this.reset();
      const id = row.id || this.ids;
      getPoint(id).then((response) => {
        this.form = response.data;
        this.imgs = this.filters(response.data.pointPic);
        this.infoOpen = true;
        this.infoTitle = "查看点位详情";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加点位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPoint(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改点位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePoint(this.form).then((response) => {
              this.$model.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPoint(this.form).then((response) => {
              this.$model.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除点位编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delPoint(ids);
        })
        .then(() => {
          this.getList();
          this.$model.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有点位数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.download(
            "/mudu/point/export",
            {
              ...this.queryParams,
            },
            "点位_" + new Date().getTime() + ".xlsx"
          );
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.container-main {
  height: calc(100vh - 84px);
  // height: 100%;
  overflow: hidden;
  padding: 0 10px;
}
::v-deep .el-descriptions-row th {
  background: #f5f5f5;
  color: #333;
  font-weight: bold !important;
  width: 16% !important;
}
::v-deep .el-descriptions-row td {
  width: 34% !important;
}
::v-deep .el-select.el-select--medium {
  width: 100%;
}
::v-deep .el-image {
  margin-right: 10px;
  box-shadow: rgba(60, 64, 67, 0.3) 0px 1px 2px 0px,
    rgba(60, 64, 67, 0.15) 0px 1px 3px 1px;
}
</style>
