<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-position="left"
      label-width="78px"
    >
      <SearchBar :show-span="12" :collapse-span="6">
        <template #header>
          <el-col :span="4">
            <el-form-item label="物料编码" prop="materialNo" label-width="68px">
              <el-input
                v-model="queryParams.materialNo"
                placeholder="请输入产品编码"
                clearable
                size="mini"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="来源仓库" prop="oldWmsStockId" label-width="68px">
              <el-input
                v-model="queryParams.oldWmsStockId"
                placeholder="请输入产品编码"
                clearable
                size="mini"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="目标仓库" prop="newWmsStockId" label-width="68px">
              <el-input
                v-model="queryParams.newWmsStockId"
                placeholder="请输入产品编码"
                clearable
                size="mini"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="6">
            <el-form-item label="产品名称" prop="name" label-width="68px">
              <el-input
                v-model="queryParams.name"
                placeholder="请输入产品名称"
                clearable
                size="mini"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col> -->
          <el-col :span="4">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker
                v-model="queryParams.createTime"
                style="width: 100%"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <div style="display: inline-block" />
          </el-col>
        </template>
        <template #body />
        <template #buttons>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </template>
      </SearchBar>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOperation(_, 'add')"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar
        :columns="columnsList"
        :show-search.sync="showSearch"
        @saveSetting="getColumnsList"
        @queryTable="getList"
      />
    </el-row>
    <vxe-table
      ref="xTable"
      border
      show-overflow
      keep-source
      highlight-current-row
      :loading="loading"
      :height="tableHeight"
      :row-config="{ height: '30' }"
      :column-config="{ resizable: true }"
      :data="list"
      :edit-config="{
        trigger: 'click',
        mode: 'row',
        icon: 'vxe-icon-edit',
        showStatus: true,
      }"
    >
      <vxe-column type="checkbox" width="50" />
      <vxe-column
        field="allotOrderNo"
        title="单号"
        align="center"
        width="110"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleViewProtuctDetail(row)">{{
            row.allotOrderNo
          }}</el-button>
        </template>
      </vxe-column>
      <vxe-column
        field="materialNo"
        title="物料编码"
        align="center"
        min-width="200"
      >
        <template #default="{ row }">
          <span>{{ row.materialNo }}</span>
        </template>
      </vxe-column>
      <vxe-column
        field="materialName"
        title="物料名称"
        min-width="200"
        align="center"
      >
        <template #default="{ row }">
          <span>{{ row.materialName }}</span>
        </template>
      </vxe-column>
      <vxe-column
        field="batchNumber"
        title="物料条码"
        align="center"
        width="160"
      >
        <template #default="{ row }">
          <span>{{ row.batchNumber }}</span>
        </template>
      </vxe-column>
      <vxe-column
        field="qty"
        title="数量"
        align="center"
        width="120"
      >
        <template #default="{ row }">
          <span>{{ row.qty }}</span>
        </template>
      </vxe-column>
      <vxe-column
        field="oldWmsStock"
        title="来源仓"
        align="center"
        width="160"
      >
        <template #default="{ row }">
          <span>{{ row.oldWmsStock }}</span>
        </template>
      </vxe-column>
      <vxe-column
        field="newWmsStock"
        title="目标仓"
        align="center"
        width="160"
      >
        <template #default="{ row }">
          <span>{{ row.newWmsStock }}</span>
        </template>
      </vxe-column>
      <vxe-column
        field="signStatus"
        title="签收状态"
        align="center"
        width="160"
      >
        <template #default="{ row }">
          <dict-tag :type="DICT_TYPE.WMS_SIGN_STATUS" :value="row.signStatus" />
        </template>
      </vxe-column>
      <vxe-column
        field="signEmployee"
        title="签收人"
        align="center"
        width="120"
      >
        <template #default="{ row }">
          <span>{{ row.signEmployee }}</span>
        </template>
      </vxe-column>
      <vxe-column
        field="createTime"
        title="创建时间"
        align="center"
        width="120"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </vxe-column>
      <vxe-column
        field="signTime"
        title="签收时间"
        align="center"
        width="120"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.signTime) }}</span>
        </template>
      </vxe-column>
      <!-- <vxe-column
        title="操作"
        width="100"
        show-overflow
        fixed="right"
      >
        <template #default="{ row }">
          <vxe-button type="text" icon="vxe-icon-edit" @click="handleOperation(row, 'edit')" />
          <vxe-button type="text" icon="vxe-icon-delete" @click="handleOperation(row, 'remove')" />
        </template>
      </vxe-column> -->
    </vxe-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getStockAllotMasterPage } from '@/api/wms/output/backFlushMaterial'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'MesMaterialAndProduct',
  components: {
    SearchBar: () => import('@/components/SearchBar')
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      tabIndex: 0,
      // 总条数
      total: 0,
      // 物料及产品列表列表
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
        materialNo: '',
        createTime: []
      },
      // 表格高度
      tableHeight: 620,
      // 表单参数
      form: {},
      // 显示隐藏列参数
      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()
  },
  mounted() {
    window.addEventListener('resize', (e) => {
      if (window.innerHeight >= 1000) {
        this.tableHeight = window.innerHeight - this.$refs.xTable.$el.offsetTop - 155
      } else {
        this.tableHeight = window.innerHeight - this.$refs.xTable.$el.offsetTop - 145
      }
    })
    this.tableHeight = window.innerHeight - this.$refs.xTable.$el.offsetTop - 180
  },
  methods: {
    handleOperation(row, menuStatus) {
      switch (menuStatus) {
      case 'add':
        this.$router.push({ path: '/entry-exit/product-out-in/backFlushMaterial/Info' })
        break
      case 'edit':
        this.$router.push({ path: '/base/material/materialBaseInfo?id=' + row?.id })
        break
      case 'remove':
        this.handleDelete(row)
        break
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getStockAllotMasterPage(this.queryParams)
        .then((response) => {
          this.list = response.data.list
          this.total = response.data.total
          this.loading = false
        })
        .finally(() => {
          this.loading = false
        })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleViewProtuctDetail(row) {
      this.$router.push({ path: '/entry-exit/product-out-in/backFlushMaterial/Info?view=true&allotOrderNo=' + row?.allotOrderNo })
    },
    // Bom详情
    handleViewBomDetail(row) {
      this.$router.push({ path: '/base/material/productBOM?opert=view&id=' + row?.id })
    },
    /** 删除产品按钮操作 */
    handleDelete(row) {
      const { id, materialName } = row
      const message = `是否确认删除物料及产品列表名称为【${materialName}】的数据项?`
      this.$modal
        .confirm(message)
        .then(() => {
          // return deleteMaterial(id)
        })
        .then(() => {
          this.handleQuery()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },
    /** 删除产品Bom按钮操作 */
    handleDeleteMaterialBom(row) {
      const { id, materialName } = row
      const message = `是否确认删除产品Bom名称为【${materialName}】的数据项?`
      this.$modal
        .confirm(message)
        .then(() => {
          // return deleteMaterialBOM(id)
        })
        .then(() => {
          this.handleQuery()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      const params = { ...this.queryParams }
      params.pageNo = undefined
      params.pageSize = undefined
      this.$modal
        .confirm('是否确认导出所有物料及产品列表数据项?')
        .then(() => {
          this.exportLoading = true
          // return exportBadTemsExcel(params)
        })
        .then((response) => {
          this.$download.excel(response, '物料及产品列表.xls')
          this.exportLoading = false
        })
        .catch(() => { })
    },
    // 获取显示隐藏列
    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>
<style lang="scss" scoped>
.app-container {
  margin: 4px;
  padding: 0;
  overflow: hidden;
  height: calc(100vh - 100px);
}

::v-deep .el-form-item {
  margin-bottom: 8px !important;
}
::v-deep .el-tabs__item {
  height: 30px !important;
  line-height: 30px !important;
  padding: 0 15px !important;
}
::v-deep .el-tabs--border-card > .el-tabs__content {
  padding: 2px !important;
}
::v-deep .pagination-container {
  margin: 0;
  padding: 0;
}
::v-deep .el-card__body {
  padding: 4px;
;
