<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="店铺名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="APPKEY" prop="appKey">
        <el-input v-model="dataForm.appKey" placeholder="APPKEY"></el-input>
      </el-form-item>
      <el-form-item label="SECRET" prop="secret">
        <el-input v-model="dataForm.secret" placeholder="secret"></el-input>
      </el-form-item>
      <el-form-item label="SESSION" prop="sessionKey">
        <el-input v-model="dataForm.sessionKey" placeholder="sessionkey"></el-input>
      </el-form-item>
      <el-form-item label="所属机构">
        <el-popover ref="dtListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="dtList"
            :props="dtListTreeProps"
            node-key="id"
            ref="dtTree"
            @current-change="dtListTreeCurrentChange"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
            style="width:194px"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.dtName"
          v-popover:dtListPopover
          :readonly="true"
          placeholder="点击选择所属机构"
          style="width:194px"
        ></el-input>
      </el-form-item>
      <el-form-item label="所属平台" prop="pfName">
        <el-select v-model="dataForm.blonPlantform" placeholder="请选择所属平台">
          <el-option
            v-for="item in pfs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            @change="pfChangeHandel"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="回调用户名" prop="aliCbUser">
        <el-input v-model="dataForm.aliCbUser" placeholder="飞猪回调接口用户"></el-input>
      </el-form-item>
      <el-form-item label="回调密码" prop="aliCbUserPass">
        <el-input v-model="dataForm.aliCbUserPass" 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,
      dataForm: {
        id: 0,
        name: '',
        appKey: '',
        secret: '',
        sessionKey: '',
        blonPlantform: '',
        dtId: '',
        aliCbUser: '',
        aliCbUserPass: '',
        pfName: '',
        dtName: ''
      },
      dtListTreeProps: {
        label: 'name',
        children: 'children'
      },
      pfs: [],
      dts: [],
      dtList: [],
      dataRule: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        appKey: [
          { required: true, message: 'APPKEY不能为空', trigger: 'blur' }
        ],
        secret: [
          { required: true, message: 'SECRET不能为空', trigger: 'blur' }
        ],
        sessionKey: [
          { required: true, message: 'SESSION不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 所属銷售平台
    pfChangeHandel(value) {
      this.dataForm.blonPlantform = value.id
      this.dataForm.pfName = value.label
    },
    // 分销商树选中
    dtListTreeCurrentChange(data, node) {
      this.dataForm.dtId = data.id
      this.dataForm.dtName = data.name
    },
    // 分销商设置当前选中节点
    dtListTreeSetCurrentNode() {
      if (this.dataForm.dtId != null) {
        this.$refs.dtTree.setCurrentKey(this.dataForm.dtId)
        this.dataForm.dtName = (this.$refs.dtTree.getCurrentNode() || {})['name']
      }
    },
    init (id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/op/group/dtpf'),
        method: 'get'
      }).then(({ data }) => {
        this.dtList = treeDataTranslate(data.dts, 'id')
        this.pfs = data.spfs
        if (this.pfs.length > 0) {
          this.dataForm.blonPlantform = this.pfs[0].id
        }
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/dt/store/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.store.name
                this.dataForm.appKey = data.store.appKey
                this.dataForm.secret = data.store.secret
                this.dataForm.sessionKey = data.store.sessionKey
                this.dataForm.blonPlantform = data.store.blonPlantform
                this.dataForm.dtId = data.store.dtId
                this.dataForm.aliCbUser = data.store.aliCbUser
                this.dataForm.aliCbUserPass = data.store.aliCbUserPass
              }
            })
          }
        })
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/dt/store/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'name': this.dataForm.name,
              'appKey': this.dataForm.appKey,
              'secret': this.dataForm.secret,
              'sessionKey': this.dataForm.sessionKey,
              'blonPlantform': this.dataForm.blonPlantform,
              'dtId': this.dataForm.dtId,
              'aliCbUser': this.dataForm.aliCbUser,
              'aliCbUserPass': this.dataForm.aliCbUserPass,
            })
          }).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>
