<!-- 搜索表单 -->
<template>
  <el-form
    label-width="77px"
    class="ele-form-search"
    @keyup.enter.native="search"
    @submit.native.prevent
  >
    <el-row :gutter="10">
      <el-col v-bind="styleResponsive ? { md: 4 } : { span: 4 }">
        <el-form-item label="物品编码">
          <el-input
            size="mini"
            clearable
            v-model.trim="where.categoryCode"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { md: 4 } : { span: 4 }">
        <el-form-item label="名称">
          <el-input
            size="mini"
            clearable
            v-model.trim="where.categoryName"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { md: 4 } : { span: 4 }">
        <el-form-item label="状态">
          <el-select
            clearable
            v-model.trim="where.status"
            placeholder="请选择"
            class="w100"
            size="mini"
          >
            <el-option
              v-for="item in Object.entries(statusOpt)"
              :key="item[0]"
              :label="item[1]"
              :value="item[0]"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col v-bind="styleResponsive ? { md: 4 } : { span: 4 }">
        <el-form-item label="级别">
          <el-select
            clearable
            v-model.trim="where.level"
            placeholder="请选择"
            class="w100"
            size="mini"
          >
            <el-option
              v-for="item in bomLevelList"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col v-bind="styleResponsive ? { md: 4 } : { span: 4 }">
        <el-form-item label="存货类型">
          <el-select
            clearable
            v-model.trim="where.attributeType"
            placeholder="请选择"
            class="w100"
            size="mini"
          >
            <el-option
              v-for="item in attributeList"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col v-bind="styleResponsive ? { md: 4 } : { span: 4 }">
        <el-form-item label="规格">
          <el-input
            size="mini"
            clearable
            v-model.trim="where.specification"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>

      <el-col v-bind="styleResponsive ? { md: 4 } : { span: 4 }">
        <el-form-item label="型号">
          <el-input
            size="mini"
            clearable
            v-model.trim="where.modeType"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <!-- <el-col v-bind="styleResponsive ? { lg: 5, md: 10 } : { span: 5}">
        <el-form-item label="组织机构:">
          <auth-selection size="mini" v-model.trim="where.deptIds" style="width: 100%"></auth-selection>
        </el-form-item>
      </el-col> -->

      <el-col v-bind="styleResponsive ? { md: 8 } : { span: 4 }">
        <div class="ele-form-actions">
          <el-button
            type="primary"
            icon="el-icon-search"
            class="ele-btn-icon"
            @click="search"
            size="mini"
          >
            查询
          </el-button>

          <el-button
            @click="reset"
            icon="el-icon-refresh"
            class="ele-btn-icon"
            size="mini"
            >重置</el-button
          >

          <el-button
            v-if="isUpload && $hasPermission('main:categoryparam:saveBom')"
            @click="uploadFile"
            type="primary"
            size="mini"
            icon="el-icon-upload2"
            plain
            >批量导入</el-button
          >
        </div>
      </el-col>
    </el-row>

    <importDialog
      :defModule="moudleName"
      ref="importDialogRef"
      @success="success"
    />
  </el-form>
</template>

<script>
  import importDialog from './import-dialog.vue';
  import { getByCode } from '@/api/system/dictionary-data';
  export default {
    components: {
      importDialog
    },
    props: {
      statusOpt: Object,
      categoryCode: String,
      isUpload: {
        type: Boolean,
        default: false
      }
    },
    data() {
      // 默认表单数据
      const defaultWhere = {
        name: '',
        code: '',
        categoryCode: this.categoryCode,
        categoryName: '',
        status: '',
        attributeType: '',
        specification: '',
        modeType: '',
        level: ''
      };
      return {
        defaultWhere,
        // 表单数据
        where: { ...defaultWhere },
        moudleName: 'mainUser',
        attributeList: [],
        bomLevelList: []
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },

    mounted() {
      this.getAttributeList('inventory_type');
      this.getBomLevel('bom_query_level');
    },

    methods: {
      /* 搜索 */
      search() {
        this.$emit('search', this.where);
      },
      /*  重置 */
      reset() {
        this.where = { ...this.defaultWhere };
        this.search();
      },

      uploadFile() {
        this.$refs.importDialogRef.open();
      },

      success() {
        this.search();
      },

      async getBomLevel(code) {
        let { data: res } = await getByCode(code);

        this.bomLevelList = res.map((item) => {
          let values = Object.keys(item);
          return {
            value: values[0],
            label: item[values[0]]
          };
        });

        console.log(this.bomLevelList, 'bomLevelList');
      },

      async getAttributeList(code) {
        let res = await getByCode(code);

        // this.attributeList = res.data.map((item) => {
        //   let values = Object.keys(item);
        //   return {
        //     value: values[0],
        //     label: item[values[0]]
        //   };
        // });

        let list = res.data.map((item) => {
          let key = Object.keys(item)[0];
          return { value: Number(key), label: item[key] };
        });
        this.attributeList = list;
      }
    }
  };
</script>
