<template>
  <div class="wrapper editwrap">
    <el-dialog
      :visible.sync="dialogVisible"
      :title="title"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form ref="editForm" :model="editForm" :rules="rules" label-width="100px">
        <el-form-item label="图例类型" prop="legendId">
          <el-select v-model="editForm.legendId" placeholder="请选择" @change="selectedFun">
            <el-option
              v-for="item in typeArr"
              :key="item.id"
              :label="item.legendName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="fullName">
          <el-input v-model="editForm.fullName"></el-input>
        </el-form-item>
        <el-form-item label="简称" prop="aliasName">
          <el-input v-model="editForm.aliasName"></el-input>
        </el-form-item>
        <el-form-item label="资源编号" prop="resourceNo">
          <el-input v-model="editForm.resourceNo"></el-input>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="editForm.contacts"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="editForm.telephone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="editForm.address"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="introduce">
          <!-- <el-input v-model="editForm.introduce"></el-input> -->
          <textarea id="editorA" v-model="editForm.introduce"></textarea>
        </el-form-item>
        <el-form-item label="经纬度" prop="longitude">
          <el-row>
            <el-col :span="8">
              <el-input v-model="editForm.longitude" type="number"></el-input>
            </el-col>
            <el-col :span="8">
              <el-input v-model="editForm.latitude" type="number"></el-input>
            </el-col>
            <el-col :span="5">
              <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="地图类型" prop="mapType">
          <el-select v-model="editForm.mapType">
            <el-option value="'1'" label="高德地图"></el-option>
            <el-option value="'2'" label="百度地图"></el-option>
            <el-option value="'3'" label="谷歌地图"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="地图显示类型" prop="showType">
          <el-select v-model="editForm.showType">
            <el-option value="1" label="视频监控"></el-option>
            <el-option value="2" label="移动设备"></el-option>
            <el-option value="3" label="安全生产企业（弹出设备数据状态信息）"></el-option>
            <el-option value="4" label="自然灾害（弹出设备列表）"></el-option>
            <el-option value="5" label="应急物资、应急保障（弹出基本信息）"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备来源" prop="source">
          <el-input v-model="editForm.source"></el-input>
        </el-form-item>
        <el-form-item label="所属地区">
          <City @changeFun="qvChange" :valueqv="qvvalue" :param="dialogVisible" />
        </el-form-item>
        <!-- <el-form-item label="所属单位">
          <el-select v-model="editForm.unitId" placeholder="请选择" @change="selectedBFun">
            <el-option
              v-for="item in deptList"
              :key="item.id"
              :label="item.fullName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="editForm.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">离线</el-radio>
            <el-radio label="2">预警</el-radio>
          </el-radio-group>
        </el-form-item>-->
        <el-form-item label="排序" prop="sort">
          <el-input v-model="editForm.sort"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" autosize v-model="editForm.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="confirmFun('editForm')">确定</el-button>
        <el-button @click="$emit('dialogFun')" size="small">取消</el-button>
      </span>
    </el-dialog>
    <Mapdialog
      v-if="loadmap"
      :dialogTableVisible="dialogTableVisible"
      @dialogcancelFun="dialogcancelFun"
      :slat="editForm.latitude"
      :slng="editForm.longitude"
      @confirm="qrFun"
    />
  </div>
</template>
<style lang="scss">
  .el-col {
    margin-left: 0;
    margin-right: 10px;
  }
</style>
<script lang="ts">
import { Component, Vue, Provide, Prop, Watch } from "vue-property-decorator";
import { State, Getter, Mutation, Action } from "vuex-class";
import City from "@/components/city.vue";
import Mapdialog from "@/components/maplnglat.vue";
import METHOD from "@/utils/methods";

@Component({
  components: { City, Mapdialog },
})
export default class CompanyMgEdit extends Vue {
  // @Getter('signs') signsData: any;
  //属性或参数中使用!:表示强制解析（告诉typescript编译器，这里一定有值），常用于vue-decorator中的@Prop
  @Prop(Boolean) dialogVisible!: Boolean;

  @Prop(Object) editForm!: {
    fullName: String;
    provinceId: String;
    cityId: String;
    countyId: String;
    longitude: Number;
    latitude: Number;
    legendId: String;
    legendCode: String;
    plegendId: String;
    plegendCode: String;
    unitName: String;
    introduce: String;
  };
  @Prop(String) title!: String;

  @Provide() qvvalue: any = []; //区域值

  @Provide() signsArr: any = []; //图例list

