<template>
  <div class="list-container">
    <el-row :gutter="10" style="margin-bottom: 10px">
      <el-col :span="4">
        <el-input v-model="queryForm.mainProduct" placeholder="主营产品" />
      </el-col>
      <el-col :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="restFetchData">查询</el-button>
        <el-button icon="el-icon-refresh-right" @click="reset">重置</el-button>
      </el-col>
    </el-row>
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button
          v-permissions="['work:order:add']"
          icon="el-icon-plus"
          size="mini"
          type="primary"
          @click="createOrder(0, 'add')">
          新建
        </el-button>
      </vab-query-form-left-panel>
      <!-- <vab-query-form-right-panel :span="12">
        <table-tool :columns="columns" table-type="deliverWorkOrderTable" />
      </vab-query-form-right-panel> -->
    </vab-query-form>
    <el-table ref="table" v-loading="listLoading" border :data="list" :height="$baseTableHeight(2)">
      <el-table-column align="center" label="序号" show-overflow-tooltip width="80">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        v-for="(item, index) in columns"
        :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 === 'trainingDate'">
            {{ parseTime(row.trainingDate, '{y}-{m}-{d}') }}
          </span>
          <span v-else-if="item.prop === 'specificTime'">
            {{ parseTime(row.specificStartTime, '{y}-{m}-{d} {h}:{m}:{s}') }} -
            {{ parseTime(row.specificEndTime, '{y}-{m}-{d} {h}:{m}:{s}') }}
          </span>
          <span v-else>{{ row[item.prop] }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleDetail(row)">查看</el-button>
          <el-button type="text" @click="createOrder(row, 'edit')">编辑</el-button>
          <el-button v-if="row.assess == 'yes'" type="text" @click="handleFinish(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" />
    <!--反馈-->
    <finish ref="finish" @fetch-data="restFetchData" />
    <!-- 创建工单 -->
    <order-edit ref="order-edit" @fetch-data="restFetchData" />
    <!-- 详情 -->
    <Details ref="details" />
  </div>
</template>

<script>
  import to from 'await-to-js'
  import Api from '@/api/work/trainSale'
  import Finish from './components/finish'
  import { mapGetters } from 'vuex'
  import orderEdit from '@/views/work/train/sale/components/Edit'
  import Details from '@/views/work/train/sale/components/details'

  export default {
    name: 'WorkOrder',
    components: { Finish, Details, orderEdit },
    data() {
      return {
        activeName: 'first',
        layout: 'total, sizes, prev, pager, next, jumper',
        queryForm: {
          distributorId: 0,
          mainProduct: '',
          pageNum: 1,
          pageSize: 10,
        },
        total: 0,
        listLoading: false,
        list: [],
        selectRows: [],
        columns: [
          {
            label: '渠道名称',
            width: '160px',
            prop: 'distributorName',
          },
          {
            label: '主营产品',
            width: '280px',
            prop: 'mainProduct',
          },
          {
            label: '主要客户',
            width: '280px',
            prop: 'mainCustomer',
          },
          {
            label: '参训人员',
            width: '100px',
            prop: 'trainees',
          },
          {
            label: '培训日期',
            width: '100px',
            prop: 'trainingDate',
          },
          {
            label: '具体时间',
            width: '300px',
            prop: 'specificTime',
          },
          {
            label: '培训主题',
            width: '280px',
            prop: 'trainTitle',
          },
          {
            label: '讲师要求',
            width: '120px',
            prop: 'instructorRequire',
          },
          {
            label: '渠道关注要点，讲解需求',
            width: '280px',
            prop: 'focusPoint',
          },
        ],
        deliveryStatusOptions: [],
        productLineOptions: [],
      }
    },
    computed: {
      ...mapGetters({
        userId: 'user/id',
      }),
    },
    activated() {
      this.fetchData()
    },
    mounted() {
      this.fetchData()
    },
    methods: {
      // 创建工单
      createOrder(val, type) {
        this.$refs['order-edit'].showEdit(val, type)
      },
      restFetchData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const params = { ...this.queryForm }
        const [err, res] = await to(Api.getList({ ...params }))
        this.listLoading = false
        if (err) return (this.listLoading = false)
        if (res.code == 200 && res.data) {
          this.list = res.data.list || []
          this.total = res.data.total
        }
        this.$nextTick(() => this.$refs.table.doLayout())
      },
      handleFinish(row) {
        this.$refs['finish'].showEdit(row)
      },
      //详情
      handleDetail(row) {
        this.$refs['details'].open(row)
      },
      reset() {
        this.queryForm = {
          pageNum: 1,
          pageSize: 10,
          distributorId: 0,
          mainProduct: '',
        }
        this.fetchData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
    },
  }
</script>

<style lang="scss" scoped>
  $base: '.list';
</style>
