<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2022-03-04 10:39:52
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @close="onClose"
  >
    <el-row :gutter="15" style="padding: 0 20px;">
      <el-form
        ref="dataForm"
        :model="dataForm"
        :rules="rules"
        size="medium"
        label-width="150px"
        label-position="right"
      >

        <el-form-item :label="$t('module.basicData.ScrapInfo.updateTime')" prop="registerTime">
          <el-date-picker
            v-model="dataForm.registerTime"
            format="yyyy-MM-dd"
            :style="{width: '100%'}"
            :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.updateTime')])"
            clearable
          />
        </el-form-item>

        <el-form-item :label="$t('module.basicData.ScrapInfo.RegisterPerson')" prop="registerPerson">
          <!-- <el-input v-model="dataForm.registerPerson" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.RegisterPerson')])" clearable :style="{width: '100%'}" /> -->
          <el-select
            v-model="dataForm.registerPerson"
            :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.RegisterPerson')])"
            clearable
            :style="{width: '100%'}"
          >
            <el-option
              v-for="(item, index) in dict.worker"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('module.basicData.ScrapInfo.entryType')" prop="entryType">
          <!-- <el-input v-model="dataForm.registerPerson" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.RegisterPerson')])" clearable :style="{width: '100%'}" /> -->
          <el-select
            v-model="dataForm.entryType"
            :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.entryType')])"
            clearable
            :style="{width: '100%'}"
          >
            <el-option
              v-for="(item, index) in entryType"
              :key="index"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('module.basicData.ScrapInfo.workOrderId')" prop="workOrderId">
          <el-select
            v-model="dataForm.workOrderId"
            :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.workOrderId')])"
            clearable
            :style="{width: '100%'}"
          >
            <el-option
              v-for="(item, index) in dict.orderList"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('module.basicData.ScrapInfo.source')" prop="source">
          <el-input v-model="dataForm.source" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.source')])" clearable :style="{width: '100%'}" />
        </el-form-item>

        <el-form-item :label="$t('module.basicData.ScrapInfo.PlateId')" prop="substrateId">
          <el-input v-model="dataForm.substrateId" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.PlateId')])" clearable :style="{width: '100%'}" />
        </el-form-item>

        <el-form-item :label="$t('module.basicData.ScrapInfo.name')" prop="equipmentId">
          <el-select v-model="dataForm.equipmentId" filterable :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.name')])" clearable :style="{width: '100%'}" @change="getScrapGrade">
            <el-option
              v-for="(item, index) in device"
              :key="index"
              :label="item.name"
              :value="item.id "
            />
          </el-select>
        </el-form-item>

        <el-form-item v-if="tag" :label="$t('module.basicData.ScrapInfo.wasteGrade')" prop="ewasteGrade">
          <el-input v-model="wasteGrade" clear readonly :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.wasteGrade')])" :disabled="true" />
        </el-form-item>

        <el-form-item :label="$t('module.basicData.ScrapInfo.cause')" prop="scrapReasonId">
          <!-- <el-input v-model="dataForm.registerPerson" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.RegisterPerson')])" clearable :style="{width: '100%'}" /> -->
          <el-select
            v-model="dataForm.scrapReasonId"
            :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.cause')])"
            clearable
            :style="{width: '100%'}"
          >
            <el-option
              v-for="(item, index) in dict.scrapReason"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item :label="$t('module.basicData.ScrapInfo.registrationPlace')" prop="placeOfRegis">
          <el-input v-model="dataForm.placeOfRegis" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.registrationPlace')])" clearable :style="{width: '100%'}" />
        </el-form-item>
        <el-form-item :label="$t('module.basicData.ScrapInfo.remark')" prop="remark">
          <el-input v-model="dataForm.remark" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.remark')])" clearable :style="{width: '100%'}" />
        </el-form-item>

      </el-form>
    </el-row>
    <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 i18n from '@/lang'
import { getScrapInfo, editScrapInfo, addScrapInfo, getScrap } from '@/api/quality-manage/scrap'
import { scrapReasonList } from '@/api/dict'
import { getDictWorker } from '@/api/dict'
import { getEqList } from '@/api/equipment/maintain'
import { workOrderListList } from '@/api/orderManage/workOrder/workOrder'

const wasteGradeArr = [{
  value: '加工可用',
  label: i18n.t('module.basicData.ScrapInfo.completeWaste')
},
{
  value: '完全废品',
  label: i18n.t('module.basicData.ScrapInfo.CanBeUsedAfterProcessing')
}]

const entryType = [{
  value: '2',
  label: i18n.t('module.basicData.ScrapInfo.manual')
},
{
  value: '1',
  label: i18n.t('module.basicData.ScrapInfo.automatic')
}]

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        source: undefined,
        registerTime: undefined,
        registerPerson: undefined,
        scrapGrade: undefined,
        description: undefined,
        substrateId: undefined,
        placeOfRegis: '00A',
        remark: undefined
      },
      wasteGradeArr,
      entryType,
      rules: {
        name: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.ScrapInfo.WasteName')]),
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.ScrapInfo.WasteCode')]),
          trigger: 'blur'
        }],
        equipmentId: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.ScrapInfo.equipmentId')]),
          trigger: 'change'
        }],
        substrateId: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.ScrapInfo.PlateId')]),
          trigger: 'blur'
        }],
        registerPerson: [{
          required: true,
          message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.ScrapInfo.registerPerson')]),
          trigger: 'change'
        }]
      },
      dict: {
        worker: [],
        scrapReason: [],
        orderList: []
      },
      device: [],
      wasteGrade: '',
      tag: false
    }
  },
  mounted() {
    this.getDict()
  },
  methods: {
    getScrapGrade(val) {
      getScrap(val).then(res => {
        if (res.code === 0) {
          if (res.data) {
            this.tag = true
            this.wasteGrade = res.data.dataName
          } else {
            this.tag = false
            this.wasteGrade = ''
          }
        }
      })
    },
    onClose() {
      this.$refs['dataForm'].resetFields()
    },
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        if (this.dataForm.id) {
          getScrapInfo({ id: this.dataForm.id }).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.dataForm.id) {
            console.log(this.device)
            editScrapInfo(this.dataForm).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            addScrapInfo(this.dataForm).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    },
    async getDict() {
      const result3 = await workOrderListList({
        current: 1,
        size: 999
      })
      console.log(result3)
      this.dict.orderList = result3.data.records
      const result1 = await getEqList({
        current: 1,
        size: 999
      })
      if (result1.code === 0) {
        this.device = result1.data.records
      }
      const result = await getDictWorker()
      this.dict.worker = result
      const result2 = await scrapReasonList()
      this.dict.scrapReason = result2
      // this.dict.orderList = result3.data.records
    }
  }
}
</script>
