<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('business.userManager.userInfo.account')" prop="account" label-width="120">
        <el-input v-model="queryParams.account" :placeholder="$t('business.userManager.userInfo.accountPlaceholder')" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('business.userManager.userInfo.idCard')" prop="idCard" label-width="auto">
        <el-input v-model="queryParams.idCard" :placeholder="$t('business.userManager.userInfo.idCardPlaceholder')" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('business.userManager.userInfo.phone')" prop="phone" label-width="auto">
        <el-input v-model="queryParams.phone" :placeholder="$t('business.userManager.userInfo.phonePlaceholder')" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('common.button.search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.button.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:userInfo:edit']">{{ $t('common.button.edit') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:userInfo:export']">{{ $t('common.button.export') }}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('business.userManager.userInfo.account')" align="center" prop="account" />
      <el-table-column :label="$t('business.userManager.userInfo.name')" align="center" prop="name" />
      <el-table-column :label="$t('business.userManager.userInfo.gender')" align="center" prop="gender" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender == '1'">{{ $t('common.dict.male') }}</el-tag>
          <el-tag v-if="scope.row.gender == '0'">{{ $t('common.dict.female') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('business.userManager.userInfo.idCard')" align="center" prop="idCard" />
      <el-table-column :label="$t('business.userManager.userInfo.phone')" align="center" prop="phone" />
      <el-table-column :label="$t('business.userManager.userRegister.contactAddress')" align="center" prop="contactAddress" />
      <el-table-column :label="$t('business.userManager.userInfo.email')" align="center" prop="email" />
      <el-table-column :label="$t('business.userManager.userInfo.addr')" align="center" prop="addr" />
      <el-table-column :label="$t('business.userManager.userInfo.status')" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '0'">{{ $t('business.userManager.userInfo.statusNormal') }}</el-tag>
          <el-tag type="danger" v-if="scope.row.status == '1'">{{ $t('business.userManager.userInfo.statusDisabled') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.tableMsg.operation')" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:userInfo:edit']">{{ $t('common.button.edit') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item :label="$t('business.userManager.userInfo.email')" prop="email">
          <el-input v-model="form.email" :placeholder="$t('business.userManager.userInfo.emailPlaceholder')"/>
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.name')" prop="name">
          <el-input v-model="form.name" :placeholder="$t('business.userManager.userInfo.namePlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.gender')" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="1">{{ $t('common.dict.male') }}</el-radio>
            <el-radio label="0">{{ $t('common.dict.female') }}</el-radio>
          </el-radio-group>
<!--          <el-input v-model="form.gender" :placeholder="$t('business.userManager.userInfo.genderPlaceholder')" />-->
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.idCard')" prop="idCard">
          <el-input v-model="form.idCard" :placeholder="$t('business.userManager.userInfo.idCardPlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.phone')" prop="phone">
          <el-input v-model="form.phone" :placeholder="$t('business.userManager.userInfo.phonePlaceholder')" />
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.addr')" prop="addr">
          <el-cascader
            :v-model="form.addr"
            :options="options_addr"
            @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.detailAddr')" prop="addrDetail">
          <el-input v-model="form.addrDetail" :placeholder="$t('business.userManager.userInfo.addrPlaceholder')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">{{ $t('common.button.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.button.cancel') }}</el-button>
      </div>
    </el-dialog>

    <!-- 用户信息详情 -->
    <el-dialog :title="title" :visible.sync="detailOpen" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="auto">
        <el-form-item :label="$t('business.userManager.userInfo.detailUserId')">
          {{form.userCode}}
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.detailName')">
          {{form.name}}
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.detailGender')">
          {{form.gender}}
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.detailIdCard')">
          {{form.idCard}}
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.detailPhone')">
          {{form.phone}}
        </el-form-item>
        <el-form-item :label="$t('business.agent.userInfoAgent.contactAddress')">
          {{form.contactAddress}}
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.detailAge')">
          {{form.age}}
        </el-form-item>
        <el-form-item :label="$t('business.userManager.userInfo.detailAddr')">
          {{form.addr}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t('common.button.close') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listUserInfo,
    getUserInfo,
    delUserInfo,
    addUserInfo,
    updateUserInfo,
    getAddress
  } from "@/api/business/userInfo";

  export default {
    name: "UserInfo",
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户信息表格数据
        userInfoList: [],
        //地址选择列表
        options_addr:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否显示详情弹出层
        detailOpen: false,
        // 用户状态时间范围
        daterangeCreateTime: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userId: undefined,
          userCode: undefined,
          account: undefined,
          name: undefined,
          gender: undefined,
          idCard: undefined,
          phone: undefined,
          age: undefined,
          addr: undefined,
          status: undefined,
          delFlag: undefined,
          createTime: undefined,
          language: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            { required: true, message: this.$t('business.userManager.userInfo.nameRequired'), trigger: "blur" }
          ],
          gender: [
            { required: true, message: this.$t('business.userManager.userInfo.genderRequired'), trigger: "blur" }
          ],
          idCard: [
            { required: true,pattern: /^\d+$/, message: this.$t('business.userManager.userInfo.idCardRequired'), trigger: "blur" }
          ],
          phone: [
            { required: true,pattern: /^\d+$/, message: this.$t('business.userManager.userInfo.phoneRequired') , trigger: "blur" }
          ],
          email: [
            { required: true, pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,}(?:\.[a-zA-Z]{2,})?$/, message: this.$t('business.userManager.userInfo.emailRequired'), trigger: "blur" }
          ],
          addr: [
            { required: true, message: this.$t('business.userManager.userInfo.addrRequired'), trigger: "change" }
          ],
          contactAddress: [
            { required: true, message: this.$t('business.agent.userInfoAgent.rule.contactAddressRequired'), trigger: "change" }
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getAddress();
    },
    methods: {
      /** 查询用户信息列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeCreateTime && '' !== this.daterangeCreateTime) {
          this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
          this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
        }
        const languageMap = {
          'zh': 1,
          'en': 2,
          'la': 3
        };
        this.queryParams.language = languageMap[this.$i18n.locale] || null;
        listUserInfo(this.queryParams).then(response => {
          this.userInfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.detailOpen = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          userId: undefined,
          userCode: undefined,
          name: undefined,
          gender: undefined,
          idCard: undefined,
          phone: undefined,
          age: undefined,
          addr: undefined,
          status: undefined,
          delFlag: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeCreateTime = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = this.$t('business.userManager.userInfo.addTitle');
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getUserInfo(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = this.$t('business.userManager.userInfo.editTitle');
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.addr = this.form.addr + "," + this.form.addrDetail;
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateUserInfo(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('common.promptMsg.editSuccess'));
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addUserInfo(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('common.promptMsg.addSuccess'));
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
       this.$modal.confirm(this.$t('common.button.deleteConfirm')).then(() => {
          this.loading = true;
          return delUserInfo(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess(this.$t('common.button.deleteSuccess'));
        }).catch(() => {}).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/userInfo/export', {
          ...this.queryParams
        }, `userInfo_${new Date().getTime()}.xlsx`)
      },
      /** 获取地址列表 */
      getAddress() {
        this.loading = true;
        getAddress(this.$i18n.locale).then(response => {
          this.options_addr = response.data;
        });
      },
      /** 地址联动事件 **/
      handleChange(value) {
        this.form.addr = value;
      }
    }
  };
</script>
