<!-- 搜索表单 -->
<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: 5, md: 12 } : { span: 5 }">
        <el-form-item label="计划编码:">
          <el-input clearable v-model="where.code" placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 5, md: 12 } : { span: 5 }">
        <el-form-item label="物料编码:">
          <el-input
            clearable
            v-model="where.materialCode"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 5, md: 12 } : { span: 5 }">
        <el-form-item label="牌号:" label-width="80px">
          <el-input clearable v-model="where.brandNo" placeholder="请输入" />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 9, md: 12 } : { span: 9 }">
        <el-form-item label="要求交付日期:" label-width="100px">
          <el-date-picker
            class="w100"
            v-model="where.deliveryTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 5, md: 12 } : { span: 5 }">
        <el-form-item label="计划状态:" label-width="80px">
          <el-select
            v-model="where.statusList"
            placeholder="请选择"
            class="w100"
          >
            <el-option
              v-for="item in statusOpt[activeName]"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 7, md: 12 } : { span: 7 }">
        <el-form-item label="创建时间:">
          <el-date-picker
            class="w100"
            v-model="where.createTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col
        v-bind="styleResponsive ? { lg: 7, md: 12 } : { span: 7 }"
        v-if="activeName == 'second'"
      >
        <el-form-item label="发布时间:">
          <el-date-picker
            class="w100"
            v-model="where.releaseTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col
        v-bind="
          styleResponsive
            ? {
                lg: activeName == 'second' ? 5 : 12,
                md: activeName == 'second' ? 12 : 24
              }
            : { span: activeName == 'second' ? 5 : 12 }
        "
      >
        <div class="ele-form-actions">
          <el-button
            type="primary"
            icon="el-icon-search"
            class="ele-btn-icon"
            @click="search"
          >
            查询
          </el-button>
          <el-button @click="reset" icon="el-icon-refresh-left" type="primary"
            >重置</el-button
          >
        </div>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
  export default {
    props: ['activeName', 'statusOpt', 'planType'],
    data () {
      // 默认表单数据
      const defaultWhere = {
        materialCode: '',
        brandNo: '',
        statusList: '',
        code: '',
        releaseTime: [],
        deliveryTime: [],
        createTime: []
      };
      return {
        // 表单数据
        where: { ...defaultWhere },
        treeData: []
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive () {
        return this.$store.state.theme.styleResponsive;
      }
    },
    watch: {
      activeName: {
        handler () {
          if (this.activeName) {
            this.where.statusList = this.statusOpt[this.activeName][0].value;
            this.search();
          }
        },
        immediate: true
      }
    },
    created () {},
    methods: {
      /* 搜索 */
      search () {
        const where = { ...this.where };

        for (const key in where) {
          if (Array.isArray(where[key]) && where[key].length) {
            where[`${key}Start`] = where[key][0];
            where[`${key}End`] = where[key][1];
          }
        }
        delete where.releaseTime;
        delete where.deliveryTime;
        delete where.createTime;

        where.statusList = where.statusList.split(',');

        this.$emit('search', where);
      },
      /*  重置 */
      reset () {
        this.where = { ...this.defaultWhere };
        this.where.statusList = this.statusOpt[this.activeName][0].value;
        this.search();
      }
    }
  };
</script>
<style lang="scss" scoped>
  .ele-form-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
  }
</style>
