<template>
  <div class="page">
    <el-form :inline="true" v-if="searchVisible" class="query-form m-b-10" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
      <!-- 搜索框-->
<!--      <el-form-item label="入库编号" prop="wareHouseNumber">-->
<!--        <el-input size="small" v-model="searchForm.wareHouseNumber" placeholder="请输入入库编号" clearable></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="商品名称" prop="tradeName">
        <el-input v-model="searchForm.tradeName" placeholder="请输入商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="入库类型" prop="wareHouseType">
        <SelectTree
          ref="areaTree"
          :props="{
                  value: 'id',             // ID字段名
                  label: 'name',         // 显示名称
                  children: 'children'    // 子级字段名
                }"
          url="/public-modules-server/material/materialType/summaryTreeData?type=last"
          :value="searchForm.wareHouseType"
          :clearable="true"
          :accordion="true"
		  size="default"
          @getValue="(value, label) => {searchForm.wareHouseType=value}"/>
      </el-form-item>
      <el-form-item label="供应商" prop="supplierName">
        <el-input v-model="searchForm.supplierName" placeholder="请选择供应商" :readonly="true" @focus="openSupplierChoose" ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="refreshList()"  icon="el-icon-search">查询</el-button>
        <el-button @click="resetSearch()"  icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="jp-table top" style="">
		<vxe-toolbar :refresh="{query: refreshList}" custom>
			<template #tools>
				<vxe-button
					text
					type="primary"
					:title="searchVisible ? '收起检索' : '展开检索'"
					icon="vxe-icon-search"
					class="tool-btn"
					@click="searchVisible = !searchVisible"
				></vxe-button>
			</template>
		</vxe-toolbar>
      <div style="height: calc(100% - 90px)">
        <vxe-table
          :key="tableKey"
          border="inner"
          auto-resize
          resizable
          height="auto"
          :loading="loading"
          ref="clientTable"
          show-header-overflow
          show-overflow
          highlight-hover-row
          :menu-config="{}"
          @sort-change="sortChangeHandle"
          :sort-config="{remote:true}"
          :data="dataList"
          :checkbox-config="{}"
          :edit-config="{trigger: 'click', mode: 'row', showStatus: false, autoClear: true, icon: 'false'}"
        >
          <vxe-column type="seq" width="60" title="序号"></vxe-column>
<!--          <vxe-column min-width="160" align="center" title="入库编号" field="wareHouseNumber"></vxe-column>-->
<!--          <vxe-column min-width="160" align="center" title="入库人" field="wareHouseManName"></vxe-column>-->
<!--          <vxe-column min-width="160" align="center" title="入库部门" field="wareHouseManOfficeName"></vxe-column>-->
          <vxe-column min-width="160" align="center" title="商品名称" field="tradeName">
            <template #default="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('material:list')" @click="view(scope.row)">{{scope.row.tradeName}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('material:list')"  @click="view(scope.row)">{{scope.row.tradeName}}</el-link>
              <span v-else>{{scope.row.tradeName}}</span>
            </template>
          </vxe-column>
          <vxe-column min-width="160" align="center" title="入库类型" field="wareHouseTypeName"></vxe-column>
          <vxe-column min-width="160" align="center" title="总量" field="allNumber"></vxe-column>
          <vxe-column min-width="160" align="center" title="领用量" field="borrowNumber"></vxe-column>
          <vxe-column min-width="160" align="center" title="剩余量" field="tradeNumber"></vxe-column>
          <vxe-column min-width="160" align="center" title="单位" field="company"></vxe-column>
          <vxe-column min-width="160" align="center" fixed="right" title="提醒数量" field="warnNum" :edit-render="{}">
            <template #edit="scope">
              <vxe-input v-model="scope.row.warnNum" type="float" min="0" @blur="changeWarnNum(scope.row)" :controls="false"></vxe-input>
            </template>
          </vxe-column>
          <vxe-column min-width="100" align="center" fixed="right" title="是否提醒" field="warnFlag">
            <template #default="scope">
              <el-switch
                :model-value="scope.row.warnFlag === '1'"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="updateStatus(scope.row.tradeName, scope.row.wareHouseType)"
              >
              </el-switch>
            </template>
          </vxe-column>
<!--          <vxe-column min-width="160" align="center" title="入库时间" field="wareHouseDate"></vxe-column>-->
          <vxe-column title="操作" width="80px" fixed="right" align="center">
            <template  #default="scope">
              <el-button v-if="hasPermission('material:summary:edit')" text type="primary"  size="small" @click="edit(scope.row.tradeName, scope.row.wareHouseType, scope.row.wareHouseTypeName)">修改</el-button>
            </template>
          </vxe-column>
        </vxe-table>
        <vxe-pager
          background
          :current-page="tablePage.currentPage"
          :page-size="tablePage.pageSize"
          :total="tablePage.total"
          :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
          @page-change="currentChangeHandle">
        </vxe-pager>
        <WareHouseAddForm ref="wareHouseAddForm" @refreshList="refreshList"></WareHouseAddForm>
        <WareHouseHistory ref="wareHouseHistory"></WareHouseHistory>
        <WareHouseHistoryPopup ref="wareHouseHistoryPopup"></WareHouseHistoryPopup>
        <SupplierChooseForm ref="supplierChooseForm" @getSupplier="getSupplier"></SupplierChooseForm>
        <EditTradeNameForm ref="editTradeNameForm" @refreshList="refreshList"></EditTradeNameForm>
      </div>
    </div>
  </div>
