<!--
 * @Author: zwq
 * @Date: 2020-12-29 15:41:11
 * @LastEditors: zwq
 * @LastEditTime: 2022-03-04 09:22:58
 * @Description:
-->
<template>
  <div class="app-container">
    <el-form
      ref="formData"
      :rules="rules"
      :model="listQuery"
      :inline="true"
      size="medium"
      label-width="100px"
    >
      <el-form-item :label="$t('module.basicData.Warehouse.TimeSlot')" prop="time">
        <el-date-picker
          v-model="listQuery.timeSlot"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :start-placeholder="$t('module.orderManage.order.StartTime')"
          :end-placeholder="$t('module.orderManage.order.StartTime')"
          :range-separator="$t('module.orderManage.order.To')"
          clearable
        />
      </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()"
    />
    <Factory-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>
import FactoryAdd from './components/ExecutionInfoDetail.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'
import i18n from '@/lang'
/**
 * 表格表头配置项 TypeScript接口注释
 * tableConfig<ConfigItem> = []
 *
 * Interface ConfigItem = {
 *  prop: string,
 *  label: string,
 *  width: string,
 *  align: string,
 *  subcomponent: function,
 *  filter: function
 * }
 *
 *
 */

const tableBtn = [
  {
    type: 'see',
    btnName: 'btn.see'
  }
]
const tableProps = [
  {
    prop: 'name',
    label: i18n.t('module.quality.QCPplan.QCPcode'),
    align: 'center'
  },
  {
    prop: 'createTime',
    label: i18n.t('module.quality.QCPplan.getTime'),
    filter: timeFormatter,
    align: 'center'
  }
]

export default {
  name: 'ExecutionInfo',
  components: { Pagination, BaseTable, MethodBtn, FactoryAdd },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      addOrUpdateVisible: false,
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      total: 0,
      listLoading: true,
      rules: {},
      listQuery: {
        current: 1,
        size: 10,
        timeSlot: []
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleClick(raw) {
      this.addNew(raw.data.id)
    },
    getList() {
      // this.listLoading = true
      if (this.listQuery.timeSlot) {
        this.listQuery.startTime = this.listQuery.timeSlot[0]
        this.listQuery.endTime = this.listQuery.timeSlot[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
      // ExecutionInfoList(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.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>
