<!--
 * @Descripttion:
 * @version:
 * @Author: fzq
 * @Date: 2022-03-05 15:55:45
 * @LastEditors: fzq
 * @LastEditTime: 2022-03-05 19:44:47
-->
<template>
  <div class="app-container">
    <!-- <div style="margin:10px 50px">
      <el-button type="success" @click="goback()">{{ 'btn.back' | i18nFilter }}</el-button>
      <el-button type="primary" @click="addNew()">{{ 'btn.add' | i18nFilter }}</el-button>
    </div> -->
    <div style="title">存储箱号:{{ num }}
      <el-button type="success" style="back" @click="goback()">{{ 'btn.back' | i18nFilter }}</el-button>
    </div>
    <base-table
      :page="listQuery.current"
      :limit="listQuery.size"
      :table-config="tableProps"
      :table-data="list"
      :is-loading="listLoading"
    >
      <method-btn
        slot="handleBtn"
        :width="trueWidth"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>
    <edit-substrate v-if="addOrUpdateVisible" :id="listQuery.id" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>import i18n from '@/lang'
import { listSubstrate, deleSubstrate } from '@/api/report-manage/report'
import editSubstrate from './edit-substrate.vue'
import BaseTable from '@/components/BaseTable'
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
/**
 * 表格表头配置项 TypeScript接口注释
 * tableConfig<ConfigItem> = []
 *
 * Interface ConfigItem = {
 *  prop: string,
 *  label: string,
 *  width: string,
 *  align: string,
 *  subcomponent: function,
 *  filter: function
 * }
 *
 *
 */

const tableBtn = [
  {
    type: 'edit',
    btnName: 'btn.edit'
  },
  {
    type: 'delete',
    btnName: 'btn.delete'
  }
]
const tableProps = [
  {
    prop: 'substrateCode',
    label: i18n.t('module.report.substrate.substrateCode'),
    align: 'center'
  },
  {
    prop: 'location',
    label: i18n.t('module.report.substrate.location'),
    align: 'center'
  },
  {
    prop: 'storeTime',
    label: i18n.t('module.report.substrate.storeTime'),
    align: 'center'
  },
  {
    prop: 'substrateStatus',
    label: i18n.t('module.report.substrate.substrateStatus'),
    align: 'center'
  }
]

export default {
  name: 'ViewSubstrate',
  components: { BaseTable, MethodBtn, editSubstrate },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      addOrUpdateVisible: false,
      num: '',
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      listLoading: true,
      listQuery: {
        current: 1,
        size: 10,
        substrateCode: ''
      }
    }
  },
  created() {
    this.listQuery.substrateCode = this.$route.query.code
    this.getList()
  },
  methods: {
    handleClick(raw) {
      if (raw.type === 'delete') {
        this.$confirm(`${this.$t('module.basicData.visual.TipsBefore')}[${raw.data.substrateCode}]?`, this.$t('module.basicData.visual.Tips'), {
          confirmButtonText: this.$t('module.basicData.visual.confirmButtonText'),
          cancelButtonText: this.$t('module.basicData.visual.cancelButtonText'),
          type: 'warning'
        }).then(() => {
        //   console.log(raw.data)
          deleSubstrate(raw.data).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)
      }
    },
    getList() {
      this.listLoading = true
      this.num = this.listQuery.substrateCode
      this.listQuery.code = this.$route.query.code
      console.log(this.listQuery)
      listSubstrate(this.listQuery).then(response => {
        if (response.data) {
          this.list = response.data
          console.log(this.list)
        } else {
          this.list.splice(0, this.list.length)
        }
        this.listLoading = false
      })
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    goback() {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.title {
  left: 150px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
.back {
  right: 20px;
}
</style>
