<!-- 搜索表单 -->
<template>
  <el-form
    label-width="80px"
    class="ele-form-search"
    @keyup.enter.native="search"
    @submit.native.prevent
  >
    <el-row :gutter="15">
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="所属工厂" prop="factoryId">
          <el-select
            filterable
            placeholder="请选择"
            @change="getTreeData"
            v-model="where.factoryId"
            clearable
            class="w100"
          >
            <el-option
              v-for="item in factoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="仓库" prop="warehouseId">
          <el-select
            filterable
            placeholder="请选择"
            v-model="where.warehouseId"
            clearable
            class="w100"
          >
            <el-option
              v-for="item in warehouseList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="拣货名称:">
          <el-input
            clearable
            v-model="where.name"
            placeholder="请输入物品名称"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="拣货编码:">
          <el-input
            clearable
            v-model="where.code"
            placeholder="请输入物品编码"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 24, md: 24 } : { span: 24 }">
        <div class="ele-form-actions">
          <el-button
            type="primary"
            icon="el-icon-search"
            class="ele-btn-icon"
            @click="search"
            size="small"
          >
            查询
          </el-button>
          <el-button
            @click="reset"
            icon="el-icon-refresh-left"
            size="small"
            type="primary"
            >重置</el-button
          >
        </div>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
  import { login } from '@/api/login';
  import storageApi from '@/api/warehouseManagement/index.js';
  import { allocationType } from '@/utils/dict/warehouse';
  import warehouseDefinition from '@/api/warehouseManagement/warehouseDefinition';
  export default {
    data() {
      // 默认表单数据
      const defaultWhere = {
        factoryId: '',
        name: '',
        code: '',
        warehouseId: ''
      };
      return {
        // 表单数据
        where: {
          factoryId: '',
          name: '',
          code: '',
          warehouseId: ''
        },
        defaultWhere: {
          factoryId: '',
          name: '',
          code: '',
          warehouseId: ''
        },
        treeLoading: false,
        allocationType,
        treeData: [],
        factoryList: [],
        warehouseList: []
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {
      this.getFactoryList();
    },
    methods: {
      async getTreeData(val) {
        this.where.warehouseId = '';
        try {
          this.treeLoading = true;
          let res = await storageApi.getWarehouseTrees({
            factoriesId: val
          });
          this.treeLoading = false;
          if (res?.code === '0') {
            this.warehouseList = res.data;
          }
        } catch (error) {
          console.log(error);
        }
        this.treeLoading = false;
      },
      //获取工厂列表
      async getFactoryList() {
        const res = await warehouseDefinition.getFactoryarea({
          pageNum: 1,
          size: 9999,
          type: 1
        });
        this.factoryList = res.list;
      },
      /* 搜索 */
      search() {
        console.log(this.where);
        this.$emit('search', this.where);
      },
      /*  重置 */
      reset() {
        this.warehouseList = [];
        this.where = { ...this.defaultWhere };
        this.search();
      }
    }
  };
</script>
<style lang="scss" scoped>
  .ele-form-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
</style>
