<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="mteStr === 'polygonAdd' || mteStr === 'polylineAdd' ? '多边形或折线操作' : '文字操作'"
      :fullscreen="false"
      width="400px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" :data="formData" :is-inline="false" label-width="70px">
      </base-form>
      <div class="scoped-color-quick" v-if="mteStr === 'polygonAdd' || mteStr === 'polylineAdd'">
        <div class="t">快速选择颜色</div>
        <div :class="fillColor === item.color ? 'op cur' : 'op'" v-for="(item, i) in colorArr" @click="colorHandle(item)" :style="`background: ${item.color}`" :key="i">{{item.t}}</div>
        <div style="padding-top: 10px;">
          <div :class="fillColor === item.color ? 'op cur' : 'op'" v-for="(item, i) in colorArr2" @click="colorHandle(item)" :style="`background: ${item.color}`" :key="i">{{item.t}}</div>
        </div>
      </div>
      <div class="xl-form">
        <div class="xl-form-footer">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button v-if="curObj.obj" class="xl-form-btn t1" @click="close('confirm')">保存</el-button>
          <el-button v-else class="xl-form-btn t3" @click="close('confirm')">去画图</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object,
    mteStr: String,
  },
  data() {
    return {
      loading: false,
      formData: [],
      fillColor: '#ff003f',
      strokeColor: '#ff003f',
      colorArr: [
        {color: '#ff0', t: '新楼盘'}, 
        {color: '#bb9c2c', t: '二手房'}, 
        {color: '#e8d0e6', t: '还建房'}, 
        {color: '#ff9149', t: '幼儿园'}, 
        {color: '#ff7fbf', t: '小初中'}, 
        // {color: '#60c68b', t: ''}, 
        {color: '#e77ad4', t: '高中'},
        {color: '#fb6557', t: '专本科'}, 
      ],
      colorArr2: [
        {color: '#ff003f', t: '纯商业'},
        {color: '#999', t: '商综合'},
        {color: '#007299', t: '公共'}, 
        {color: '#00ff3f', t: '公园'}, 
        {color: '#4da4ee', t: '医院'}, 
      ],
      cObj: {},
      estateObj: {},
      schoolObj: {},
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.estateObj = {}
        this.schoolObj = {}
        this.cObj = JSON.parse(JSON.stringify(this.curObj))
        this.getDef()
      }
    },
  },
  methods: {
    getDef (str) {
      let params = { ...this.curObj.obj }
      let remoteEstateOptions = []
      if (params.estateObj) {
        let estateObj = {}
        if (typeof(params.estateObj) === 'object') {
          estateObj = params.estateObj
        } else {
          estateObj = JSON.parse(params.estateObj)
        }
        if (estateObj.id) {
          params.estate_id = estateObj.id
          remoteEstateOptions = [{ keyRO: estateObj.estate_name, valRO: estateObj.id }]
        }
      }
      let remoteSchoolOptions = []
      if (params.schoolObj) {
        const schoolObj = JSON.parse(params.schoolObj)
        if (schoolObj.id) {
          params.school_id = schoolObj.id
          remoteSchoolOptions = [{ keyRO: schoolObj.school_name, valRO: schoolObj.id }]
        }
      }
      if (str === 'color') {
        const oldform = this.$refs.ruleForm.baseForm
        params = {...oldform}
        params.fillColor = this.fillColor
        params.strokeColor = this.strokeColor
      }
      if (str === 'estate') {
        const oldform = this.$refs.ruleForm.baseForm
        params = {...oldform}
        params.text = this.estateObj.estate_name
        remoteEstateOptions = [{ keyRO: this.estateObj.estate_name, valRO: this.estateObj.id }]
        params.school_id = ''
      }
      if (str === 'school') {
        const oldform = this.$refs.ruleForm.baseForm
        params = {...oldform}
        params.text = this.schoolObj.school_name
        remoteSchoolOptions = [{ keyRO: this.schoolObj.school_name, valRO: this.schoolObj.id }]
        params.estate_id = ''
      }
      if (!params.text) params.text = '标题示例'
      if (!params.strokeStyle) params.strokeStyle = 'solid'
      if (this.mteStr === 'polygonAdd' || this.mteStr === 'polylineAdd') {
        if (!params.fillColor) {
          params.fillColor = this.fillColor
          params.fillOpacity = '0.4'
          params.strokeColor = this.strokeColor
        }
        this.formData = [
          { label: '关联楼盘', key: 'estate_id', type: 'selectRemote', changeHandle: this.estateChange,
            remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
            remoteOptions: remoteEstateOptions
          },
          { label: '关联学校', key: 'school_id', type: 'selectRemote', changeHandle: this.schoolChange,
            remoteParams: { skey: 'school_name', api: `school.admschoollist`, opKey: 'school_name', opVal: 'id' },
            remoteOptions: remoteSchoolOptions
          },
          { label: '标题', key: 'text', rules: 1},
          { label: '填充色', key: 'fillColor', type: 'colorPicker', rules: 1},
          { label: '透明度', key: 'fillOpacity', rules: 1},
          { label: '轮廓色', key: 'strokeColor', type: 'colorPicker', rules: 1},
          { label: '实虚线', key: 'strokeStyle', type: 'select', options: [{key: '实线', val: 'solid'}, {key: '虚线', val: 'dashed'}]},
        ]
      } else {
        if (!params.color) {
          params.color = '#fff'
        }
        this.formData = [
          { label: '内容', key: 'text', rules: 1},
          { label: '颜色', key: 'color', type: 'colorPicker', rules: 1},
        ]
      }
      this.setDefaultValue(params)
    },
    estateChange (val, op, valObj) {
      this.estateObj = {
          id: valObj.id,
          estate_name: valObj.estate_name,
          pri_image: valObj.pri_image,
          price_range: valObj.price_range,
          product_type: valObj.product_type,
          // property_type: valObj.property_type,
          // area_type: valObj.area_type,
          // metro_type: valObj.metro_type,
          // metro_line: valObj.metro_line,
          // vr_image: valObj.vr_image,
          // vr_key: valObj.vr_key,
        }
      this.getDef('estate')
    },
    schoolChange (val, op, valObj) {
      this.schoolObj = {
        id: valObj.id,
        school_name: valObj.school_name,
        pri_image: valObj.pri_image,
        school_attrib: valObj.school_attrib,
        school_type: valObj.school_type,
        remarked: valObj.remarked,
      }
      this.getDef('school')
    },
    colorHandle (item) {
      this.fillColor = item.color
      this.strokeColor = item.color
      this.getDef('color')
    },
    close (str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const newForm = { ...oldform }
            if (this.estateObj && this.estateObj.id) {
              newForm.estateObj = JSON.stringify(this.estateObj)
            }
            if (this.schoolObj && this.schoolObj.id) {
              newForm.schoolObj = JSON.stringify(this.schoolObj)
            }
            if (this.curObj.obj) {
              this.$emit('close', newForm, 'edit')
            } else {
              this.$emit('close', newForm)
            }
            this.setDefaultValue()
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/libEdit.scss';
.lib-edit {
  padding-top: 0;
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
  ::v-deep .el-textarea__inner {
    height: 300px;
  }
}

.scoped-color-quick {
  margin-bottom: 20px;
  .t {
    color: #313131;
  }
  .op {
    display: inline-block;
    vertical-align: middle;
    width: 40px;
    height: 26px;
    line-height: 26px;
    color: #fff;
    cursor: pointer;
    text-align: center;
    margin-right: 10px;
    opacity: .6;
    color: #000;
    user-select: none;
    font-size: 12px;
    &.cur {
      opacity: 1;
      color: #fff;
    }
  }
}
</style>
