<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="申请日期">
        <el-date-picker
          v-model="searchDate"
          type="daterange"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="left"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
          size="small"
          style="width: 260px"
          :default-time="['00:00:00', '23:59:59']"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入店铺名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="merchantPhone">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务经理" prop="promoter">
        <el-input
          v-model="queryParams.promoter"
          placeholder="请输入业务经理手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          v-model="queryParams.shopState"
          placeholder="请选择"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
          <el-option label="全部" value></el-option>
          <el-option label="申请中" value="001"></el-option>
          <el-option label="已驳回" value="003"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          @click="handleQuery"
          plain
        >搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
        >重置</el-button>

      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="shopList" border stripe>
      <el-table-column label="个体户id" align="center" prop="merchantId" width="120" />
      <el-table-column label="手机号" align="center" prop="user.phonenumber" width="150" />
      <el-table-column
        label="姓名"
        align="center"
        prop="user.userName"
        show-overflow-tooltip
      />

      <el-table-column
        label="身份证号"
        align="center"
        prop="info.legalPersonNo"
        width="250"
      />
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="业务经理"
        align="center"
        prop="promoter"
        width="150"
      />
      <el-table-column label="状态" align="center" prop="shopState">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.shopState == '001'" type="success">
            申请中</el-tag
          >
          <el-tag v-if="scope.row.shopState == '003'" type="info">
            已驳回</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-view"
            @click="queryDetails(scope.row.merchantId)"
            v-hasPermi="['shop:audit:detail']"
          >查看详情
          </el-button>
          <el-button
            type="text"
            icon="el-icon-circle-check"
            @click="handleAudit(scope.row)"
            v-hasPermi="['shop:audit:audit']"
            v-if="scope.row.shopState == '001'"
          >审核
          </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="500px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        label-width="120px"
      >
        <el-form-item label="审核结果">
          <el-radio v-model="form.shopState" label="002">通过</el-radio>
          <el-radio v-model="form.shopState" label="003">不通过</el-radio>
        </el-form-item>
        <el-form-item label="原因" v-if="form.shopState == '003'">
          <el-input
            type="textarea"
            :rows="2"
            v-model="form.examineReason"
            placeholder="请输入不通过的原因"
          />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { auditList, handleAudit } from "@/api/merchant/adminMerchant";

export default {
  name: "adminMerchant",
  data() {
    return {
      merchantType: 1,
      showSearch: true,
      loading: true,
      total: 0,
      shopList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantName: "",
        phonenumber: "",
        userName: "",
        shopState: "",
        promoter: "",
        merchantId: "",
        merchantType: "",
        beginTime: "",
        endTime: ""
      },
      form: {},
      // 弹出层标题
      title: "",
      open: false,

      searchDate: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询待审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.merchantType = this.merchantType;
      if(this.searchDate == null){
        this.searchDate = ''
      }
      const [searchBegin, searchEnd] = this.searchDate;
      let sBegin = searchBegin;
      let sEnd = searchEnd;
      if (typeof searchBegin == "undefined") {
        sBegin = "";
      }
      if (typeof searchEnd == "undefined") {
        sEnd = "";
      }
      this.queryParams.beginTime = sBegin;
      this.queryParams.endTime = sEnd;
      auditList(this.queryParams)
        .then((response) => {
          console.log(response)
          this.shopList = response.rows;

          this.total = parseInt(response.total);
        }).catch(err => {
        console.log(err)
      }).finally(() => {
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        merchantId: null,
        examineReason: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.searchDate = [];
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        merchantName: "",
        phonenumber: "",
        userName: "",
        shopState: "",
        promoter: "",
        merchantId: "",
        merchantType: "",
        beginTime: "",
        endTime: ""
      };
      this.handleQuery();
    },
    /**  查看详情 */
    queryDetails(primaryId) {
      this.$router.push("/adminPersonal/adminPersonal/audit/info?merchantId=" + primaryId);
    },
    parseAddress(row) {
      return row.detail + row.houseNumber;
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.reset();
      this.form.merchantId = row.merchantId;
      this.open = true;
      this.title = "审核";
    },

    /** 提交按钮 */
    submitForm() {
      if (this.form.shopState == null ) {
        this.$message.error("审核结果必选")
        return;
      }
      if (this.form.merchantId != null) {
        handleAudit(this.form).then(res => {
          if (res. code == 200) {
            this.$message.success("审核成功");
          } else {
            this.$message.error(res.msg)
          }
          this.open = false;
          this.getList();
          console.log(res)
        }).catch(err => {
          console.log(err)
          this.$message.error("操作失败")
        })

      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  },
};
</script>

<style>
</style>
