<template>
  <div>

    <!-- 列表 -->
    <AutoResizer :given-height="250">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <!-- <el-table-column
        label="销售出库单"
        align="center"
        prop="saleOutNo"
        show-overflow-tooltip
      />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="产品编码" align="center" prop="materialNo" />
      <el-table-column label="产品名称" align="center" prop="materialName" /> -->
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="批号"
            align="center"
            prop="materialLots"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="生产订单"
            align="center"
            prop="erpOrderNo"
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="审核状态"
            align="center"
            prop="auditStatus"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_OQC_AUDIT_STATUS" :value="scope.row.auditStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="时间"
            align="center"
            prop="ipqcDate"
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="班别"
            align="center"
            prop="classFlag"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.CLASS_FLAG" :value="scope.row.classFlag" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="创建时间"
            align="center"
            prop="createTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="创建人"
            align="center"
            prop="creator"
          />
          <!-- <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-magic-stick"
                @click="handleInspection(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" />
    <!-- 检验执行 -->
    <InspectDetail ref="inspectDetail" @resetComplete="resetComplete" />
  </div>
</template>

<script>
import { getFQCpage } from '@/api/wms/quality/fqcInspectionExecute'
import SalesDetails from '@/views/wms/output/salesIssue/components/SalesDetails.vue'
import InspectDetail from './InspectDetail.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'SaleOutOrder',
  components: {
    SalesDetails,
    InspectDetail
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // wms销售出库单主列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        auditStatus: -1
      },
      // 表单参数
      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 }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.paramsQuery,
        ...this.queryParams
      }
      getFQCpage(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
    },
    //  检验执行
    handleInspection(row) {
      const { wmsIncomingReceiptInspectionId, materialLots, materialNo, auditStatus } = row
      // const params = {
      //   warehousingOrderNo: saleOutNo
      // }
      // oqccreateMaterialItemResult(params).then(res => {
      //   if (res.code !== 0) {
      //     this.$modal.msgWarning('暂未权限~')
      //     return
      //   }
      //   this.$refs.inspectDetail.visible = true
      //   this.$refs.oqcInspectList.queryParams.warehousingOrderNo = saleOutNo
      //   this.$refs.oqcInspectList.queryParams.saleOutNo = saleOutNo
      //   this.$refs.oqcInspectList.queryParams.type = 2
      // })
      this.$refs.inspectDetail.queryParams.materialLots = materialLots
      this.$refs.inspectDetail.queryParams.materialNo = materialNo
      this.$refs.inspectDetail.queryParams.key = wmsIncomingReceiptInspectionId
      this.$refs.inspectDetail.queryParams.type = 2
      this.$refs.inspectDetail.apiStatus = auditStatus
      this.$refs.inspectDetail.visible = true
    },
    oqcInspectComplete() {
      this.getList()
      this.$emit('oqcComplete')
    },
    resetComplete() {
      this.getList()
      this.$emit('resetComplete')
    },
    // 获取显示隐藏列
    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>

