<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.agent.userInfoAgent.agentName')" prop="agentName">
        <el-input v-model="queryParams.agentName"
          :placeholder="$t('business.agent.userInfoAgent.placeholder.agentName')" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item :label="$t('business.agent.userInfoAgent.account')" prop="account">
        <el-input v-model="queryParams.account" :placeholder="$t('business.agent.userInfoAgent.placeholder.account')"
          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="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:userInfoAgent: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="userInfoAgentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('business.agent.userInfoAgent.account')" align="center" prop="account" />
      <el-table-column :label="$t('business.agent.userInfoAgent.agentName')" align="center" prop="agentName" />
      <el-table-column :label="$t('business.agent.userInfoAgent.license')" align="center" prop="license" />
      <el-table-column :label="$t('business.agent.userInfoAgent.licenseOther')" align="center" prop="licenseOther" />
      <el-table-column :label="$t('business.agent.userInfoAgent.legalPerson')" align="center" prop="legalPerson" />
      <el-table-column :label="$t('business.agent.userInfoAgent.operateStatus')" align="center" prop="operateStatus" />
      <el-table-column :label="$t('business.agent.userInfoAgent.contactAddress')" align="center"
        prop="contactAddress" />
      <el-table-column :label="$t('business.agent.userInfoAgent.operateAddr')" align="center" prop="operateAddr" />
      <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:userInfoAgent: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.agent.userInfoAgent.agentName')" prop="agentName">
          <el-input v-model="form.agentName" :placeholder="$t('business.agent.userInfoAgent.placeholder.agentName')" />
        </el-form-item>
        <el-form-item :label="$t('business.agent.userInfoAgent.license')" prop="license">
          <el-input v-model="form.license" :placeholder="$t('business.agent.userInfoAgent.placeholder.license')" />
        </el-form-item>
        <el-form-item :label="$t('business.agent.userInfoAgent.licenseOther')" prop="licenseOther">
          <el-input v-model="form.licenseOther"
            :placeholder="$t('business.agent.userInfoAgent.placeholder.licenseOther')" />
        </el-form-item>
        <el-form-item :label="$t('business.agent.userInfoAgent.legalPerson')" prop="legalPerson">
          <el-input v-model="form.legalPerson"
            :placeholder="$t('business.agent.userInfoAgent.placeholder.legalPerson')" />
        </el-form-item>
        <el-form-item :label="$t('business.agent.userInfoAgent.contactAddress')" prop="contactAddress">
          <el-input v-model="form.contactAddress"
            :placeholder="$t('business.agent.userInfoAgent.placeholder.contactAddress')" />
        </el-form-item>
        <el-form-item :label="$t('business.agent.userInfoAgent.operateAddr')" prop="operateAddr">
          <el-input v-model="form.operateAddr"
            :placeholder="$t('business.agent.userInfoAgent.placeholder.operateAddr')" />
        </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>

  </div>
</template>

<script>
  import {
    listUserInfoAgent,
    getUserInfoAgent,
    delUserInfoAgent,
    addUserInfoAgent,
    updateUserInfoAgent
  } from "@/api/business/userInfoAgent";

  export default {
    name: "UserInfoAgent",
    data() {
      return {
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 代理商信息表格数据
        userInfoAgentList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userId: undefined,
          agentName: undefined,
          license: undefined,
          licenseOther: undefined,
          legalPerson: undefined,
          operateStatus: undefined,
          contactAddress: undefined,
          operateAddr: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          id: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.idRequired'),
            trigger: "blur"
          }],
          /* userId: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.userIdRequired'),
            trigger: "blur"
          }], */
          agentName: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.agentNameRequired'),
            trigger: "blur"
          }],
          license: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.licenseRequired'),
            trigger: "blur"
          }],
          licenseOther: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.licenseOtherRequired'),
            trigger: "blur"
          }],
          legalPerson: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.legalPersonRequired'),
            trigger: "blur"
          }],
          /* operateStatus: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.operateStatusRequired'),
            trigger: "change"
          }], */
          contactAddress: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.contactAddressRequired'),
            trigger: "blur"
          }],
          operateAddr: [{
            required: true,
            message: this.$t('business.agent.userInfoAgent.rule.operateAddrRequired'),
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询代理商信息列表 */
      getList() {
        this.loading = true;
        listUserInfoAgent(this.queryParams).then(response => {
          this.userInfoAgentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          userId: undefined,
          agentName: undefined,
          license: undefined,
          licenseOther: undefined,
          legalPerson: undefined,
          operateStatus: undefined,
          contactAddress: undefined,
          operateAddr: undefined,
          delFlag: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        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.agent.userInfoAgent.addTitle');
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getUserInfoAgent(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = this.$t('business.agent.userInfoAgent.editTitle');
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateUserInfoAgent(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('business.agent.userInfoAgent.editSuccess'));
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addUserInfoAgent(this.form).then(response => {
                this.$modal.msgSuccess(this.$t('business.agent.userInfoAgent.addSuccess'));
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm(this.$t('business.agent.userInfoAgent.deleteConfirm', {
          ids: ids
        })).then(() => {
          this.loading = true;
          return delUserInfoAgent(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess(this.$t('business.agent.userInfoAgent.deleteSuccess'));
        }).catch(() => {}).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('business/userInfoAgent/export', {
          ...this.queryParams
        }, `userInfoAgent_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
