<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="groupName">
        <el-input v-model="dataForm.groupName" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item v-if="!this.dataForm.id"  label="平台" prop="pfName">
        <el-select v-model="dataForm.blonPlantform" placeholder="请选择平台" clearable="true"  @change="pfChange">
          <el-option
            v-for="item in pfs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="!this.dataForm.id" label="店铺名称" prop="stName">
        <el-select v-model="dataForm.store" placeholder="请选择所属店铺" clearable="true" >
          <el-option
            v-for="item in stores"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </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>
export default {
  data () {
    return {
      limitUpload: 1,
      visible: false,
      dataForm: {
        id: 0,
        groupName: '',
        dtId: '',
        store: '',
        blonPlantform: '',
        isDirect: '',
        isRoomMap: '',
        hotelIds: [],
        pfName: '',
        dtName: '',
        stName: ''
      },
      stores: [],
      pfs: [],
      dataRule: {
        groupName: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 选择销售平台
    pfChange() {
      this.getStore()
    },
    // 查询店铺信息
    getStore() {
      this.$http({
        url: this.$http.adornUrl("/op/group/store"),
        method: "get",
        params: this.$http.adornParams({
          dtId: this.dataForm.dtId,
          pf: this.dataForm.blonPlantform
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.stores = data.list
        }
      })
    },
    // 页面跳转初始化
    init (id, dtId, pfs) {
      console.info("修改页面初始化")
      this.dataForm.id = id || 0
      this.dataForm.dtId = dtId
      this.pfs = pfs
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/op/group/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.groupName = data.group.groupName
              this.dataForm.dtId = data.group.dtId
              this.dataForm.storeId = data.group.storeId
              this.dataForm.blonPlantform = data.group.blonPlantform
              this.dataForm.isDirect = data.group.isDirect
              this.dataForm.isRoomMap = data.group.isRoomMap
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/op/group/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'groupName': this.dataForm.groupName,
              'dtId': this.dataForm.dtId,
              'storeId': this.dataForm.store,
              'blonPlantform': this.dataForm.blonPlantform,
              'isDirect': this.dataForm.isDirect,
              'isRoomMap': this.dataForm.isRoomMap,
              'dtName': this.dataForm.dtName,
              'pfName': this.dataForm.pfName,
              'stName': this.dataForm.stName
            })
          }).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>
