<!-- 搜索表单 -->
<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="计划编号:">
          <el-input
            size="mini"
            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
            size="mini"
            clearable
            v-model="where.productCode"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="牌号:" label-width="80px">
          <el-input
            size="mini"
            clearable
            v-model="where.brandNo"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="型号:" label-width="80px">
          <el-input
            size="mini"
            clearable
            v-model="where.model"
            placeholder="请输入"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item size="mini" label="计划类型:" label-width="80px">
          <el-select
            size="mini"
            clearable
            v-model="where.planType"
            placeholder="请选择"
            class="w100"
          >
            <el-option
              v-for="item in planType"
              :label="item.label"
              :value="item.value"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="计划状态:" label-width="80px">
          <el-select
            size="mini"
            v-model="where.status"
            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: 9, md: 12 } : { span: 9 }"
        v-if="activeName == 'second'"
      >
        <el-form-item label-width="150px">
          <template slot="label">
            <el-select
              size="mini"
              v-model="deliveryVal"
              @change="
                () => {
                  where.deliveryTime = [];
                  where.formingTime = [];
                }
              "
            >
              <el-option label="预测交货日期" value="deliveryTime"></el-option>
              <el-option label="实际交货日期" value="formingTime"></el-option>
            </el-select>
          </template>
          <el-date-picker
            class="w100"
            v-model="where[deliveryVal]"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 9, md: 12 } : { span: 9 }">
        <el-form-item label="" label-width="150px">
          <template slot="label">
            <el-select
              size="mini"
              v-model="shapeVal"
              @change="
                () => {
                  where.reqMoldTime = [];
                  where.planFormingTime = [];
                  where.formingTime = [];
                }
              "
            >
              <el-option label="要求完成日期" value="reqMoldTime"></el-option>
              <el-option
                label="预测生产日期"
                value="planFormingTime"
              ></el-option>
              <!-- <el-option label="实际生产日期" value="formingTime"></el-option> -->
            </el-select>
          </template>
          <el-date-picker
            size="mini"
            class="w100"
            v-model="where[shapeVal]"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col
        v-bind="styleResponsive ? { lg: 9, md: 12 } : { span: 9 }"
        v-if="activeName == 'second'"
      >
        <el-form-item label="工单发布时间:" label-width="150px">
          <el-date-picker
            class="w100"
            size="mini"
            v-model="where.releaseTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd HH:mm:ss"
            :default-time="['00:00:00', '23:59:59']"
          >
          </el-date-picker>
        </el-form-item>
      </el-col>

      <el-col v-bind="styleResponsive ? { lg: 6, md: 6 } : { span: 6 }">
        <el-form-item label="组织机构:">
          <auth-selection
            size="mini"
            data-type="Array"
            v-model="where.deptIds"
          ></auth-selection>
        </el-form-item>
      </el-col>

      <el-col v-bind="styleResponsive ? { lg: 6, md: 6 } : { span: 6 }">
        <el-form-item label="加工方式:">
          <el-select
                  size="mini"
                  clearable
                  v-model="where.productType"
                  placeholder="请选择"
                  class="w100"
          >
            <el-option
                    v-for="item in productTypeList"
                    :label="item.name"
                    :value="item.code"
                    :key="item.code"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>

      <el-col v-bind="styleResponsive ? { lg: 6, md: 6 } : { span: 6 }">
        <div class="ele-form-actions">
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="search"
          >
            查询
          </el-button>
          <el-button
            @click="reset"
            icon="el-icon-refresh-left"
            size="mini"
            type="primary"
            >重置</el-button
          >
        </div>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
  export default {
    props: ['activeName', 'statusOpt', 'planType'],
    data() {
      // 默认表单数据
      const defaultWhere = {
        code: '',
        productCode: '',
        status: '',
        model: '',
        brandNo: '',
        planType: '',
        deptIds: [],
        releaseTime: [],
        deliveryTime: [],
        formingTime: [],
        reqMoldTime: [],
        planFormingTime: [],
        executeUserName: ''
      };
      return {
        // 表单数据
        deliveryVal: 'deliveryTime',
        shapeVal: 'reqMoldTime',
        where: { ...defaultWhere },
        treeData: [],
        productTypeList: [
          { code: 2, name: '加工(MBOM)' },
          { code: 3, name: '装配(ABOM)' },
        ],
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    watch: {
      activeName: {
        handler() {
          if (this.activeName) {
            this.where.status = this.statusOpt[this.activeName][0].value;
            this.reset();
          }
        },
        immediate: true
      }
    },
    created() {},
    methods: {
      /* 搜索 */
      search() {
        const where = { ...this.where };
        for (const key in where) {
          if (Array.isArray(where[key]) && key !== 'deptIds') {
            where[`${key}End`] = where[key][1];
            where[key] = where[key][0];
          }
        }

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

        this.$emit('search', where);
      },
      /*  重置 */
      reset() {
        this.where = { ...this.defaultWhere };
        this.where.status = 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>
