<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.ptsid ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false" width="70%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="名称" prop="name">
          <el-input v-model="dataForm.name" placeholder=""></el-input>
      </el-form-item>
          <el-form-item label="上级" prop="parentid">
          <el-popover v-model="typeListVisible" ref="typeListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="typeList"
            :props="{ label: 'name', children: 'children' }"
            node-key="id"
            ref="typeListTree"
            :highlight-current="true"
            :expand-on-click-node="false"
            accordion
            @current-change="typeListTreeCurrentChangeHandle">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:typeListPopover :readonly="true" placeholder="上级">
          <i v-if="dataForm.parentId !== '0'" slot="suffix" @click.stop="typeListTreeSetDefaultHandle()" class="el-icon-circle-close el-input__icon"></i>
        </el-input>
      </el-form-item>
          <el-form-item label="图标" prop="thumbnail">
            <upload-img ref="uploadImg"></upload-img>
      </el-form-item>
          <el-form-item label="排序" prop="sort">
          <el-input v-model="dataForm.sort" placeholder=""></el-input>
      </el-form-item>
          </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import UploadImg from '../utils/upload-img'
export default {
  data () {
    return {
      visible: false,
      typeListVisible:false,
      typeList:[],
      dataForm: {
        ptsid: '',
        zntid: '',
        name: '',
        parentid: '',
        selectid: '',
        link: '',
        thumbnail: '',
        sort: '',
        creator: '',
        createDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        zntid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        parentid: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        selectid: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        link: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        thumbnail: [
          { required: false, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getTypeList().then(() => {
          if (this.dataForm.id) {
            this.getInfo()
          }else{
            this.$refs['uploadImg'].imgSrc = "";
            this.dataForm.parentName = "一级栏目"
            this.dataForm.parentId = 0
         }
        })
      })
    },
    // 获取类型列表
    getTypeList () {
      return this.$http.get('/cniyv/cniyvproducttype/list').then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.typeList = res.data
      }).catch(() => {})
    },
    // 上级菜单树, 选中
    typeListTreeCurrentChangeHandle (data) {
      this.dataForm.parentId = data.ptsid
      this.dataForm.parentName = data.name
      this.typeListVisible = false
    },
    // 上级菜单树, 设置默认值
    typeListTreeSetDefaultHandle () {
      this.dataForm.pid = '0'
      this.dataForm.parentName = this.$t('menu.parentNameDefault')
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/cniyv/cniyvproducttype/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }

        this.$refs.typeListTree.setCurrentKey(this.dataForm.parentId)

        this.$refs['uploadImg'].imgSrc = this.dataForm.thumbnail;

      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.ptsid ? 'post' : 'put']('/cniyv/cniyvproducttype/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  },
  components: {
    UploadImg 
  }
}
</script>
