<!--
 * @Author: zwq
 * @Date: 2020-12-29 15:41:11
 * @LastEditors: zwq
 * @LastEditTime: 2022-03-16 09:46:19
 * @Description:
-->
<template>
  <div class="app-container">
    <el-form
      ref="formData"
      :rules="rules"
      :model="listQuery"
      :inline="true"
      size="medium"
    >
      <el-form-item :label="$t('module.art.processList.processName')" prop="key">
        <el-select
          v-model="listQuery.key"
          :placeholder="$i18nForm(['placeholder.input', $t('module.art.processList.processName')])"
          clearable
          filterable
          :style="{width: '100%'}"
        >
          <el-option
            v-for="(item, index) in processArr"
            :key="index"
            :label="item.name"
            :value="item.name"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()"> {{ 'btn.search' | 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="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList()"
    />
  </div>
</template>

<script>
import { list } from '@/api/art-manage/process'
import BaseTable from '@/components/BaseTable'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import i18n from '@/lang'
import { timeFormatter } from '@/filters'
/**
 * 表格表头配置项 TypeScript接口注释
 * tableConfig<ConfigItem> = []
 *
 * Interface ConfigItem = {
 *  prop: string,
 *  label: string,
 *  width: string,
 *  align: string,
 *  subcomponent: function,
 *  filter: function
 * }
 *
 *
 */

const tableBtn = [
  {
    type: 'see',
    btnName: i18n.t('module.basicData.Warehouse.processStorageLink')
  }
]
const tableProps = [
  {
    prop: 'code',
    label: i18n.t('module.art.processList.processCode'),
    align: 'center'
  },
  {
    prop: 'name',
    label: i18n.t('module.art.processList.processName'),
    align: 'center'
  },
  {
    prop: 'createTime',
    label: i18n.t('module.art.eqName'),
    filter: timeFormatter,
    align: 'center'
  }
]

export default {
  name: 'ExecutionInfo',
  components: { Pagination, BaseTable, MethodBtn },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      processArr: [],
      total: 0,
      listLoading: true,
      rules: {},
      listQuery: {
        current: 1,
        size: 10,
        key: ''
      }
    }
  },
  created() {
    this.getList()
    this.init()
  },
  methods: {
    handleClick(raw) {
      this.addNew(raw.data.id)
    },
    init() {
      const lparams = {
        current: 1,
        size: 999
      }
      list(lparams).then(response => {
        if (response.data.records) {
          this.processArr = response.data.records
        } else {
          this.processArr.splice(0, this.list.length)
        }
        this.total = response.data.total
      })
    },
    getList() {
      this.listLoading = true
      list(this.listQuery).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.$router.push({
        name: 'ProcessStorageManagementInfo',
        query: {
          id
        }
      })
    }
  }
}
</script>

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