<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2022-03-03 16:43:26
 * @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="190px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.art.processList.processName')" prop="name">
        <el-input v-model="dataForm.name" :placeholder="$i18nForm(['placeholder.input', $t('module.art.processList.processName')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.art.processList.processEq')" prop="equipmentIds">
        <el-select v-model="dataForm.equipmentIds" clearable filterable multiple>
          <el-option v-for="item in eqList" :key="item.id" :value="item.id" :label="item.enName" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item :label="$t('module.art.processList.type')" prop="type">
        <el-select v-model="dataForm.type" clearable filterable>
          <el-option v-for="item in typeList" :key="item.id" :value="item.id" :label="item.name" />
        </el-select>
      </el-form-item> -->
      <el-form-item :label="$t('module.art.processList.description')" prop="address">
        <el-input v-model="dataForm.description" :placeholder="$i18nForm(['placeholder.input', $t('module.art.processList.description')])" 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 { getInfo, add, update } from '@/api/art-manage/process'
import i18n from '@/lang'

const typeList = [
  { id: 0, name: i18n.t('module.art.processList.equipment') },
  { id: 1, name: i18n.t('module.art.processList.buffer') }
]

export default {
  props: {
    eqList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        type: null,
        equipmentIds: [],
        description: ''
      },
      dataRule: {
        name: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.art.processList.processName')]),
            trigger: 'blur' }
        ],
        equipmentIds: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.select', this.$t('module.art.processList.processEq')]),
            trigger: 'blur' }
        ]
      },
      typeList
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || null
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getInfo({ id: this.dataForm.id }).then(res => {
            this.dataForm.id = res.data.id
            this.dataForm.name = res.data.name
            this.dataForm.type = res.data.type
            this.dataForm.description = res.data.description
            this.dataForm.equipmentIds = res.data.equipments.map(item => {
              return item.id
            })
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      console.log(!this.dataForm.id)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'name': this.dataForm.name,
            'type': this.dataForm.type,
            'equipmentIds': this.dataForm.equipmentIds,
            'description': this.dataForm.description,
            'id': this.dataForm.id
          }
          if (this.dataForm.id) {
            update(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            add(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
