<!-- 搜索表单 -->
<template>
  <el-form
    label-width="100px"
    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.planCode" 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: 6, md: 12 } : { span: 6 }">
        <el-form-item label="计划名称:">
          <el-input clearable v-model="where.planName" placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="盘点部门:">
          <DeptSelect v-model="where.executeGroupId" />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="盘点人员:">
          <el-select
            filterable
            class="w100"
            size="small"
            v-model="where.executorId"
            placeholder="请选择"
          >
            <el-option
              v-for="item in userList"
              :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-select
            filterable
            class="w100"
            size="small"
            v-model="where.status"
            placeholder="请选择"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </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 DeptSelect from '@/components/CommomSelect/new-dept-select.vue';
  import storageApi from '@/api/warehouseManagement/index.js';
  import warehouseDefinition from '@/api/warehouseManagement/warehouseDefinition';
  export default {
    components: { DeptSelect },
    data() {
      // 默认表单数据
      const defaultWhere = {
        name: '',
        factoryId: '',
        warehouseId: '',
        executorId: '',
        code: '',
        executeGroupId: '',
        status: '',
        planCode: '',
        executeGroupName: '',
        executorName: ''
      };
      return {
        defaultWhere: { ...defaultWhere },
        // 表单数据
        statusOptions: [
          {
            label: '待处理',
            value: 0
          },
          {
            label: '执行中',
            value: 1
          },
          {
            label: '已完成',
            value: 2
          }
        ],
        statusObj: {
          0: '待处理',
          1: '执行中',
          2: '已完成'
        },
        userList: [],
        warehouseList: [],
        factoryList: [],
        where: { ...defaultWhere },
        treeData: []
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {
      this.getFactoryList();
      this.getuserList();
    },
    methods: {
      /* async nodeClick(id, data) {
        this.where.executeGroupId = id;
        if (data) {
          this.where.executeGroupName = data.name;
        }
        this.where.executorName = '';
        this.where.executorId = '';
        if (id) {
          let userData = await warehouseDefinition.getUserPage({
            groupId: id,
            size: 9999,
            pageNum: 1
          });
          this.userList = userData.list;
        }
      }, */

      async getuserList() {
        let userData = await warehouseDefinition.getUserPage({
          size: 9999,
          pageNum: 1
        });
        this.userList = userData.list;
      },
      async getTreeData(val) {
        this.where.targetWarehouse = '';
        this.where.sourceWarehouse = '';
        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.where = { ...this.defaultWhere };
        this.search();
      }
    }
  };
</script>
<style lang="scss" scoped>
  .ele-form-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
</style>
