<template>
  <el-dialog :close-on-click-modal="false" id="staffManageDialog" :title="title" :visible.sync="dialogVisible">
    <el-form ref="item" :model="item" :rules="rules" label-width="auto" class="content">
      <el-form-item label="名称" prop="name">
        <el-input v-model="item.name" clearable />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="item.sort"
          min="0"
          type="number"
          on-keypress="return(/[\d]/.test(String.fromCharCode(event.keyCode)))" />
      </el-form-item>
      <!-- <el-form-item label="图标">
        <el-popover width="400" v-model="isIconShow" trigger="focus">
          <ul>
            <li v-for="(item, index) in iconList" :key="index">
              <i class="iconfont" :class="item.className" @click="selectIcon(item.className)" />
            </li>
          </ul>
          <span slot="reference">
            <el-input v-model="item.ico" clearable />
          </span>
        </el-popover>
      </el-form-item> -->
      <el-form-item label="类型">
        <el-select v-model="item.type" clearable class="w100" placeholder="请选择">
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="链接" prop="url">
        <el-input v-model="item.url" clearable />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit('item')">确认</el-button>
    </span>
  </el-dialog>
</template>
<script>
// import '@/assets/font1/iconfont.css'
import { getNavigationIdShow, saveNavigation } from '@/api/system_manage'

export default {
  name: 'ManageDialog',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    item: {
      type: Object,
      default: () => {
        return {}
      }
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    const sortPudge = (rule, value, callback) => {
      if (this.item.sort && this.item.sort.length > 10) {
        callback(new Error('排序超长'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        name: [
          { required: true, message: '名字不能为空', trigger: 'blur' },
          { max: 100, message: '名字超长', trigger: 'blur' }
        ],
        sort: [{ validator: sortPudge, trigger: 'blur' }]
      },
      dialogVisible: this.value,
      iconList: [],
      isIconShow: false,
      typeList: [
        {
          value: 'category',
          label: '导航分类'
        },
        {
          value: 'item',
          label: '导航项'
        }
      ]
    }
  },
  watch: {
    value(val) {
      this.dialogVisible = val
      // console.log(this.$store.state)
      // this.$nextTick(function () {
      //   this.iconList = this.$store.state.icon.iconList
      // })
    },
    dialogVisible(val) {
      this.$emit('input', val)
    }
  },
  methods: {
    selectIcon(className) {
      this.isIconShow = false
      this.$set(this.item, 'ico', className)
    },
    // getData () {
    //   getNavigationIdShow(this.id)
    //     .then(rt => {
    //       if (rt.status === '200') {
    //         this.item = rt.data
    //       }
    //       this.loading = false
    //     })
    //     .catch(() => {
    //       this.loading = false
    //     })
    // },
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          saveNavigation(this.item)
            .then(rt => {
              if (rt.status === '200') {
                this.$message.success(`${this.title}成功`)
                this.dialogVisible = false
                this.$emit('confirm', this.item)
              }
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    cancel() {
      this.dialogVisible = false
    },
    addList() {
      this.item.navigationMainDtoList.push({})
    },
    deleData(index) {
      this.item.navigationMainDtoList.splice(index, 1)
    }
  }
}
</script>

<style scoped>
  .content {
    width: 60%;
    margin: 0 auto;
    overflow: auto;
    padding: 0 10px;
  }

  .w100 {
    width: 100%;
  }

  .el-popover li {
    display: inline-block;
    vertical-align: top;
  }

  .el-popover li i {
    width: 30px;
    height: 30px;
    line-height: 30px;
    list-style: none;
    display: block;
    text-align: center;
    margin-top: 10px;
    cursor: pointer;
    -webkit-transition: font-size 0.25s linear, width 0.25s linear;
    -moz-transition: font-size 0.25s linear, width 0.25s linear;
    transition: font-size 0.25s linear, width 0.25s linear;
  }

  .el-popover li:hover i {
    font-size: 30px;
  }
</style>
<style lang="scss">
  #staffManageDialog {
    .avatar-uploader .el-upload {
      border: 1px dashed $dc1;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }

    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }

    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }

    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
</style>
