<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="'添加图案操作'"
      :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-img-quick">
        <div class="t">快速选择图标</div>
        <div :class="curImgUrl === item.url ? 'op cur' : 'op'" v-for="(item, i) in imgArr" @click="imgHandle(item)" :key="i">
          <img :src="item.url" alt="img" class="img">
        </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,
  },
  data() {
    return {
      loading: false,
      formData: [],
      curImgUrl: '',
      imgArr: [
        {url: 'https://img2.honglounews.com/20221205054341-4368.png'},
        {url: 'https://img2.honglounews.com/20221205054412-6635.png'},
      ],
      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.icon = this.curImgUrl
      }
      if (!params.text) params.text = '标题示例'
      if (!params.icon) params.icon = 'https://img2.honglounews.com/20221205054341-4368.png'
      this.formData = [
        { label: '站点', key: 'text', rules: 1},
        { label: '图标', key: 'icon', type: 'uploads', rules: 1 },
      ]
      this.setDefaultValue(params)
    },
    imgHandle (item) {
      this.curImgUrl = item.url
      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-img-quick {
  margin-bottom: 20px;
  .t {
    color: #313131;
  }
  .op {
    display: inline-block;
    vertical-align: middle;
    width: 44px;
    height: 44px;
    color: #fff;
    cursor: pointer;
    text-align: center;
    margin-right: 10px;
    &.cur {
      border: 2px solid #19be6b;
    }
    .img {
      width: 40px;
      height: 40px;
    }
  }
}
</style>
