<template>
  <!-- 网元创建对话框 -->
  <el-dialog @close="close"
             width='450px'
             :title="$t('ReportTask.copyReportTask')"
             v-dialogDrag
             :visible.sync="createAndCopyDeviceVisible">
    <el-main>
      <el-form :label-position="left" :model="form" inline>
        <el-form-item :label="$t('ReportTask.reportTaskName') + ':'"
                      style="margin-bottom: -7px"
                      :label-width="formLabelWidth">
          <el-input size='mini'
                    @blur="reportTaskNameCheck"
                    :placeholder="$t('common.placeholder')"
                    v-model="form.reportTaskName"
                    clearable="true"
                    style="width: 180px;">
          </el-input>
        </el-form-item>
        <el-form-item :label="$t('ReportTask.period')+':'"
                      style="margin-bottom: -7px"
                      :label-width="formLabelWidth">
          <el-select size='mini'
                     @change="periodCheck"
                     v-model="form.period"
                     :placeholder="$t('common.placeholder')"
                     style="width: 180px;">
            <el-option
              v-for="item in PeriodList"
              :key="item.key"
              :label="item.label"
              :value="item.key"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('ReportTask.reportType')+':'"
                      style="margin-bottom: -7px"
                      :label-width="formLabelWidth">
          <el-select size='mini'
                     @change="reportTypeChange"
                     v-model="form.reportType"
                     :placeholder="$t('common.placeholder')"
                     style="width: 180px;">
            <el-option
              v-for="item in reportTypeList"
              :key="item.key"
              :label="item.label"
              :value="item.key"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('ReportTask.reportTemplateName')+':'"
                      style="margin-bottom: -7px"
                      :label-width="formLabelWidth">
          <el-select size='mini'
                     v-model="form.reportTemplateKey"
                     @change="reportTemplateKeyChange"
                     :placeholder="$t('common.placeholder')"
                     style="width: 180px;">
            <el-option
              v-for="item in reportTemplateList"
              :key="item.reportTemplateKey"
              :label="item.reportTemplateName"
              :value="item.reportTemplateKey">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="margin-bottom: -7px"
                      :label-width="formLabelWidth" v-if="showBook">
          <el-radio-group style="margin-left: 80px" v-model="form.radio">
            <el-radio :label=0>{{$t("ReportTask.top")}}</el-radio>
            <el-radio :label=1 style="margin-left: -26px">{{$t("ReportTask.bottom")}}</el-radio>
          </el-radio-group>
          <el-input-number v-model="form.num" size="mini" :min='1' :step=10></el-input-number>
        </el-form-item>
        <el-form-item :label="$t('ReportTask.taskStatus')+':'"
                      style="margin-bottom: -7px"
                      :label-width="formLabelWidth">
          <el-switch
            style="width: 180px;"
            v-model="form.status"
            :active-value=0
            :inactive-value=1>
          </el-switch>
        </el-form-item>
      </el-form>
    </el-main>
    <div slot="footer" class="dialog-footer">
      <el-button size='mini' icon='el-icon-circle-check' type="primary" @click="createReportTask">
        {{$t("common.ok")}}
      </el-button>
      <el-button size='mini' icon='el-icon-circle-close' @click="close">{{$t("common.cancel")}}</el-button>
    </div>
  </el-dialog>
</template>
<script>
import legitimacyCheck from '../../../utils/legitimacyCheck'
import HelperUtil from '../../../utils/HelperUtil'
import ReportTemplateService from '../../../domain/services/ReportTemplateService'
import ReportTaskService from '../../../domain/services/ReportTaskService'

