<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物资名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物资名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="materialType">
        <el-select v-model="queryParams.materialType" placeholder="请选择分类" clearable size="small">
          <el-option
            v-for="dict in materialTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </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="['system:info:add']"
        >新增</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="['system:info: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="['system:info: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="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <!--<el-table-column label="分类" align="center" prop="id" />-->
      <el-table-column label="物资名称" align="center" prop="materialName" />
      <el-table-column label="分类" align="center" prop="materialType" :formatter="materialTypeFormat" />
      <el-table-column label="物资数量" align="center" prop="num" width="80"/>
      <!--<el-table-column label="性能" align="center" prop="performance" />-->
      <!--<el-table-column label="用途" align="center" prop="purpose" />-->
      <el-table-column label="有效时间" align="center" prop="validityTime" />
      <!--<el-table-column label="所属部门" align="center" prop="deptId" />-->
      <!--<el-table-column label="经度" align="center" prop="longitude" />-->
      <!--<el-table-column label="维度" align="center" prop="latitude" />-->
      <el-table-column label="地址信息" align="center" prop="address" />
      <el-table-column label="联系人" align="center" prop="contacts" width="100"/>
      <el-table-column label="手机号" align="center" prop="phone" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="210">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click=""
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:info:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:info:remove']"
          >删除</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 :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <div class="division">
            <div style="width: 50%">
              <el-form-item label="物资名称" prop="materialName">
                <el-input v-model="form.materialName" placeholder="请输入物资名称" />
              </el-form-item>
              <el-form-item label="分类" prop="materialType">
                <el-select v-model="form.materialType" placeholder="请选择分类">
                  <el-option
                    v-for="dict in materialTypeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="parseInt(dict.dictValue)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="物资数量" prop="num">
                <el-input v-model="form.num" placeholder="请输入物资数量" />
              </el-form-item>
            </div>
            <div style="width: 50%">
              <el-form-item label="有效时间" prop="validityTime">
                <el-date-picker clearable size="small"
                                style="width: 100%"
                                v-model="form.validityTime"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择有效时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="联系人" prop="contacts">
                <el-input v-model="form.contacts" placeholder="请输入联系人" />
              </el-form-item>
              <el-form-item label="手机号" prop="phone">
                <el-input v-model="form.phone" placeholder="请输入手机号" />
              </el-form-item>
            </div>
        </div>
        <div class="division" style="width: 100%">
          <el-form-item label="经纬度" prop="longitude">
            <div class="division">
              <el-input v-model="form.longitude" placeholder="请输入经度" />
              <el-input style="margin-left: 10px" v-model="form.latitude" placeholder="请输入维度" />
            </div>
          </el-form-item>

          <el-button style=" height: 36px;margin-left: 10px" type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
          <!--<div class="btn">选择经纬度</div>-->
        </div>
        <el-form-item label="地址信息" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址信息" />
        </el-form-item>
        <el-form-item label="性能" prop="performance">
          <el-input v-model="form.performance" type="textarea" placeholder="请输入性能" />
        </el-form-item>
        <el-form-item label="用途" prop="purpose">
          <el-input v-model="form.purpose" type="textarea"  placeholder="请输入用途" />
        </el-form-item>
        <!--<el-form-item label="所属部门" prop="deptId">-->
          <!--<el-input v-model="form.deptId" 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 title="物资详情" :visible.sync="open2" width="900px" append-to-body>
      <el-form  label-width="90px">
        <div class="division">
          <div style="width: 40%">
            <el-form-item label="物资名称:" prop="materialName">
              <span>{{form.materialName}}</span>
            </el-form-item>
            <el-form-item label="分类:" prop="materialType">
              <span>{{form.materialType}}</span>
            </el-form-item>
            <el-form-item label="物资数量:" prop="num">
              <span>{{form.num}}</span>
            </el-form-item>
            <el-form-item label="有效时间:" prop="validityTime">
              <span>{{form.validityTime}}</span>
            </el-form-item>
            <el-form-item label="联系人:" prop="contacts">
              <span>{{form.contacts}}</span>
            </el-form-item>
            <el-form-item label="手机号:" prop="phone">
              <span>{{form.phone}}</span>
            </el-form-item>
          </div>
          <div style="width: 60%;height: 340px;background: #99a9bf" id="enterpriseContainer">

          </div>
        </div>
        <!--<div class="division" style="width: 100%">-->
          <!--<el-form-item label="经纬度:" prop="longitude">-->
            <!--<span>{{form.longitude}}</span>，<span>{{form.latitude}}</span>-->
          <!--</el-form-item>-->

          <!--&lt;!&ndash;<div class="btn">选择经纬度</div>&ndash;&gt;-->
        <!--</div>-->
        <el-form-item label="地址信息:" prop="address">
          <span>{{form.address}}</span>
        </el-form-item>
        <el-form-item label="性能:" prop="performance">
          <span>{{form.performance}}</span>
        </el-form-item>
        <el-form-item label="用途:" prop="purpose">
          <span>{{form.purpose}}</span>
        </el-form-item>


      </el-form>
    </el-dialog>
    <GetPos
      :dialogVisible.sync="dialogTableVisible"
      device=""
      :devicePos="devicePos"
      @close="dialogcancelFun"
      @getPath="getPath"
    />
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/system/info";
import GetPos from '@/components/GetPos';
import { EditorMap } from "@/utils/mapClass/getPath.js";

export default {
  name: "Info",
  components: {
    GetPos
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急物资管理表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2:false,
      // 分类字典
      materialTypeOptions: [],
      // 地图
      dialogTableVisible: false,
      devicePos: [],
      map: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialName: null,
        materialType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialName: [
          { required: true, message: "物资名称不能为空", trigger: "blur" }
        ],
        materialType: [
          { required: true, message: "分类不能为空", trigger: "change" }
        ],
        num: [
          { required: true, message: "物资数量不能为空", trigger: "blur" }
        ],
        validityTime: [
          { required: true, message: "有效时间不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "经纬度度不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "维度不能为空", trigger: "blur" }
        ],
        contacts: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_material_type").then(response => {
      this.materialTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询应急物资管理列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 分类字典翻译
    materialTypeFormat(row, column) {
      return this.selectDictLabel(this.materialTypeOptions, row.materialType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialName: null,
        materialType: null,
        num: null,
        performance: null,
        purpose: null,
        validityTime: null,
        deptId: null,
        longitude: null,
        latitude: null,
        address: null,
        contacts: null,
        phone: null,
        createTime: null,
        updateTime: null,
        isDelete: 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
    },
    MapdialogFun() {
      this.dialogTableVisible = true;
    },
    dialogcancelFun() {
      this.dialogTableVisible = false;
    },
    getPath(res){
      console.log("res", res);
      console.log(this.form.longitude, this.form.latitude);
      //确认选择经纬度
      this.form.longitude = res[0];
      this.form.latitude = res[1];
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应急物资管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急物资管理";
      });
    },
    //详情
    handleDetail(row) {
      //this.fileList=[];
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.form.materialType = this.materialTypeFormat(this.form,null);
        this.open2 = true;
      this.$nextTick(() => {
        // console.log(document.getElementById("enterpriseContainer"))
        const path = eval(this.$store.state.user.systemSetting.map_center);
        this.map = new EditorMap("enterpriseContainer", {center: path}, this);
        if(this.form.longitude != null){
          this.devicePos = [this.form.longitude, this.form.latitude];
          this.map.addDevice({ path: this.devicePos });
        }
        this.map.nowMouseTarget = null;
        this.map.mousetoolClose(false);
      })

    });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除应急物资数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有应急物资管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>
  .division{
    display:flex;
    flex-direction:row;
    justify-content:flex-start;
  }
  .btn{
    width: 80px;
    background: #00afff;
    color: white;
    font-size: 14px;
    text-align: center;
    height: 36px;
    line-height: 36px;
    border-radius: 5px;
    margin-left: 10px;
    cursor: pointer;

  }
</style>
<style lang="scss" scoped>
  ::v-deep .el-select{
    width: 100%;
  }
  ::v-deep .el-dialog__header{
    border-bottom: 1px solid #ccc;
  }
</style>
