<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="mteStr === 'polygonAdd' ? '多边形操作' : '文字操作'"
      :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'">
        <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">{{fillColor === item.color ? '已选' : ' '}}</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: '#DC3021',
      strokeColor: '#DC3021',
      colorArr: [
        {color: '#fb6557'}, 
        {color: '#ff9149'}, 
        {color: '#9f7df4'}, 
        {color: '#60c68b'}, 
        {color: '#4da4ee'}, 
        {color: '#e77ad4'}
      ],
      cObj: {},
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.cObj = JSON.parse(JSON.stringify(this.curObj))
        this.getDef()
      }
    },
  },
  methods: {
    getDef (str) {
      let params = { ...this.curObj.obj }
      if (str === 'color') {
        const oldform = this.$refs.ruleForm.baseForm
        params = {...oldform}
        params.fillColor = this.fillColor
        params.strokeColor = this.strokeColor
      }
      if (!params.text) params.text = '标题示例'
      if (this.mteStr === 'polygonAdd') {
        if (!params.fillColor) {
          params.fillColor = this.fillColor
          params.fillOpacity = '0.4'
          params.strokeColor = this.strokeColor
        }
        this.formData = [
          { label: '标题', key: 'text', rules: 1},
          { label: '填充色', key: 'fillColor', type: 'colorPicker', rules: 1},
          { label: '透明度', key: 'fillOpacity', rules: 1},
          { label: '轮廓色', key: 'strokeColor', type: 'colorPicker', rules: 1},
        ]
      } else {
        if (!params.color) {
          params.color = '#fff'
        }
        this.formData = [
          { label: '内容', key: 'text', rules: 1},
          { label: '颜色', key: 'color', type: 'colorPicker', rules: 1},
        ]
      }
      this.setDefaultValue(params)
    },
    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.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;
  }
}
</style>
