<!--
 * @Author: zwq
 * @Date: 2020-12-29 15:41:11
 * @LastEditors: zwq
 * @LastEditTime: 2021-03-10 11:13:58
 * @Description:
-->
<template>
  <div class="app-container">
    <el-form
      :model="formData"
      :inline="true"
      size="medium"
      label-width="80px"
    >
      <el-form-item v-if="false" :label="$t('module.basicData.ScrapInfo.PlateId')" prop="basalId">
        <el-input v-model="formData.basalId" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.PlateId')])" style="width:200px" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.ScrapInfo.TimePeriod')" prop="time">
        <el-date-picker
          v-model="formData.timeSlot"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :start-placeholder="$t('module.basicData.ScrapInfo.StartTime')"
          :end-placeholder="$t('module.basicData.ScrapInfo.EndTime')"
          :range-separator="$t('module.basicData.ScrapInfo.To')"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()"> {{ 'btn.search' | i18nFilter }} </el-button>
        <el-button type="primary" @click="addNew()"> {{ 'btn.add' | i18nFilter }} </el-button>
      </el-form-item>
    </el-form>
    <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>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="formData.current"
      :limit.sync="formData.size"
      @pagination="getList()"
    />
    <ScrapInfo-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>import i18n from '@/lang'
import { ScrapInfoList, ScrapInfoDelete } from '@/api/basicData/ScrapInfo'
import ScrapInfoAdd from './components/ScrapInfo-add.vue'
import ScrapInfoCause from './components/ScrapInfoCause.vue'
import BaseTable from '@/components/BaseTable'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import { timeFormatter } from '@/filters'
/**
 * 表格表头配置项 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: 'createTime',
    label: i18n.t('module.basicData.factory.createTime'),
    filter: timeFormatter
  },
  // {
  //   prop: 'basalId',
  //   label: i18n.t('module.basicData.ScrapInfo.PlateId'),
  //   align: 'center'
  // },
  {
    prop: 'source',
    label: i18n.t('module.basicData.ScrapInfo.source'),
    align: 'center'
  },
  {
    prop: 'wasteGrade',
    label: i18n.t('module.basicData.ScrapInfo.wasteGrade'),
    align: 'center'
  },
  {
    prop: 'creatorName',
    label: i18n.t('module.basicData.ScrapInfo.RegisterPerson')
  },
  {
    prop: 'updateTime',
    label: i18n.t('module.basicData.ScrapInfo.updateTime'),
    filter: timeFormatter
  },
  {
    prop: 'cause',
    label: i18n.t('module.basicData.ScrapInfo.cause'),
    subcomponent: ScrapInfoCause
  }
]

export default {
  name: 'ScrapInfo',
  components: { Pagination, BaseTable, MethodBtn, ScrapInfoAdd },
  data() {
    return {
      addOrUpdateVisible: false,
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      total: 0,
      listLoading: true,
      formData: {
        timeSlot: [],
        basalId: '',
        current: 1,
        size: 10
      },
      listQuery: {
        current: 1,
        size: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleClick(raw) {
      console.log(raw)
      if (raw.type === 'delete') {
        this.$confirm(`${this.$t('module.basicData.visual.TipsBefore')}[${raw.data.status}]?`, this.$t('module.basicData.visual.Tips'), {
          confirmButtonText: this.$t('module.basicData.visual.confirmButtonText'),
          cancelButtonText: this.$t('module.basicData.visual.cancelButtonText'),
          type: 'warning'
        }).then(() => {
          ScrapInfoDelete(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)
      }
    },
    getList() {
      this.listLoading = true
      if (this.formData.timeSlot) {
        this.formData.startTime = this.formData.timeSlot[0]
        this.formData.endTime = this.formData.timeSlot[1]
      } else {
        this.formData.startTime = ''
        this.formData.endTime = ''
      }
      ScrapInfoList(this.formData).then(response => {
        if (response.data.records) {
          this.list = response.data.records
        } else {
          this.list.splice(0, this.list.length)
        }
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    }
  }
}
</script>

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