<!-- 搜索表单 -->
<template>
  <el-form
    label-width="77px"
    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 clearable v-model="where.username" 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.mobile" placeholder="请输入" />
        </el-form-item>
      </el-col>
      <!--
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="机构">
          <el-select value="" v-model="where.teamId" clearable>
            <el-option
              v-for="item in teamList"
              :value="item.id"
              :key="item.id"
              :label="item.title"
            />
          </el-select>
        </el-form-item>
      </el-col>
      -->
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="城市:">
          <regions-select
            v-model="where.citys"
            type="provinceCity"
            placeholder="请选择省市"
            class="ele-fluid"
          />
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="机构">
          <el-select value="" v-model="where.orgName" clearable filterable>
            <el-option
              v-for="(item, index) in orgNameList"
              :value="item"
              :key="index"
              :label="item"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="中心">
          <el-select v-model="where.centerNumber" clearable>
            <el-option
              v-for="(item, index) in centerList"
              :value="index"
              :key="index"
              :label="item"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <el-form-item label="角色">
          <el-select v-model="where.groupType" clearable>
            <el-option
              v-for="(item, index) in groupList"
              :value="index"
              :key="index"
              :label="item"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :md="6" :sm="12">
        <el-form-item label="状态">
          <el-select v-model="where.status" clearable>
            <el-option value="1" label="正常" />
            <el-option value="0" label="禁用" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
        <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">重置</el-button>
        </div>
      </el-col>
    </el-row>
  </el-form>
</template>

<script>
  import { listOrganizations } from '@/api/organization/manage';
  import {
    groupTypeUsers,
    centerUsers,
    orgNameList
  } from '@/api/organization/user';
  import RegionsSelect from '@/components/RegionsSelect/index.vue';

  export default {
    components: { RegionsSelect },
    data() {
      // 默认表单数据
      const defaultWhere = {
        username: '',
        mobile: '',
        orgId: '',
        teamId: '',
        groupType: '',
        centerNumber: '',
        citys: [],
        orgName: ''
      };
      return {
        // 表单数据
        where: { ...defaultWhere },
        teamList: [],
        groupList: [],
        centerList: [],
        orgNameList: []
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {
      this.initData();
      this.loadOrgNameList();
    },
    methods: {
      async initData() {
        try {
          // await this.loadOrg();
          // await this.loadOrgTeam();
          await Promise.all([
            groupTypeUsers()
              .then((data) => {
                this.groupList = data;
              })
              .catch((e) => {
                this.$message.error(e.message);
              }),
            centerUsers()
              .then((data) => {
                this.centerList = data;
              })
              .catch((e) => {
                this.$message.error(e.message);
              })
          ]);
        } catch (e) {
          this.$message.error(e.message);
        }
      },
      loadOrgNameList() {
        orgNameList()
          .then((data) => {
            this.orgNameList = data;
          })
          .catch((e) => {
            this.$message.error(e.message);
          });
      },
      loadOrg() {
        return new Promise((resolve, reject) => {
          listOrganizations({ pid: 0 })
            .then((data) => {
              this.where.orgId = data[0]?.id || '';
              console.log('Set orgId:', this.where.orgId);
              resolve();
            })
            .catch((e) => {
              this.$message.error(e.message);
              reject(e);
            });
        });
      },
      loadOrgTeam() {
        return new Promise((resolve, reject) => {
          this.teamList = [];
          this.where.teamId = '';

          if (!this.where.orgId) {
            return resolve();
          }

          listOrganizations({ pid: this.where.orgId })
            .then((data) => {
              this.teamList = data;
              resolve();
            })
            .catch((e) => {
              this.$message.error(e.message);
              reject(e);
            });
        });
      },
      /* 搜索 */
      search() {
        this.$emit('search', this.where);
      },
      /*  重置 */
      reset() {
        this.where = { ...this.defaultWhere };
        this.search();
      }
    }
  };
</script>
