<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"  ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="上级机构" prop="parentName" v-if="!dataForm.id">
        <el-popover ref="dtListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="dtList"
            :props="dtListTreeProps"
            node-key="id"
            ref="dtParentTree"
            @current-change="dtListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:dtListPopover
          :readonly="true"
          placeholder="点击选择上级单位"
        ></el-input>
      </el-form-item>
      <el-form-item label="编码" prop="code" v-if="!dataForm.id">
        <el-input v-model="dataForm.code" placeholder="编码"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="linkman">
        <el-input v-model="dataForm.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils"
export default {
  data () {
    return {
      visible: false,
      isRoot: false,
      dataForm: {
        id: 0,
        parentName: "",
        parentId: '',
        code: '',
        name: '',
        linkman: '',
        phone: '',
        email: '',
        message: '',
        wechat: '',
        orderNum: '',
        delFlag: '',
        ptId: '',
        ptName: ''
      },
      dts: [],
      dtList: [],
      dtListTreeProps: {
        label: "name",
        children: "children"
      }
    }
  },
  methods: {
    // 分销商树选中
    dtListTreeCurrentChangeHandle(data, node) {
      this.dataForm.parentId = data.id
      this.dataForm.parentName = data.name
    },
    // 分销商树设置当前选中节点
    dtListTreeSetCurrentNode() {
      this.$refs.dtParentTree.setCurrentKey(this.dataForm.parentId)
    },
    init (id) {
      this.isRoot = false
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl("/dt/dt/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 100000
        })
      })
        .then(({ data }) => {
          this.dtList = treeDataTranslate(data.page.list, "id")
        })
        .then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/dt/dt/info/${this.dataForm.id}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm.parentId = data.dt.parentId
                  // 根目录
                  if (this.dataForm.parentId === 0) {
                    this.isRoot = true
                  } else {
                    this.isRoot = false
                  }
                  this.dataForm.parentName = data.dt.parentName
                  this.dataForm.code = data.dt.code
                  this.dataForm.name = data.dt.name
                  this.dataForm.linkman = data.dt.linkman
                  this.dataForm.phone = data.dt.phone
                  this.dataForm.email = data.dt.email
                  this.dataForm.message = data.dt.message
                  this.dataForm.wechat = data.dt.wechat
                  this.dataForm.startDate = data.dt.startDate
                  this.dataForm.endDate = data.dt.endDate
                  this.dataForm.orderNum = data.dt.orderNum
                  this.dataForm.delFlag = data.dt.delFlag
                }
                // this.dtListTreeSetCurrentNode()
              })
            }
          })
        })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/dt/dt/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'parentId': this.dataForm.parentId,
              'code': this.dataForm.code,
              'name': this.dataForm.name,
              'linkman': this.dataForm.linkman,
              'phone': this.dataForm.phone,
              'email': this.dataForm.email,
              'message': this.dataForm.message,
              'wechat': this.dataForm.wechat,
              'startDate': this.dataForm.startDate,
              'endDate': this.dataForm.endDate,
              'orderNum': this.dataForm.orderNum,
              'delFlag': this.dataForm.delFlag
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
