<template>
  <div class="page">
    <el-form size="small" :inline="true" class="query-form" 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 size="small" 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="/material/materialType/summaryTreeData?type=last"
          :value="searchForm.wareHouseType"
          :clearable="true"
          :accordion="true"
          @getValue="(value, label) => {searchForm.wareHouseType=value}"/>
      </el-form-item>
<!--      <el-form-item label="入库人" prop="wareHouseMan">-->
<!--        <UserSelect :limit='1' :userName="searchForm.wareHouseMan" @getValue='(value, label) => {searchForm.wareHouseMan = label}'></UserSelect>-->
<!--&lt;!&ndash;        <SelectUserTree&ndash;&gt;-->
<!--&lt;!&ndash;          ref="companyTree"&ndash;&gt;-->
<!--&lt;!&ndash;          :props="{&ndash;&gt;-->
<!--&lt;!&ndash;                  value: 'id',             // ID字段名&ndash;&gt;-->
<!--&lt;!&ndash;                  label: 'name',         // 显示名称&ndash;&gt;-->
<!--&lt;!&ndash;                  children: 'children'    // 子级字段名&ndash;&gt;-->
<!--&lt;!&ndash;                }"&ndash;&gt;-->
<!--&lt;!&ndash;          :url="`/sys/user/treeUserDataAllOffice?type=2`"&ndash;&gt;-->
<!--&lt;!&ndash;          :value="searchForm.wareHouseMan"&ndash;&gt;-->
<!--&lt;!&ndash;          :clearable="true"&ndash;&gt;-->
<!--&lt;!&ndash;          :accordion="true"&ndash;&gt;-->
<!--&lt;!&ndash;          @getValue="(value) => {searchForm.wareHouseMan=value}"/>&ndash;&gt;-->
<!--      </el-form-item>-->
<!--      <el-form-item  prop="wareHouseManOffice" label="入库人部门">-->
<!--        <SelectTree-->
<!--          ref="officeTree"-->
<!--          :props="{-->
<!--                  value: 'id',             // ID字段名-->
<!--                  label: 'name',         // 显示名称-->
<!--                  children: 'children'    // 子级字段名-->
<!--                }"-->

<!--          :url="`/sys/office/treeData?type=1`"-->
<!--          :value="searchForm.wareHouseManOffice"-->
<!--          :accordion="true"-->
<!--          @getValue="(value) => {searchForm.wareHouseManOffice=value}"/>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="入库时间" prop="wareHouseDates">-->
<!--        <el-date-picker-->
<!--          placement="bottom-start"-->
<!--          format="yyyy-MM-dd HH:mm:ss"-->
<!--          value-format="yyyy-MM-dd HH:mm:ss"-->
<!--          v-model="searchForm.wareHouseDates"-->
<!--          type="datetimerange"-->
<!--          range-separator="至"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->

      <el-form-item>
        <el-button type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
        <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="bg-white top" style="">
      <div style="height: calc(100% - 90px)">
        <vxe-table
          :key="tableKey"
          border="inner"
          auto-resize
          resizable
          height="auto"
          :loading="loading"
          size="small"
          ref="clientTable"
          show-header-overflow
          show-overflow
          highlight-hover-row
          :menu-config="{}"
          @sort-change="sortChangeHandle"
          :sort-config="{remote:true}"
          :data="dataList"
          :checkbox-config="{}">
          <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="wareHouseTypeName"></vxe-column>
          <vxe-column min-width="160" align="center" title="商品名称" field="tradeName">
            <template slot-scope="scope">
              <el-link  type="primary" :underline="false" v-if="hasPermission('material:list')" @click="view(scope.row.tradeName)">{{scope.row.tradeName}}</el-link>
              <el-link  type="primary" :underline="false" v-else-if="hasPermission('material:list')"  @click="view(scope.row.tradeName,)">{{scope.row.tradeName}}</el-link>
              <span v-else>{{scope.row.tradeName}}</span>
            </template>
          </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" title="入库时间" field="wareHouseDate"></vxe-column>-->

        </vxe-table>
        <vxe-pager
          background
          size="small"
          :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>
      </div>
    </div>
  </div>
</template>

<script>
  import UserSelect from '@/components/userSelect'
  import WareHouseAddForm from '../wareHouse/WareHouseAddForm'
  import WareHouseHistory from './WareHouseHistory'
  import WareHouseHistoryPopup from './WareHouseHistoryPopup'
  import InputNumber from '@/views/modules/cw/workContract/InputNumber.vue'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import WorkClientService from '@/api/cw/workClientInfo/WorkClientService'
  import MaterialManagementService from '@/api/materialManagement/MaterialManagementService'
  import WareHouseService from '@/api/materialManagement/WareHouseService'
  import TaskService from '@/api/flowable/TaskService'
  import ProcessService from '@/api/flowable/ProcessService'
  import UserService from '@/api/sys/UserService'
  import SelectUserTree from '@/views/modules/utils/treeUserSelect'
  export default {
    data () {
      return {
        num: 0,
        searchForm: {
          wareHouseType: '',
          tradeName: '',
          wareHouseNumber: '',
          wareHouseMan: '',
          wareHouseManOffice: '',
          wareHouseDates: [],
          procurementType: '',
          createBy: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        tableKey: '',
        loading: false,
        processDefinitionAuditId: '',
        procDefAuditKey: '',
        isAdmin: false,
        create: ''
      }
    },
    workClientService: null,
    materialManagementService: null,
    wareHouseService: null,
    taskService: null,
    processService: null,
    userService: null,
    created () {
      this.workClientService = new WorkClientService()
      this.materialManagementService = new MaterialManagementService()
      this.wareHouseService = new WareHouseService()
      this.taskService = new TaskService()
      this.processService = new ProcessService()
      this.userService = new UserService()
    },
    components: {
      UserSelect,
      InputNumber,
      SelectUserTree,
      WareHouseHistory,
      WareHouseHistoryPopup,
      SelectTree,
      WareHouseAddForm
    },
    computed: {
      userName () {
        return JSON.parse(localStorage.getItem('user')).name
      },
      user () {
        this.createName = JSON.parse(localStorage.getItem('user')).name
        return JSON.parse(localStorage.getItem('user'))
      }
    },
    mounted () {
      this.refreshList()
    },
    activated () {
      this.refreshList()
    },
    methods: {
      // 查看
      view (tradeName) {
        // this.$refs.wareHouseHistoryPopup.init()
        this.$refs.wareHouseHistory.init(tradeName)
      },
      // 查询当前用户是否是管理员用户
      checkIsAdmin () {
        this.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()
        this.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.refreshList()
      }
    }
  }
</script>
