<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-03-31 11:24:59
 * @enName:
-->
<template>
  <div style="margin:20px">
    <div slot="title" style=" background-color:#02BCFF;font-size:1.5em;color:white;padding:5px 20px;margin:20px">{{ isdetail? 'btn.detail' : !dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter }}</div>
    <div style="margin:0 15px">
      <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px" @keyup.enter.native="dataFormSubmit()">
        <el-form-item :label="$t('module.orderManage.substrateBatch.batchName')" prop="name">
          <el-input v-model="dataForm.name" :disabled="isdetail" :placeholder="$i18nForm(['placeholder.input', $t('module.orderManage.substrateBatch.batchName')])" clearable />
        </el-form-item>
        <el-form-item :label="$t('module.orderManage.substrateBatch.batchCode')" prop="code">
          <el-input v-model="dataForm.code" :disabled="isdetail" :placeholder="$i18nForm(['placeholder.input', $t('module.orderManage.substrateBatch.batchCode')])" clearable />
        </el-form-item>
        <el-form-item :label="$t('module.basicData.visual.Remarks')" prop="remark">
          <el-input v-model="dataForm.remark" :disabled="isdetail" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.Remarks')])" clearable />
        </el-form-item>
      </el-form>
      <div style="margin:20px">
        <el-button type="success" @click="goback()">{{ 'btn.back' | i18nFilter }}</el-button>
        <el-button v-if="isdetail" type="primary" @click="goEdit()">{{ 'btn.edit' | i18nFilter }}</el-button>
        <span v-if="!isdetail">
          <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.save' | i18nFilter }}</el-button>
          <el-button v-if="listQuery.subId" type="primary" @click="addNew()">{{ 'btn.addattr' | i18nFilter }}</el-button>
        </span>
      </div>
      <div style="height:380px;overflow:auto">
        <base-table
          :page="listQuery.current"
          :limit="listQuery.size"
          :table-config="tableProps"
          :table-data="list"
        >
          <method-btn
            v-if="!isdetail"
            slot="handleBtn"
            :method-list="tableBtn"
            @clickBtn="handleClick"
          />
        </base-table>
      </div>
    </div>
    <substrate-batch-attr-add v-if="addOrUpdateVisible" ref="addOrUpdate" :sub-id="listQuery.subId" @refreshDataList="getList" />
  </div>
</template>

<script>import i18n from '@/lang'
import { substrateBatchDetail, substrateBatchUpdate, substrateBatchAdd, substrateBatchCode } from '@/api/orderManage/substrateBatch'
import { substrateBatchAttrList, substrateBatchAttrDelete } from '@/api/orderManage/substrateBatchAttr'
import BaseTable from '@/components/BaseTable'
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import substrateBatchAttrAdd from './substrateBatchAttr-add'
import { timeFormatter } from '@/filters'
const tableBtn = [
  {
    type: 'edit',
    btnName: 'btn.edit'
  },
  {
    type: 'delete',
    btnName: 'btn.delete'
  }
]
const tableProps = [
  {
    prop: 'createTime',
    label: i18n.t('module.basicData.factory.createTime'),
    filter: timeFormatter,
    align: 'center'
  },
  {
    prop: 'name',
    label: i18n.t('module.basicData.visual.AttributeName'),
    align: 'center'
  },
  {
    prop: 'value',
    label: i18n.t('module.basicData.visual.AttributeValue'),
    align: 'center'
  }
]

export default {
  components: { BaseTable, MethodBtn, substrateBatchAttrAdd },
  data() {
    return {
      addOrUpdateVisible: false,
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      dataForm: {
        name: '',
        code: '',
        enName: '',
        abbr: '',
        category: '',
        description: '',
        remark: ''
      },
      dataRule: {
        name: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.material.MaterialName')]), trigger: 'blur' }
        ],
        code: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.material.MaterialCoding')]), trigger: 'blur' }
        ]
      },
      listQuery: {
        current: 1,
        size: 990,
        subId: ''
      },
      isdetail: false
    }
  },
  created() {
    this.listQuery.subId = this.$route.query.id
    this.init()
  },
  methods: {
    init() {
      this.isdetail = false
      this.isdetail = Boolean(this.$route.query.isdetail)
      this.list.splice(0, this.list.length)
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.listQuery.subId) {
          substrateBatchDetail(this.listQuery.subId).then(res => {
            this.dataForm = res.data
          })
          substrateBatchAttrList(this.listQuery).then(response => {
            if (response.data.records) {
              this.list = response.data.records
            } else {
              this.list.splice(0, this.list.length)
            }
          })
        } else {
          substrateBatchCode().then(res => {
            this.dataForm.code = res.data
          })
        }
      })
    },
    getList() {
      substrateBatchAttrList(this.listQuery).then(response => {
        if (response.data.records) {
          this.list = response.data.records
        } else {
          this.list.splice(0, this.list.length)
        }
      })
    },
    handleClick(raw) {
      if (raw.type === 'delete') {
        this.$confirm(`${this.$t('module.basicData.visual.TipsBefore')}[${raw.data.name}]?`, this.$t('module.basicData.visual.Tips'), {
          confirmButtonText: this.$t('module.basicData.visual.confirmButtonText'),
          cancelButtonText: this.$t('module.basicData.visual.cancelButtonText'),
          type: 'warning'
        }).then(() => {
          substrateBatchAttrDelete(raw.data.id).then(response => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getList()
              }
            })
          })
        }).catch(() => {})
      } else {
        this.addNew(raw.data.id)
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.id = this.listQuery.subId
          const data = this.dataForm
          if (this.listQuery.subId) {
            substrateBatchUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500
              })
            })
          } else {
            substrateBatchAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500
              })
              this.listQuery.subId = res.data.id
            })
          }
        }
      })
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    goEdit() {
      this.isdetail = false
    },
    goback() {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.drawer-footer {
  width: 100%;
  margin-top: 50px;
  border-top: 1px solid #e8e8e8;
  border-bottom: 1px solid #e8e8e8;
  padding: 10px 50px;
  text-align: left;
  background: #fff;
}
</style>