</template>

<script>
  import WareHouseAddForm from '../wareHouse/WareHouseAddForm'
  import WareHouseHistory from './WareHouseHistory'
  import WareHouseHistoryPopup from './WareHouseHistoryPopup'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import MaterialManagementService from '@/api/materialManagement/MaterialManagementService'
  import WareHouseService from '@/api/materialManagement/WareHouseService'
  import processService from '@/api/flowable/processService'
  import userService from '@/api/sys/userService'
  import SupplierChooseForm from '@/views/materialManagement/supplier/SupplierChooseForm'
  import EditTradeNameForm from '@/views/materialManagement/wareHouseSummary/EditTradeNameForm'
  export default {
    data () {
      return {
		  searchVisible: true,
        num: 0,
        searchForm: {
          wareHouseType: '',
          tradeName: '',
          wareHouseNumber: '',
          wareHouseMan: '',
          wareHouseManOffice: '',
          wareHouseDates: [],
          procurementType: '',
          createBy: '',
          supplierId: '',
          supplierName: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        tableKey: '',
        loading: false,
        processDefinitionAuditId: '',
        procDefAuditKey: '',
        isAdmin: false,
        create: ''
      }
    },
    materialManagementService: null,
    wareHouseService: null,
    created () {
      this.materialManagementService = new MaterialManagementService()
      this.wareHouseService = new WareHouseService()
    },
    components: {
      WareHouseHistory,
      WareHouseHistoryPopup,
      SelectTree,
      WareHouseAddForm,
      SupplierChooseForm,
      EditTradeNameForm
    },
    computed: {
      userName () {
        return this.$store.state.user.name
      },
      user () {
        this.createName = this.$store.state.user.name
        return this.$store.state.user
      }
    },
    mounted () {
      this.refreshList()
    },
    activated () {
      this.refreshList()
    },
    methods: {
      // 查看
      view (row) {
        // this.$refs.wareHouseHistoryPopup.init()
        let detailId = this.commonJS.isNotEmpty(this.searchForm.supplierId) ? row.id : ''
        this.$refs.wareHouseHistory.init(row.tradeName, detailId)
      },
      // 修改商品名称
      edit (tradeName, type, typeName) {
        this.$refs.editTradeNameForm.init(tradeName, type, typeName)
      },
      // 查询当前用户是否是管理员用户
      checkIsAdmin () {
        userService.is().then((data) => {
          this.isAdmin = data
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.wareHouseService.wareHouseSummaryList({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then((data) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.tableKey = Math.random()
          this.loading = false
        })
        this.checkIsAdmin()
        processService.getByName('物资管理-采购申请').then((data) => {
          if (!this.commonJS.isEmpty(data.id)) {
            this.processDefinitionAuditId = data.id
            this.procDefAuditKey = data.key
          }
        })
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
      sortChangeHandle (column) {
        this.tablePage.orders = []
        if (column.order != null) {
          this.tablePage.orders.push({column: this.$utils.toLine(column.property), asc: column.order === 'asc'})
        }
        this.refreshList()
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.searchForm.supplierId = '' // 重置时清空供应商筛选
        this.refreshList()
      },
      // 修改提醒状态 启用/停用
      updateStatus (tradeName, wareHouseType) {
        this.loading = true
        this.wareHouseService.updateWarnFlagByTradeNameAndType(tradeName, wareHouseType).then((data) => {
          this.$message.success(data)
          this.loading = false
          this.refreshList()
        }).catch(() => {
          this.refreshList()
          this.loading = false
        })
      },
      // 修改提醒数量blur事件
      changeWarnNum (row) {
        this.loading = true
        // 如果提醒数量的值为空的话，将赋值为0
        if (this.commonJS.isEmpty(row.warnNum)) {
          row.warnNum = 0.00
        }
        // 根据商品名称、入库类型修改提醒数量
        this.wareHouseService.updateWarnNumByTradeNameAndType(row.tradeName, row.wareHouseType, row.warnNum).then((data) => {
          this.$message.success(data)
          this.refreshList()
          this.loading = false
        }).catch(() => {
          this.refreshList()
          this.loading = false
        })
      },
      getSupplier (row) {
        this.searchForm.supplierId = row.id
        this.searchForm.supplierName = row.name
      },
      // 打开供应商选择组件
      openSupplierChoose () {
        this.$refs.supplierChooseForm.init()
      }
    }
  }
</script>
