<template>
  <div>

    <!-- 列表 -->
    <AutoResizer :given-height="220">
      <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="saleOrderNo"
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="客户名称"
            align="center"
            prop="client"
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="销售状态"
            align="center"
            prop="saleStatus"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_SALES_STATUS" :value="scope.row.saleStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="交货日期"
            align="center"
            prop="handOverTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.handOverTime) }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="出库状态" align="center" prop="outStatus" />
      <el-table-column label="检验结果" align="center" prop="testResult" /> -->
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="160"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-hasPermi="['wms:sale-out-order-detail:query']"
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
              >查看</el-button>
              <el-button
                v-hasPermi="['wms:sale-out-order:update']"
                size="mini"
                type="text"
                icon="el-icon-thumb"
                @click="handleIssue(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"
    />
    <!-- 销售明细 -->
    <SalesDetails ref="salesDetails" />
    <!-- 出库 -->
    <SalesIssue ref="salesIssue" @complete="issueComplete" />
  </div>
</template>

<script>
import { getSaleOutOrderPage } from '@/api/wms/output/salesIssue'
import SalesDetails from './SalesDetails.vue'
import SalesIssue from './SalesIssue.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'SaleOutOrder',
  components: {
    SalesDetails,
    SalesIssue
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // wms销售出库单主列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 高级查询
      advParams: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        saleOrderDetailId: [{ required: true, message: '关联销售单明细表ID不能为空', trigger: 'blur' }],
        saleOutNo: [{ required: true, message: '销售出库单号不能为空', trigger: 'blur' }]
      },
      // 显示隐藏列参数
      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 },
        { key: 7, label: '物料名称', visible: true },
        { key: 8, label: '客户名称', visible: true },
        { key: 9, label: '箱号', visible: true },
        { key: 10, label: '计划数量', visible: true },
        { key: 11, label: '装箱数量', visible: true },
        { key: 12, label: '单位', visible: true },
        { key: 13, label: '创建人', visible: true },
        { key: 14, label: '创建时间', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  // 获取当前登录用户id
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams,
        ...this.advParams
      }
      getSaleOutOrderPage(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    // 查看销售明细
    handleView(row) {
      const { id } = row
      this.$refs.salesDetails.queryParams.id = id
      this.$refs.salesDetails.open = true
      this.$refs.salesDetails.title = '销售明细'
    },
    // 出库
    handleIssue(row) {
      const { id } = row
      this.$refs.salesIssue.queryParams.id = id
      this.$refs.salesIssue.open = true
    },
    issueComplete() {
      this.getList()
      this.$emit('salesComplete')
    },
    // 获取显示隐藏列
    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
        }
      }).finally(() => {
        this.$emit('columns', this.columnsList)
      })
    }
  }
}
</script>

