<template>
  <el-dialog title="违规管理"
             :close-on-click-modal="false"
             :visible.sync="visible"
             width="35%">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="处理人"
                    prop="handler">
        <el-input v-model="dataForm.handler"
                  :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="下线原因"
                    prop="offlineReason">
        <el-input type="textarea"
                  :autosize="{ minRows: 2, maxRows: 10}"
                  maxlength="200"
                  v-model="dataForm.offlineReason"
                  :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="下线时间"
                    prop="createTime">
        <el-input v-model="dataForm.createTime"
                  :disabled="true"></el-input>
      </el-form-item>

      <el-form-item v-if="isAudit"
                    label="申请理由"
                    prop="applyReason">
        <el-input type="textarea"
                  :autosize="{ minRows: 2, maxRows: 10}"
                  maxlength="200"
                  v-model="dataForm.applyReason"
                  :disabled="true"></el-input>
      </el-form-item>

      <el-form-item v-if="dataForm.status>1"
                    label="处理状态"
                    prop="status">
        <!-- 1平台进行下线 2 重新申请，等待审核 3.审核通过 4 审核未通过 -->
        <el-radio-group v-model="dataForm.status"
                        :disabled="!isAudit">
          <el-radio :label="2">待审核</el-radio>
          <el-radio :label="3">审核通过</el-radio>
          <el-radio :label="4">审核不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="dataForm.status == 4"
                    label="拒绝原因"
                    prop="refuseReason">
        <el-input :disabled="!isAudit"
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 10}"
                  maxlength="200"
                  v-model="refuseReason"></el-input>
      </el-form-item>

    </el-form>
    <el-divider></el-divider>
    <el-form>
      <div>
        <div style="margin-bottom:10px"><strong>历史申请</strong></div>
        <div style=""
             v-for="item in dataForm.offlineHandleEventItemList"
             :key="item.eventItemId">
          <el-row type="flex"
                  justify="space-around">
            <el-col :span="3"><strong>申请时间：</strong></el-col>
            <el-col :span="24"><span class="content-color">{{item.createTime}}</span></el-col>
          </el-row>
          <el-row type="flex"
                  justify="space-around">
            <el-col :span="3"><strong>申请理由：</strong></el-col>
            <el-col :span="24"><span class="content-color">{{item.reapplyReason}}</span></el-col>
          </el-row>
          <el-row type="flex"
                  justify="space-around"
                  v-if="item.auditTime">
            <el-col :span="3"><strong>审核时间：</strong></el-col>
            <el-col :span="24"><span class="content-color">{{item.auditTime}}</span></el-col>
          </el-row>
          <el-row type="flex"
                  justify="space-around"
                  v-if="item.refuseReason">
            <el-col :span="3">
              <div><strong>拒绝原因：</strong></div>
            </el-col>
            <el-col :span="24">
              <div><span class="content-color">{{item.refuseReason}}</span></div>
            </el-col>
          </el-row>
          <div style="width:50%">
            <el-divider />
          </div>
        </div>
      </div>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false"
                 size="mini">取消</el-button>
      <el-button type="primary"
                 size="mini"
                 :disabled="!isAudit"
                 @click="dataFormSubmit()">确认审核</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        eventId: null,
        handleType: null,
        handleId: null,
        shopId: null,
        handlerId: null,
        status: null,
        offlineReason: null,
        applyReason: null,
        eventItemId: null,
        lastOfflineHandleEventItem: null
      },
      isAudit: false,
      refuseReason: '',
      id: 0,
      dataRule: {
      }
    }
  },
  props: {
    selectUrl: {
      default: '',
      type: String
    },
    updateUrl: {
      default: '',
      type: String
    }
  },
  methods: {
    init (id) {
      this.id = id || 0
      this.isAudit = false
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.id) {
          this.$http({
            url: this.$http.adornUrl(this.selectUrl + '/' + this.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (!data) {
              this.$message({
                message: '未找到该违规信息,请稍后重试',
                type: 'error',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
            this.dataForm = data
            let lastOfflineHandleEventItem = data.lastOfflineHandleEventItem
            if (lastOfflineHandleEventItem) {
              this.dataForm.applyReason = lastOfflineHandleEventItem.reapplyReason
              this.dataForm.eventItemId = lastOfflineHandleEventItem.eventItemId
              this.refuseReason = lastOfflineHandleEventItem.refuseReason
            }
            if (data.status === 2) {
              this.refuseReason = ''
              this.isAudit = true
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$confirm('确认对活动/商品进行审核吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.visible = false
        this.$http({
          url: this.$http.adornUrl(this.updateUrl),
          method: 'post',
          data: this.$http.adornData({
            eventId: this.dataForm.eventId,
            eventItemId: this.dataForm.eventItemId,
            handleId: this.id,
            status: this.dataForm.status,
            refuseReason: this.refuseReason || null
          })
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        })
      })
    }
  }
}
</script>
<style  lang="scss" scoped>
.content-color {
  color: #4395ff;
}
</style>
