<template>
  <div class="divBox">
    <div class="selCard">
      <el-form :model="tableFrom" ref="searchForm" size="small" label-width="90px" :inline="true">
        <el-form-item label="状态：" prop="status">
          <el-select
            v-model="tableFrom.status"
            clearable
            placeholder="请选择"
            class="selWidth"
            @change="getList(1)"
          >
            <el-option label="全部" value=""/>
            <el-option label="待审核" value="0"/>
            <el-option label="审核通过" value="1"/>
            <el-option label="审核未通过" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="申请时间：">
          <el-date-picker
            v-model="timeVal"
            placeholder="选择日期"
            value-format="yyyy/MM/dd HH:mm:ss"
            format="yyyy/MM/dd HH:mm:ss"
            type="datetimerange"
            :picker-options="pickerOptions"
            @change="onchangeTime"
            style="width: 280px"
          />
        </el-form-item>
        <el-form-item label="审核时间：">
          <el-date-picker
            v-model="timeVals"
            placeholder="选择日期"
            value-format="yyyy/MM/dd HH:mm:ss"
            format="yyyy/MM/dd HH:mm:ss"
            type="datetimerange"
            :picker-options="pickerOptions"
            @change="onchangeTimes"
            style="width: 280px"
          />
        </el-form-item>
        <el-form-item label="店铺类型：" prop="type_id">
          <el-select
            v-model="tableFrom.type_id"
            clearable
            placeholder="请选择"
            class="selWidth"
            @change="getList(1)"
          >
            <el-option
              v-for="item in storeType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属城市：" prop="city">
          <el-input
            v-model="tableFrom.city"
            @keyup.enter.native="getList(1)"
            placeholder="请输入所属城市"
            class="selWidth"
            clearable
          />
        </el-form-item>
        <el-form-item label="商户关键字：" prop="keyword">
          <el-input
            v-model="tableFrom.keyword"
            @keyup.enter.native="getList(1)"
            placeholder="请输入商户名称/企业名称的关键字"
            class="selWidth"
            clearable
          />
        </el-form-item>
        <el-form-item label="审核人：" prop="audit_name">
          <el-input
            v-model="tableFrom.audit_name"
            @keyup.enter.native="getList(1)"
            placeholder="请输入审核人"
            class="selWidth"
            clearable
          />
        </el-form-item>
        <el-form-item label="备注：" prop="mark">
          <el-input
            v-model="tableFrom.mark"
            @keyup.enter.native="getList(1)"
            placeholder="请输入备注"
            class="selWidth"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getList(1)">搜索</el-button>
          <el-button size="small" @click="searchReset()">重置</el-button> 
        </el-form-item>
      </el-form>
    </div>
    <el-card class="mt14">
      <div class="mb20">
        <el-button size="small" type="primary" class="mt5" @click="exports">导出列表</el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="small"
        highlight-current-row
        class="switchTable"
      >
        <el-table-column prop="mer_intention_id" label="ID" min-width="60" />
        <el-table-column prop="mer_name" label="店铺名称" min-width="150" />
        <el-table-column prop="com.name" label="企业名称" min-width="150" />
        <!-- <el-table-column prop="category_name" label="商户分类" min-width="150" /> -->
        <el-table-column prop="type_name" label="店铺类型" min-width="150" />
        <el-table-column prop="com.contact" label="对接人姓名" min-width="100" />
        <el-table-column prop="com.mobile" label="对接人电话" min-width="100" />
        <el-table-column prop="com.number" label="对接人身份证号" min-width="120" />
        <!-- <el-table-column prop="com.initiator" label="发起人姓名" min-width="100" />
        <el-table-column prop="com.inti_number" label="发起人编号" min-width="100" />
        <el-table-column prop="com.inti_mobile" label="发起人电话" min-width="100" /> -->
        <el-table-column prop="biz.cityInfo" label="所属城市" min-width="100" />
        <el-table-column prop="create_time" label="申请时间" min-width="150" />
        <!-- <el-table-column prop="create_time" label="资质图片" min-width="150">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                v-for="(item, index) in scope.row.images"
                :key="index"
                :src="item"
                class="mr5"
                :preview-src-list="[item]"
              />
            </div>
          </template>
        </el-table-column> -->
        <el-table-column label="状态" min-width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == 1" type="success">通过</el-tag>
            <el-tag v-if="scope.row.status == 0" type="info">未处理</el-tag>
            <el-tag v-if="scope.row.status == 2" type="warning">未通过</el-tag>
            <div v-if="scope.row.status == 2">原因：{{ scope.row.fail_msg }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="audit_name" label="审核人" min-width="80" />
        <el-table-column prop="audit_time" label="审核时间" min-width="150" />
        <el-table-column prop="mark" label="备注" min-width="150" />
        <el-table-column label="操作" min-width="150" fixed="right">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status == 0"
              type="text"
              size="small"
              @click="onchangeIsShow(scope.row.mer_intention_id)"
              >审核</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="onEdit(scope.row.mer_intention_id)"
              >备注</el-button
            >
            <el-button type="text" size="small" @click="onDetails(scope.row.mer_intention_id)">详情</el-button>
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.mer_intention_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

    <!--商户详情-->
    <mer-detail
      ref="merDetail"
      :merId="mer_id"
      :merCateList="merCateList"
      :storeType="storeType"
      @closeDrawer="closeDrawer"
      @changeDrawer="changeDrawer"
      @getList="getList"
      :drawer="drawer"
    ></mer-detail>

  </div>
</template>

<script>

import {
  intentionLstApi,
  auditApi,
  intentionDelte,
  intentionStatusApi,
  getstoreTypeApi,
  getMerCateApi,
  exportIntentionApi
} from "@/api/merchant";
import merDetail from './merApplicationDetail.vue';
import { fromList, statusList } from "@/libs/constants.js";
import { roterPre } from "@/settings";
import timeOptions from '@/utils/timeOptions';
import cityOptions from '@/utils/city';
import createWorkBook from '@/utils/newToExcel.js';
export default {
  name: "MerchantApplication",
  components: { merDetail },
  data() {
    return {
      props: {
        emitPath: false
      },
      cityOptions:cityOptions,
      pickerOptions: timeOptions,
      fromList: fromList,
      statusList: statusList, //筛选状态列表
      roterPre: roterPre,
      isChecked: false,
      listLoading: true,
      merCateList: [],
      storeType: [],
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        audit_name:'',
        audit_time:'',
        date: "",
        status: this.$route.query.status ? this.$route.query.status : "",
        keyword: "",
        mer_intention_id: this.$route.query.id ? this.$route.query.id : "",
        category_id: "",
        type_id: ""
      },
      mer_id: this.$route.query.id ? this.$route.query.id : "",
      autoUpdate: true,
      timeVal: [],
      timeVals: [],
      drawer: false
    };
  },
  watch: {
    mer_id(newName, oldName) {
     this.getList("");
    }
  },
  mounted() {
    this.getMerCategory();
    this.getStoreType();
    this.getList("");
  },
  methods: {
    handleCascader(){
      this.tableFrom.city = this.$refs["cascader"]?.getCheckedNodes()[0]?.label
      this.getList(1)
    },
    async exports() {
      let excelData = JSON.parse(JSON.stringify(this.tableFrom)), data = []
      excelData.page = 1
      excelData.limit = 100
      let pageCount = 1
      let lebData = {};
      for (let i = 0; i < pageCount; i++) {
        lebData = await this.downData(excelData)
        pageCount = Math.ceil(lebData.count/excelData.limit)
        if (lebData.export.length) {
          data = data.concat(lebData.export)
          excelData.page++
        }
      }
      createWorkBook(lebData.header, lebData.title, data, lebData.foot,lebData.filename);
      return
    },
    /**订单列表 */
    downData(excelData) {
      return new Promise((resolve, reject) => {
        exportIntentionApi(excelData).then((res) => {
          return resolve(res.data)
        })
      })
    },
    /**重置 */
    searchReset(){
      this.timeVal = []
      this.timeVals = []
      this.tableFrom.date = ""
      this.tableFrom.audit_time = ""
      this.$refs.searchForm.resetFields()
      this.getList(1)
    },
    statusChange(tab) {
      this.tableFrom.status = tab;
      this.tableFrom.page = 1;
      this.getList("");
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = this.timeVal ? this.timeVal.join("-") : "";
      this.tableFrom.page = 1;
      this.getList("");
    },
    // 具体日期
    onchangeTimes(e) {
      this.timeVals = e;
      this.tableFrom.audit_time = this.timeVals ? this.timeVals.join("-") : "";
      this.tableFrom.page = 1;
      this.getList("");
    },
    // 商户分类；
    getMerCategory() {
      getMerCateApi().then(res => {
          this.merCateList = res.data
      }).catch(res => {
          this.$message.error(res.message)
      })
    },
    /**获取店铺类型 */
    getStoreType(){
      getstoreTypeApi().then(res => {
          this.storeType = res.data
      }).catch(res => {
          this.$message.error(res.message)
      })
    },
    onDetails(id){
      this.mer_id = id;
      this.$refs.merDetail.isEdit = false;
      this.$refs.merDetail.getInfo(id);
      this.drawer = true;
    },
    changeDrawer(v) {
      this.drawer = v;
    },
    closeDrawer() {
      this.drawer = false;
    },
    // 列表
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      intentionLstApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.data.list;
          this.tableData.total = res.data.count;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList("");
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList(1);
    },
    // 修改状态
    onchangeIsShow(id) {
      this.$modalForm(intentionStatusApi(id)).then(() => this.getList(""));
    },
    // 添加

    // 编辑
    onEdit(id) {
      this.$modalForm(auditApi(id)).then(() => this.getList(""));
    },
    // 删除
    handleDelete(id) {
      this.$deleteSure().then(() => {
        intentionDelte(id)
          .then(({ message }) => {
            this.$message.success(message);
            this.getList("");
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep table .el-image {
  display: inline-block !important;
}
@import '@/styles/form.scss';
</style>
