<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :before-close="handleCancel"
    class="assign-dialog"
    title="重置客户消息接收状态"
    width="450px"
  >
    <div class="form-wrap" v-loading="loading">
      重置后该客服下所有客户状态均变更为【可接收消息】<br/><br/>
      后续根据群发结果重新判断客户可接收消息状态。
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="handleCancel">取 消</el-button>
      <el-button size="mini" type="primary" @click="handleConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "resetReceiveDialog",
  props: {
    // 控制弹框是否显示
    dialogVisible: {
      type: Boolean,
      default: () => false
    },
    // 成员信息
    userInfo: {
      type: Object,
      default: () => ({
        user_id: '',
        name: '',
      })
    },
  },
  data() {
    return {
      loading: false,
      form: {
        corpid: '',
        userid: '',
      }
    }
  },
  watch: {
    dialogVisible(isShow) {
      if (isShow) {
        // 获取表单数据
        this.handleGetFormData()
      }
    },
  },
  methods: {
    // 获取弹框表单数据
    handleGetFormData() { 
      const { user_id = '' } = this.userInfo
      this.form.user_id = user_id
      console.log(this.form.userid)
    },
    // 清空弹框表单数据
    handleClearFormData() {
      this.form.user_id = ''
    },
    async handleConfirm() {
      try {
        const url = `${this.URL.BASEURL}${this.URL.member_resetCustomerReceiveStatus}`
        const params = {
          ...this.form,
          corpid: this.$localSelfStore.getLocal('defaultCorp') && this.$localSelfStore.getLocal('defaultCorp') != 'undefined' ? JSON.parse(this.$localSelfStore.getLocal('defaultCorp')).corpid : '',
        }
        this.loading = true
        const { data: res = {} } = await this.$axios.get(url, { params })
        if (res && res.errno == 0) {
          this.$message.success('操作成功')
          this.handleClearFormData()
          this.$emit('confirm')
        } else if (res.errno != 4002) {
          this.$message.warning(res.err || '操作失败')
          this.handleCancel()
        }
      } catch (error) {
        console.log('error => ', error)
      } finally {
        this.loading = false
      }
    },
    handleCancel() {
      this.handleClearFormData()
      this.$emit('cancel')
    },
  },
};
</script>

<style lang="scss" scoped>
.assign-dialog {
  .form-wrap {
    padding: 0 10px;
  }
  .dialog-footer {
    text-align: right;
  }
}
</style>