export default {
  name: 'CreateAndCopyReportTaskDialog',
  props: ['command'],

  data () {
    return {
      createAndCopyDeviceVisible: true,
      initList: this.command.target,
      showBook: false,
      check: null,

      formLabelWidth: '150px',
      reportTemplateList: [],
      reportTemplateList1: {},
      // 双向绑定弹出窗口ipput域
      form: {
        reportTaskName: '',
        period: 5,
        radio: 0,
        reportType: '',
        num: 10,
        reportTemplateKey: '',
        status: 0
      },
      reportTypeList: [
        {
          key: 0,
          label: this.$t('ReportTemplateManagement.alarmDistribution')
        },
        {
          key: 1,
          label: this.$t('ReportTemplateManagement.alarmTrend')
        },
        {
          key: 2,
          label: this.$t('ReportTemplateManagement.alarmTopBottom')
        },
        {
          key: 3,
          label: this.$t('ReportTemplateManagement.performanceDistribution')
        },
        {
          key: 4,
          label: this.$t('ReportTemplateManagement.performanceTrend')
        },
        {
          key: 5,
          label: this.$t('ReportTemplateManagement.performanceTopBottom')
        }
      ],
      PeriodList: [
        {
          key: 5,
          label: this.$t('ReportTask.dayReport')
        },
        {
          key: 4,
          label: this.$t('ReportTask.weekReport')
        },
        {
          key: 3,
          label: this.$t('ReportTask.monthReport')
        },
        {
          key: 2,
          label: this.$t('ReportTask.quarterReport')
        },
        {
          key: 1,
          label: this.$t('ReportTask.halfYearReport')
        },
        {
          key: 0,
          label: this.$t('ReportTask.yearReport')
        }
      ]
    }
  },
  methods: {
    /**
     * @Description  : 初始化
     * @author       : ls
     * @date         : 2020/6/9 15:45
     * @param        :
     * @return       :
     */
    createDeviceInit: function () {
      this.form.reportTaskName = this.initList.reportTaskName
      this.form.period = this.initList.period
      this.form.reportTemplateKey = this.initList.reportTemplateKey
      this.form.reportType = this.initList.reportType
      this.form.status = this.initList.status
      if (this.form.reportType === 2 || this.form.reportType === 5) {
        this.showBook = true
        this.form.radio = this.initList.topN
        this.form.num = this.initList.topNValue
      } else {
        this.showBook = false
      }
    },
    /**
       * @Description  : reportTypeChange
       * @author       : ls
       * @date         : 2020/6/8 12:28
       * @param        :
       * @return       :
       */
    reportTaskNameCheck: function () {
      if (!this.check.textNullCheck(this.form.reportTaskName)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('ReportTask.reportTaskName'))
        return false
      }
      return true
    },
    /**
       * @Description  : periodCheck
       * @author       : ls
       * @date         : 2020/6/8 12:30
       * @param        :
       * @return       :
       */
    periodCheck: function () {
      if (!this.check.textNullCheck(this.form.period)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('ReportTask.period'))
        return false
      }
      return true
    },
    /**
       * @Description  : reportTypeChange
       * @author       : ls
       * @date         : 2020/6/8 12:08
       * @param        :
       * @return       :
       */
    reportTypeChange: function () {
      this.reportTemplateList = this.reportTemplateList1
      this.reportTemplateList = this.reportTemplateList.filter(item => {
        return item.reportType === this.form.reportType
      })
      this.form.reportTemplateKey = ''
      if (this.form.reportType === 2 || this.form.reportType === 5) {
        this.showBook = true
      } else {
        this.showBook = false
      }
    },
    /**
       * @Description  : reportTemplateKeyCheck
       * @author       : ls
       * @date         : 2020/6/8 12:33
       * @param        :
       * @return       :
       */
    reportTemplateKeyCheck: function () {
      if (!this.check.textNullCheck(this.form.reportTemplateKey)) {
        this.InfoTip.warningTip(this, HelperUtil.getCheckStatusCodeObjectByCode(this.successCode.TEXT_NULL_CODE), this.$t('ReportTask.reportTemplateName'))
        return false
      }
      return true
    },
    /**
       * @Description  : reportTemplateKeyChange
       * @author       : ls
       * @date         : 2020/6/9 14:42
       * @param        :
       * @return       :
       */
    reportTemplateKeyChange: function () {
      this.form.reportType = this.reportTemplateList1[this.form.reportTemplateKey].reportType
      this.reportTypeChange()
    },
    /**
       * @Description  : total
       * @author       : ls
       * @date         : 2020/6/8 12:36
       * @param        :
       * @return       :
       */
    totalCheck: function () {
      if (!this.reportTaskNameCheck()) {
        return false
      }
      if (!this.periodCheck()) {
        return false
      }
      if (!this.reportTemplateKeyCheck()) {
        return false
      }
      // if (!this.reportTypeChange()) {
      //   return false
      // }
      return true
    },
    /**
       * @Description  : 创建
       * @author       : ls
       * @date         : 2020/6/8 12:27
       * @param        :
       * @return       :
       */
    createReportTask: function () {
      if (!this.totalCheck()) {
        return
      }
      let reportTask = {}
      reportTask.reportTaskName = this.form.reportTaskName
      reportTask.reportTemplateKey = this.form.reportTemplateKey
      reportTask.period = this.form.period
      reportTask.status = this.form.status
      if (this.form.reportType === 2 || this.form.reportType === 5) {
        reportTask.topN = this.form.radio
        reportTask.topNValue = this.form.num
      } else {
        reportTask.topN = -999
        reportTask.topNValue = -999
      }
      let _this = this
      let loadingInstance = _this.Loading.openLoading()
      ReportTaskService.createReportTask(reportTask).then(result => {
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.successTip(_this, HelperUtil.getStatusCodeObjectByCode(_this.successCode.ADD_CODE))
        _this.close()
      }).catch(err => {
        _this.Loading.closeLoading(loadingInstance)
        _this.InfoTip.errorTip(_this, err)
      })
    },
    close () {
      this.command.done()
    },
    /**
       * @Description  : 获取报表模板
       * @author       : ls
       * @date         : 2020/6/8 12:45
       * @param        :
       * @return       :
       */
    getAllReportTemplate: function () {
      let _this = this
      ReportTemplateService.getAllReportTemplateCollection().then(result => {
        // 成功
        _this.reportTemplateList = Object.values(result)
        _this.reportTemplateList1 = Object.values(result)
        this.reportTemplateList = this.reportTemplateList.filter(item => {
          return item.reportType === _this.form.reportType
        })
      }).catch(err => {
        _this.InfoTip.errorTip(_this, err)
      })
    }
  },
  created: function () {
    this.check = legitimacyCheck()
    this.getAllReportTemplate()
    this.createDeviceInit()
  }
}
</script>

<style scoped>

</style>