  @Provide() typeArr: any = [];

  @Provide() deptList: any = []; //单位

  @Provide() dialogTableVisible: Boolean = false;

  @Provide() rules: any = {
    fullName: [{ required: true, trigger: "blur", message: "该项为必填项！" }],
    aliasName: [{ required: true, trigger: "blur", message: "该项为必填项！" }],
    legendId: [{ required: true, trigger: "blur", message: "该项为必填项！" }],
    deviceNo: [{ required: true, trigger: "blur", message: "该项为必填项！" }],
    address: [{ required: true, trigger: "blur", message: "该项为必填项！" }],
    longitude: [{ required: true, trigger: "blur", message: "该项为必填项！" }],
    latitude: [{ required: true, trigger: "blur", message: "该项为必填项！" }],
  };

  @Provide() loadmap: Boolean = false;
  @Provide() editorA: any;

  @Watch("editForm") formChange(row: any) {
    let varr = [];
    if (this.title == "编辑") {
      varr.push(row.provinceId);
      varr.push(row.cityId);
      varr.push(row.countyId);
    }
    this.qvvalue = varr;
  }
  selectedFun(id: any) {
    let arr = this.typeArr.filter((ele: any) => ele.id == id);
    this.editForm.legendCode = arr[0].legendCode;
    this.editForm.plegendCode = arr[0].plegendCode;
    this.editForm.plegendId = arr[0].parentId;
  }
  selectedBFun(id: any) {
    let arr = this.deptList.filter((ele: any) => ele.id == id);
    this.editForm.unitName = arr[0].fullName;
  }
  qrFun(v: any) {
    //确认选择经纬度
    this.editForm.longitude = v.lng;
    this.editForm.latitude = v.lat;
  }
  MapdialogFun() {
    this.loadmap = false;
    this.$nextTick(() => {
      this.loadmap = true;
    });
    this.dialogTableVisible = true;
  }
  dialogcancelFun() {
    this.dialogTableVisible = false;
  }
  //取消事件要在父级取消是所以用到 $emit
  confirmFun(formName: string) {
    let that = this,
      params = that.editForm;
      that.editForm.introduce = that.editorA.getHTML();
    (that as any).$refs[formName].validate((valid: boolean) => {
      if (valid) {
        METHOD.addListData(that, params, function (res: any) {
          (that as any).$message({
            message: "操作成功！",
            type: "success",
          });
          (that as any).$emit("dialogFun");
          that.$emit("refreshTableData"); //刷新表格
        });
      } else {
        return false;
      }
    });
  }
  qvChange(v: any) {
    switch (v.length) {
      case 1:
        this.editForm.provinceId = v[0];
        break;
      case 2:
        this.editForm.provinceId = v[0];
        this.editForm.cityId = v[1];
        break;
      case 3:
        this.editForm.provinceId = v[0];
        this.editForm.cityId = v[1];
        this.editForm.countyId = v[2];
        break;
    }
  }
  //获取图例类型
  getTypeData() {
    let that = this,
      id = this.$route.matched[1].meta.listId;
    (that as any).$axios
      .get(`/legendInfo/getChildLegendInfoByParentId/${id}`)
      .then((res: any) => {
        that.typeArr = res;
      });
  }
  //创建编辑器
  creatEditor() {
    this.editorA = new ice.editor("editorA");
    //自定义编辑器工具栏菜单（默认展示所有功能）
    this.editorA.menu = [
      "fontSize",
      "foreColor",
      "bold",
      "italic",
      "underline",
      "strikeThrough",
      "line",
      "justifyLeft",
      "justifyCenter",
      "justifyRight",
      "line",
      "table",
      "insertImage",
      "insertOrderedList",
      "insertUnorderedList",
      "indent",
    ];
    this.editorA.height = "200px";
    this.editorA.create();
  }
  created() {
    // this.signsArr = [...this.signsData];
    let that = this;
    METHOD.deptList(that, function (res: any) {
      that.deptList = res;
    });
    that.getTypeData();
    let varr = [],
      row = that.editForm;
    if (that.title == "编辑" && row.provinceId && row.cityId && row.countyId) {
      varr.push(row.provinceId);
      varr.push(row.cityId);
      varr.push(row.countyId);
      that.qvvalue = varr;
    } else {
      that.qvvalue = [];
    }
    that.$nextTick(() => {
      that.creatEditor();
      if(that.editForm.introduce ){
        that.editorA.setValue(that.editForm.introduce);
      }
    });
  }
}
</script>
