<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="18">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-col :span="5">
            <el-form-item>
              <el-input v-model.trim="queryForm.prodCode" clearable placeholder="产品编码" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <el-input v-model.trim="queryForm.prodClass" clearable placeholder="产品类别" />
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item>
              <el-input v-model.trim="queryForm.prodName" clearable placeholder="产品名称" />
            </el-form-item>
          </el-col>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
            <el-button icon="el-icon-refresh-right" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>

      <vab-query-form-left-panel :span="15">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit($event)">添加</el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="29">
        <el-button icon="el-icon-download" />
        <table-tool :check-list.sync="checkList" :columns="columns" />
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table v-loading="listLoading" border :data="list" :height="height" @selection-change="setSelectRows">
      <el-table-column
        v-for="(item, index) in finallyColumns"
        :key="index"
        align="center"
        :label="item.label"
        :prop="item.prop"
        show-overflow-tooltip
        :sortable="item.sortable"
        :width="item.width">
        <template #default="{ row }">
          <!-- <span v-if="item.prop === 'prodName'">
        {{ row.prodName }}
      </span> -->
          <el-button v-if="item.prop === 'prodName'" style="font-size: 14px" type="text" @click="handleDetail(row)">
            {{ row.prodName }}
          </el-button>
          <span v-if="item.prop === 'prodClass'">
            {{ row.prodClass }}
          </span>
          <span v-if="item.prop === 'prodCode'">
            {{ row.prodCode }}
          </span>
          <span v-if="item.prop === 'agentPrice'">{{ parseFloat(row.agentPrice.toFixed(2)).toLocaleString() }}¥</span>
          <span v-if="item.prop === 'distPrice'">{{ parseFloat(row.distPrice.toFixed(2)).toLocaleString() }}¥</span>
          <span v-if="item.prop === 'guidPrice'">{{ parseFloat(row.guidPrice.toFixed(2)).toLocaleString() }}¥</span>
          <span v-if="item.prop === 'marketPrice'">{{ parseFloat(row.marketPrice.toFixed(2)).toLocaleString() }}¥</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" show-overflow-tooltip width="95">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <!-- <el-button type="text" @click="showDefail(row)">详情</el-button> -->
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-image class="vab-data-empty" :src="require('@/assets/empty_images/data_empty.png')" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange" />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import Edit from './components/ProductEdit'
  import productApi from '@/api/product'
  import TableTool from '@/components/table/TableTool'

  export default {
    components: { Edit, TableTool },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          userName: '',
        },
        checkList: [],
        columns: [
          {
            label: '产品名称',
            width: '120px',
            prop: 'prodName',
            sortable: false,
          },
          {
            label: '产品类别',
            width: 'auto',
            prop: 'prodClass',
            sortable: false,
            disableCheck: true,
          },
          {
            label: '产品型号',
            width: 'auto',
            prop: 'prodCode',
            sortable: false,
          },
          {
            label: '签约代理价',
            width: 'auto',
            prop: 'agentPrice',
            sortable: false,
          },
          {
            label: '经销商价',
            width: 'auto',
            prop: 'distPrice',
            sortable: true,
          },
          {
            label: '建议成交价',
            width: 'auto',
            prop: 'guidPrice',
            sortable: true,
          },
          {
            label: '市场报价',
            width: 'auto',
            prop: 'marketPrice',
            sortable: true,
          },
        ],
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight(1)
      },
      finallyColumns() {
        console.log('finallyColums')
        return this.columns.filter((item) => this.checkList.includes(item.label))
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      reset() {
        this.queryForm = {
          pageNum: 1,
          pageSize: 10,
          prodCode: '',
          prodClass: '',
          prodName: '',
        }
      },
      //详情
      handleDetail(row) {
        this.$router.push({
          path: '/product/detail',
          query: {
            id: row.id,
          },
        })
      },

      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },

      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await productApi.doDelete({ ids: [row.id] })
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => parseInt(item.id))
            console.log(ids)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await productApi.doDelete({ ids })
              this.$baseMessage(msg, 'success', 'vab-hey-message-success')
              await this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const {
          data: { list, total },
        } = await productApi.getList(this.queryForm)
        this.list = list
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
<style>
  .el-form-item--small {
    margin: 0 0 10px 0 !important;
  }
</style>
