<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="98px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="送货单号" prop="saleOutNo">
            <el-input
              v-model="queryParams.saleOutNo"
              placeholder="请输入送货单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="回签状态" prop="backSignStatus">
            <DictSelect v-model="queryParams.backSignStatus" placeholder="请选择回签状态" dict-type="WMS_DELIVERY_BACKSIGN_STATUS" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="销售订单" prop="erpOrderNo">
            <el-input
              v-model="queryParams.erpOrderNo"
              placeholder="请输入销售订单"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </QueryFormWrapper>
    </el-form>

    <!-- 右侧工具栏 -->
    <right-toolbar
      :columns="columnsList"
      :show-search.sync="showSearch"
      @saveSetting="getColumnsList"
      @queryTable="getList"
    />

    <!-- 列表 -->
    <AutoResizer :given-height="177">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="送货单号"
            align="center"
            prop="saleOutNo"
            width="150"
            fixed="left"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="客户编码"
            align="center"
            prop="customerCode"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="客户名称"
            align="center"
            prop="customerName"
            width="180"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="销售订单"
            align="center"
            prop="erpOrderNo"
            width="160"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="回签状态"
            align="center"
            prop="backSignStatus"
            width="120"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_DELIVERY_BACKSIGN_STATUS" :value="scope.row.backSignStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="创建人"
            align="center"
            prop="creator"
            width="100"
          />
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="创建时间"
            align="center"
            prop="createTime"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>

          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="80"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
              >查看</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 回签明细 -->
    <endorsementDetails ref="endorsementDetails" />

  </div>
</template>

<script>
import { getSaleOutOrderPage } from '@/api/wms/orders/deliveryEndorsement'
import endorsementDetails from './endorsementDetails.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'DeliveryEndorsement',
  components: {
    endorsementDetails
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      synchronizationLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 是否显示明细
      detailVisible: false,
      // 总条数
      total: 0,
      // ERP采购订单主列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      ids: '',
      currentRows: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        outStatus: 2,
        backSignStatus: null,
        saleOutNo: null,
        erpOrderNo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 显示隐藏列参数
      columns: [
        { key: 0, label: '送货单号', visible: true },
        { key: 1, label: '客户编码', visible: true },
        { key: 2, label: '客户名称', visible: true },
        { key: 3, label: '销售订单', visible: true },
        { key: 4, label: '回签状态', visible: true },
        { key: 5, label: '创建人', visible: true },
        { key: 6, label: '创建时间', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    this.queryParams.workOrderBomNo = this.$route.query.workOrderBomNo
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getSaleOutOrderPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        erpOrderId: undefined,
        erpOrderNo: undefined,
        erpOrderTime: undefined,
        billAmount: undefined,
        billType: undefined,
        taxAmount: undefined,
        totalAmount: undefined,
        supplierCode: undefined,
        documentType: undefined,
        status: undefined,
        sourceOrderNo: undefined,
        sourceOrderType: undefined,
        exchangeRate: undefined,
        erpMdtime: undefined,
        erpMark: undefined,
        deliveryTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 查看明细
    handleView(row) {
      const { saleOutNo } = row
      this.$refs.endorsementDetails.queryParams.saleOutNo = saleOutNo
      this.$refs.endorsementDetails.detailVisible = true
    },
    // 获取显示隐藏列
    getColumnsList() {
      const { componentName } = this.$route.meta || null
      const params = {
        userId: this.userId,
        vueForm: componentName
      }
      getFormColumnsList(params).then(res => {
        if (isArray(res.data)) {
          this.columnsList = res.data.length === 0 ? this.columns : res.data
        } else {
          this.columnsList = this.columns
        }
      }).catch((err) => {
        if (err) {
          this.columnsList = this.columns
        }
      })
    }
  }
}
</script>

