<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      :class="showSearch ? '' : ' hide'"
      label-width="68px"
    >
      <el-form-item label="企业名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态："
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <br />
      <el-form-item label="审核人" prop="auditBy">
        <el-input
          v-model="queryParams.auditBy"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间">
        <el-date-picker
          v-model="daterangeAuditTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-form>

    <el-table v-loading="loading" :data="customerList">
      <el-table-column label="编号" align="center" prop="id" width="80" />
      <el-table-column label="企业名称" prop="companyName" />
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_audit_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditBy" />
      <el-table-column
        label="审核时间"
        align="center"
        prop="auditTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.auditTime, "{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.status === '0' ? 'el-icon-edit' : 'el-icon-view'"
            @click="handleAudit(scope.row, scope.row.status === '0')"
            v-hasPermi="['system/audit:customer:audit']"
          >
            <span v-if="scope.row.status === '0'">审核</span>
            <span v-else>查看</span>
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system/audit:customer:remove']"
            >删除</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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="企业名称:" prop="companyName">
              {{ form.companyName }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="营业执照:" prop="licenseUrl">
              <image-preview :src="form.licenseUrl" :width="50" :height="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人:" prop="linkMan">
              {{ form.linkMan }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话:" prop="phonenumber">
              {{ form.phonenumber }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="统一信用代码:" prop="unifiedCode">
              {{ form.unifiedCode }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="地址:" prop="address">
              {{ form.address }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请原因:" prop="applyReason">
              {{ form.applyReason }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户账号:" prop="userName">
              {{ form.userName }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="100px"
        v-if="isAudit"
      >
        <el-divider></el-divider>
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请结果:" prop="status">
              <el-radio v-model="form.status" label="1">通过</el-radio>
              <el-radio v-model="form.status" label="2">驳回</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="原因说明:" prop="auditRemark">
              <el-input
                v-model="form.auditRemark"
                placeholder="请输入原因说明"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form label-width="100px" v-else>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="24">
            <el-form-item label="审核结果:" prop="status">
              <dict-tag
                :options="dict.type.sys_audit_status"
                :value="form.status"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="原因说明:" prop="auditRemark">
              {{ form.auditRemark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isAudit" :loading="submitting">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCustomer,
  getCustomer,
  delCustomer,
  auditCustomer,
} from "@/api/system/customer/company";

export default {
  name: "Customer",
  dicts: ["sys_audit_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      submitting: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业用户申请表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核意见时间范围
      daterangeCreateTime: [],
      // 审核意见时间范围
      daterangeAuditTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        linkMan: null,
        phonenumber: null,
        unifiedCode: null,
        address: null,
        userName: null,
        status: '0',
        remark: null,
        createTime: null,
        auditBy: null,
        auditTime: null,
        auditRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          {
            required: true,
            message: "请选择审核结果",
            trigger: "change",
          },
        ],
        auditRemark: [
          { required: true, message: "请填写原因说明", trigger: "change" },
        ],
      },
      isAudit: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询企业用户申请列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0] + ' 00:00:00';
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1] + ' 23:59:59';
      }
      if (null != this.daterangeAuditTime && "" != this.daterangeAuditTime) {
        this.queryParams.params["beginAuditTime"] = this.daterangeAuditTime[0] + ' 00:00:00';
        this.queryParams.params["endAuditTime"] = this.daterangeAuditTime[1] + ' 23:59:59';
      }
      listCustomer(this.queryParams).then((response) => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        auditRemark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeAuditTime = [];
      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 = "添加企业用户申请";
    },
    /** 修改按钮操作 */
    handleAudit(row, isAudit) {
      this.reset();
      const id = row.id || this.ids;
      getCustomer(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "审核企业用户";
        this.isAudit = isAudit;
        if (this.isAudit) {
          this.form.auditRemark = "";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.submitting = true;
          auditCustomer(this.form).then((response) => {
            this.submitting = false;
            this.$modal.msgSuccess("审核成功");
            this.open = false;
            this.getList();
          }).catch(() => {
              this.submitting = false;
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除企业用户申请编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCustomer(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/audit/customer/export",
        {
          ...this.queryParams,
        },
        `customer_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
