<template>
  <el-card v-loading="loadingStatus" element-loading-text="生成Excel文件中,请稍后" element-loading-background="rgba(0, 0, 0, 0.2)">
    <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent>
      <el-form-item label="出库单号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入出库单号" size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品号" prop="colorCode">
        <el-input v-model="queryParams.colorCode" placeholder="请输入品号" size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="品名" prop="colorName">
        <el-input v-model="queryParams.colorName" placeholder="请输入品名" size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="事业部" prop="subsidiaryId">
        <el-select v-model="queryParams.subsidiaryId" placeholder="请选择事业部" size="small" @change="handleQuery">
          <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseName">
        <el-input v-model="queryParams.warehouseName" placeholder="请输入仓库" size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出库类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择入库类型" size="small" @change="handleQuery">
          <el-option v-for="item in stockType" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey" />
        </el-select>
      </el-form-item>
      <el-form-item label="日期:">
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择开始日期"
            v-model="queryParams.strTime"
            value-format="yyyy-MM-dd 00:00:00"
            size="small"
            style="width: 100%"
            @change="handleQuery"
            :picker-options="pickerOptions()"
          ></el-date-picker>
        </el-col>
        <el-col class="line" :span="2" style="text-align: center">至</el-col>
        <el-col :span="11">
          <el-date-picker
            type="date"
            placeholder="选择结束日期"
            v-model="queryParams.endTime"
            value-format="yyyy-MM-dd 23:59:59"
            size="small"
            style="width: 100%"
            @change="handleQuery"
            :picker-options="pickerOptions()"
          ></el-date-picker>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button size="small" @click="handleQuery" class="searchBtn">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" style="margin-bottom: 10px">
      <el-col :span="24">
        <el-button type="primary" size="mini" @click="handleAdd" v-if="!userInfo.subsidiaryId" v-db-click>出库登记</el-button>
        <el-button type="primary" size="mini" @click="handleExcel" v-db-click>导出Excel</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableList" :cell-style="{ padding: '0' }" :row-style="{ height: '35px' }" v-loading="loading" header-row-class-name="tableHeader">
      <el-table-column label="出库单号" prop="code" width="180" />
      <el-table-column label="归属事业部" width="140">
        <template slot-scope="scope">
          <span v-if="scope.row.subsidiaryName">{{ scope.row.subsidiaryName }}</span>
          <span v-else>胜德体育</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库类型" width="120">
        <template slot-scope="scope">
          <span>{{ getWarehouseName(scope.row.warehouseId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库" prop="warehouseName" width="120" />
      <el-table-column label="出库类型" width="120" :formatter="statusFormat" />
      <el-table-column label="品号" prop="colorCode" width="140" />
      <el-table-column label="品名" prop="colorName" min-width="300" />
      <el-table-column label="数量" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.quantity">{{ Number(scope.row.quantity) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出库时间" align="center" prop="createTime" width="140" />
      <el-table-column label="出库申请人" prop="proposer" width="120" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 验证密码 -->
    <el-dialog title="验证密码" v-if="openCheckPWD" :visible.sync="openCheckPWD" width="30%" append-to-body>
      <el-form ref="ruleForm" :model="pwd" label-width="110px" @submit.native.prevent>
        <el-form-item label="密码:" prop="projectPrice" class="other-charges">
          <el-input v-model="pwd" size="small" placeholder="请输入密码" @keyup.enter.native="clickPassword" />
        </el-form-item>
        <div style="width: 100%; text-align: center">
          <el-button type="primary" @click="clickPassword" size="small" v-db-click>确 认</el-button>
          <el-button size="small" @click="openCheckPWD = false">取 消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
import * as API from '@/api/shengde/warehouse/outAndInWarehouse'
import { mapGetters } from 'vuex'
import { warehouseList } from '@/api/shengde/warehouse/management'
import { getList } from '@/api/shengde/group/subcompany/subcompanyManagement/index.js'
import Utils from '@/util/transit'

export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: 2,
        subsidiaryId: '',
        warehouseName: '',
        code: '',
        type: '',
        strTime: '',
        endTime: '',
        colorCode: '',
        colorName: '',
      },
      loading: false,
      tableList: [],
      total: 0,
      warehouseList: [],
      stockType: '',
      companyList: [{ id: '0', name: '胜德体育' }],
      pwd: '',
      openCheckPWD: false,
      loadingStatus: false,
    }
  },
  created() {
    this.stockType = this.dictData.filter((item) => item.code === 'come_stock_type')[0].children
    warehouseList({ pageNum: 1, pageSize: 9999 }).then((res) => {
      this.warehouseList = res.data.data.list
      this.loading = false
    })
    getList({ pageNum: 1, pageSize: 9999 }).then((res) => {
      this.companyList = this.companyList.concat(res.data.data.records)
    })
  },
  mounted() {
    this.getList()
    Utils.$on('refreshInList', () => {
      this.getList()
    })
    Utils.$on('outboundSearchCode', (val) => {
      this.queryParams.code = val
      this.getList()
    })
  },
  computed: mapGetters(['dictData', 'userInfo']),
  methods: {
    getList() {
      this.loading = true
      API.detailsList(this.queryParams).then(
        (res) => {
          this.tableList = res.data.data.list
          this.loading = false
        },
        (err) => {
          console.log('detailsList: ' + err)
          this.loading = false
        }
      )
      API.detailsListCount(this.queryParams).then((res) => {
        this.total = res.data.data.count
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.strTime = ''
      this.queryParams.endTime = ''
      this.handleQuery()
    },
    getWarehouseName(id) {
      if (this.warehouseList && this.warehouseList.length > 0 && id) {
        let data = this.warehouseList.filter((item) => item.id === id)
        if (data && data.length > 0) {
          return data[0].name
        }
      }
      return ''
    },
    statusFormat(row) {
      if (row.type) {
        return this.selectConstantsLabel(this.stockType, row.type + '')
      } else {
        return ''
      }
    },
    handleAdd() {
      // this.pwd = ''
      // this.openCheckPWD = true
      let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
      let maxPos = $chars.length
      let random = ''
      for (let i = 0; i < 32; i++) {
        random += $chars.charAt(Math.floor(Math.random() * maxPos))
      }
      this.$router.push({
        path: '/outbound/register',
        query: {
          random: random,
        },
      })
    },
    clickPassword() {
      if (this.pwd) {
        if (this.pwd === 'sd2022') {
          this.msgSuccess('验证成功')
          this.openCheckPWD = false
          let $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
          let maxPos = $chars.length
          let random = ''
          for (let i = 0; i < 32; i++) {
            random += $chars.charAt(Math.floor(Math.random() * maxPos))
          }
          this.$router.push({
            path: '/outbound/register',
            query: {
              random: random,
            },
          })
        } else {
          this.msgInfo('密码验证错误')
        }
      } else {
        this.msgInfo('请输入密码')
      }
    },
    handleExcel() {
      this.loadingStatus = true
      let queryParams = JSON.parse(JSON.stringify(this.queryParams))
      queryParams.pageNum = 1
      queryParams.pageSize = 9999
      API.detailsExportExcel(queryParams).then(
        (res) => {
          this.loadingStatus = false
          this.exportFile(res, '出库明细表.xlsx')
        },
        (err) => {
          console.log('detailsExportExcel: ' + err)
          this.loadingStatus = false
        }
      )
    },
  },
}
</script>

<style lang="scss" scoped>
* {
  font-size: 12px;
}
.searchBtn {
  background: #20b2aa;
  color: #fff;
  border: 1px solid #20b2aa;
}

::v-deep {
  .el-input__inner {
    border-radius: 1px;
  }
  .el-button--mini {
    border-radius: 1px;
  }
  .tableHeader th {
    background-color: #edf0f5;
    height: 35px;
    padding: 0;
  }
}
</style>
