<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-07-26 14:42:17
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="110px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.orderManage.powerClassification.typeName')" prop="name">
        <el-input v-model="dataForm.name" :placeholder="$i18nForm(['placeholder.input', $t('module.orderManage.powerClassification.typeName')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.orderManage.powerClassification.typeCode')" prop="code">
        <el-input v-model="dataForm.code" :placeholder="$i18nForm(['placeholder.input', $t('module.orderManage.powerClassification.typeCode')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.orderManage.powerClassification.parentClass')" prop="parentId">
        <el-cascader
          ref="cascader"
          v-model="dataForm.parentId"
          :placeholder="$i18nForm(['placeholder.input', $t('module.orderManage.powerClassification.parentClass')])"
          :props="{ checkStrictly: true,value: 'id',label: 'name' }"
          :options="parentArr"
          filterable
          clearable
          @change="setParent"
        />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.visual.Remarks')" prop="remark">
        <el-input v-model="dataForm.remark" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.Remarks')])" clearable />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { powerClassificationTree, powerClassificationDetail, powerClassificationUpdate, powerClassificationAdd, powerClassificationCode } from '@/api/orderManage/powerClassification'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        code: '',
        parentId: '',
        parentName: '',
        remark: ''
      },
      parentArr: [],
      dataRule: {
        name: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.orderManage.powerClassification.typeName')]),
            trigger: 'blur' }
        ],
        parentId: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.orderManage.powerClassification.parentClass')]),
            trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.parentArr.splice(0, this.parentArr.length)
      powerClassificationTree().then(res => {
        this.parentArr = res.data
      })
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          powerClassificationDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
          })
        } else {
          powerClassificationCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = this.dataForm
          data.parentId = data.parentId[data.parentId.length - 1]
          if (this.dataForm.id) {
            powerClassificationUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            powerClassificationAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    },
    setParent(val) {
      this.dataForm.parentName = this.$refs['cascader'].getCheckedNodes()[0].pathLabels[this.$refs['cascader'].getCheckedNodes()[0].pathLabels.length - 1]
    }
  }
}
</script>
