<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="deviceForm" label-well-code="right" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model.trim="deviceForm.productName" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品类型" prop="productType">
            <el-input v-model.trim="deviceForm.productType" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="产品型号" prop="productModel">
            <el-input v-model.trim="deviceForm.productModel" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="批产负责人" prop="principal">
            <el-select v-model="deviceForm.principal" filterable placeholder="批产负责人" clearable value="必选">
              <el-option
                v-for="item in principalList"
                :key="item.id"
                :label="item.name"
                :value="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-row :gutter="20">
            <el-col >
              <el-form-item label="整机版本号" prop="machineVersion">
                <el-input v-model.trim="deviceForm.machineVersion" type="text" placeholder="必填"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col >
              <el-form-item label="硬件版本号" prop="hardwareVersion">
                <el-input v-model.trim="deviceForm.hardwareVersion" type="text" placeholder="必填"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col>
              <el-form-item label="软件版本号" prop="softwareVersion">
                <el-input v-model.trim="deviceForm.softwareVersion" type="text" placeholder="必填"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col>
              <el-form-item label="设计归档" prop="designArchive">
                <el-input v-model.trim="deviceForm.designArchive" type="text" placeholder="必填"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <row :gutter="20">
            <el-form-item label="设备版本" prop="communicationVersion">
              <el-select v-model="deviceForm.communicationVersion" placeholder="必选" clearable value="必选">
                <el-option
                  v-for="item in options1"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </row>
          <row :gutter="20">
            <el-form-item label="状态" prop="status">
              <el-select v-model="deviceForm.status" placeholder="必选" clearable value="必选">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </row>
          <row :gutter="20">
            <el-form-item label="性能指标" prop="kpi">
              <el-input v-model.trim="deviceForm.kpi" type="textarea" placeholder="必填" rows = "5"/>
            </el-form-item>
        </row></el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="批产归档" prop="batchFiling">
            <el-input v-model.trim="deviceForm.batchFiling" type="text" placeholder="必填"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="定额汇总表" >
            <el-upload
              :limit="1"
              :show-file-list="false"
              :http-request="uploadFile"
              :file-list="fileList"
              action="string"
              accept=".xls,.xlsx"
              class="edit_btn">
              <el-button slot="trigger" size="small">选择文件</el-button>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">提交</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDeviceType, updateDeviceType, getPrincipalList, getDeviceTypeKpi, getDeptList } from '@/api/devicetype'

export default {
  name: 'EditDeviceType',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      quotaSummaryFile: null,
      statusFmt: '',
      deviceForm: {
        productName: '',
        productType: '',
        productModel: '',
        machineVersion: '',
        hardwareVersion: '',
        softwareVersion: '',
        designArchive: '',
        batchFiling: '',
        status: '',
        principal: '',
        kpi: '',
        quotaSummary: '',
        communicationVersion: '',
        id: ''
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      fileList: [],
      principalList: null, // 批产负责人列表
      rules: {
        productName: [{ required: true, message: '产品名称不能为空', trigger: ['blur', 'change'] }],
        productModel: [{ required: true, message: '产品型号不能为空', trigger: ['blur', 'change'] }],
        productType: [{ required: true, message: '产品类型不能为空', trigger: ['blur', 'change'] }],
        machineVersion: [{ required: true, message: '整机版本号不能为空', trigger: ['blur', 'change'] }],
        hardwareVersion: [{ required: true, message: '硬件版本号不能为空', trigger: ['blur', 'change'] }],
        softwareVersion: [{ required: true, message: '软件版本号不能为空', trigger: ['blur', 'change'] }],
        designArchive: [{ required: true, message: '设计归档不能为空', trigger: ['blur', 'change'] }],
        batchFiling: [{ required: true, message: '批产归档不能为空', trigger: ['blur', 'change'] }],
        kpi: [{ required: true, message: '性能指标不能为空', trigger: ['blur', 'change'] }],
        principal: [{ required: true, message: '请选择负责人', trigger: 'change' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }],
        communicationVersion: [{ required: true, message: '请选择设备版本', trigger: 'change' }]
      }, // 前端校验规则
      isEditMode: false,
      options: [{
        value: 1,
        label: '在产'
      }, {
        value: 0,
        label: '停产'
      }],
      options1: [{
        value: 'N',
        label: 'N'
      }, {
        value: 'A',
        label: 'A'
      }, {
        value: 'B',
        label: 'B'
      }, {
        value: '其他',
        label: '其他'
      }]
    }
  },
  created() {
    this.fetchPrincipalList()
    this.fetchDeptList()
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.isEditMode = false
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.deviceForm = {
          productName: row.productName,
          productType: row.productType,
          productModel: row.productModel,
          machineVersion: row.machineVersion,
          hardwareVersion: row.hardwareVersion,
          softwareVersion: row.softwareVersion,
          designArchive: row.designArchive,
          batchFiling: row.batchFiling,
          status: row.status,
          principal: row.principal,
          id: row.id,
          kpi: '',
          quotaSummary: row.quotaSummary,
          communicationVersion: row.communicationVersion
        }
        this.quotaSummaryFile = null
        this.statusFmt = row.statusFmt
        this.fetchDeviceTypeKpi(row.id)
        this.isEditMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.quotaSummaryFile = null
      this.deviceForm = {
        productName: '',
        productType: '',
        productModel: '',
        machineVersion: '',
        hardwareVersion: '',
        softwareVersion: '',
        designArchive: '',
        batchFiling: '',
        status: '',
        principal: '',
        kpi: '',
        quotaSummary: '',
        communicationVersion: ''
      }
      this.quotaSummaryFile = null
      this.statusFmt = ''
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addDeviceType(this.deviceForm.productName, this.deviceForm.productModel, this.deviceForm.machineVersion,
            this.deviceForm.hardwareVersion, this.deviceForm.softwareVersion, this.deviceForm.designArchive,
            this.deviceForm.batchFiling, this.deviceForm.status, this.deviceForm.principal,
            this.deviceForm.kpi, this.deviceForm.quotaSummary, this.quotaSummaryFile, this.deviceForm.communicationVersion,
            this.deviceForm.productType).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDeviceType(this.deviceForm.id, this.deviceForm.productName, this.deviceForm.productModel, this.deviceForm.machineVersion,
            this.deviceForm.hardwareVersion, this.deviceForm.softwareVersion, this.deviceForm.designArchive,
            this.deviceForm.batchFiling, this.deviceForm.status, this.deviceForm.principal,
            this.deviceForm.kpi, this.deviceForm.quotaSummary, this.quotaSummaryFile, this.deviceForm.communicationVersion,
            this.deviceForm.productType).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 定额汇总表上传
    uploadFile(param) {
      // 判断文件大小是否符合要求
      const _file = param.file
      const isLt5M = _file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('请上传5M以下的excel文件')
        return false
      }
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '导入中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.quotaSummaryFile = _file
      this.deviceForm.quotaSummary = this.quotaSummaryFile.name
      this.fileList = []
      loading.close() // 关闭加载动画
    },
    // 获取批产负责人
    fetchPrincipalList() {
      getPrincipalList('operator,producer').then(response => {
        this.principalList = response.data
      })
    },
    // 获取组织机构
    fetchDeptList() {
      getDeptList().then(response => {
        this.deptidList = response.data
      })
    },
    fetchDeviceTypeKpi(id) {
      getDeviceTypeKpi(id).then(response => {
        this.deviceForm.kpi = response.data.kpi
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
</style>
