<!--/*
* @Author: zwq
* @Date: 2020-12-29 15:41:11
* @LastEditTime: 2022-04-15
* @LastEditors: juzi
* @Description: 拆分了搜索区和功能按钮区，增加了toptitle
*/
-->
<template>
  <div class="app-container">
    <head-form :form-config="headFormConfig" @headBtnClick="btnClick" />
    <base-table
      :top-btn-config="topBtnConfig"
      :page="listQuery.current"
      :limit="listQuery.size"
      :table-config="tableProps"
      :table-data="list"
      :is-loading="listLoading"
      @clickTopBtn="clickTopBtn"
    >
      <method-btn
        slot="handleBtn"
        :width="calculateWidth"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList()"
    />
    <customerData-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>
import CustomerDataAdd from './components/customerData-add.vue'
import HeadForm from '@/components/basicData/HeadForm'
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 topBtnConfig = [
  {
    type: 'add',
    btnName: 'btn.add'
  }
]

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: 'name1',
    label: '客户名称'
  },
  {
    prop: 'name2',
    label: '分类'
  },
  {
    prop: 'name3',
    label: '联系人'
  },
  {
    prop: 'name4',
    label: '电话'
  }
  // {
  //   prop: 'name5',
  //   label: 'ERP代码'
  // }
]

export default {
  name: 'CustomerData',
  components: { Pagination, BaseTable, MethodBtn, HeadForm, CustomerDataAdd },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      addOrUpdateVisible: false,
      topBtnConfig,
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 20,
        key: ''
      },
      headFormConfig: [
        {
          type: 'input',
          label: i18n.t('module.basicData.visual.keyword'),
          placeholder: '客户名称',
          param: 'name'
        },
        {
          type: 'button',
          btnName: 'btn.search',
          name: 'search',
          color: 'primary'
        }
      ],
      headFormValue: {}
    }
  },
  computed: {
    calculateWidth() {
      return this.tableBtn.length * 40 // 操作列的每个按钮宽度40
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleClick(raw) {
      console.log(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(() => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getList()
              }
            })
          })
          .catch(() => {})
      } else {
        this.addNew(raw.data)
      }
    },
    getList() {
      this.listLoading = false
      this.listQuery.key = this.headFormValue.name
      this.list = [
        {
          id: 1,
          name1: '东路钢铁公司',
          name2: '供应商',
          name3: '张总',
          name4: '13768470056',
          name5: ''
        }
      ]
    },
    btnClick(val) {
      this.headFormValue = val
      // 如果点击的是搜索栏的其他按钮在这里继续写判断
      if (this.headFormValue.btnName === 'search') {
        this.getList()
      }
    },
    clickTopBtn(val) {
      if (val === 'add') {
        this.addNew()
      }
    },
    // 新增 / 修改
    addNew(row) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row)
      })
    }
  }
}
</script>

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