<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="审核类型" prop="applyType">
        <el-select v-model="queryParams.applyType" placeholder="请选择审核类型" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.store_apply_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable  @change="handleQuery">
          <el-option
            v-for="dict in dict.type.store_apply_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入提交人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="verifyName">
        <el-input
          v-model="queryParams.verifyName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核意见" prop="verifyRemark">
        <el-input
          v-model="queryParams.verifyRemark"
          placeholder="请输入审核意见"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['ss:storeApply:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['ss:storeApply:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['ss:storeApply:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ss:storeApply:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeApplyList" @selection-change="handleSelectionChange" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
            :key="column.key"
            :label="column.label"
            :prop="column.key"
            :align="column.align"
            :min-width="column.minWidth"
            :sort-orders="orderSorts"
            :sortable="column.sortable"
            :show-overflow-tooltip="column.overflow"
            :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'id'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'applyType'">
              <dict-tag :options="dict.type.store_apply_type" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'status'">
              <dict-tag :options="dict.type.store_apply_status" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'storeName'">
              <store-link :id="d.row.storeId" :name="d.row.storeName"/>
            </template>
            <template v-else-if="column.key === 'userName'">
              <user-link :id="d.row.userId" :name="d.row.userName"/>
            </template>
            <template v-else>
              {{d.row[column.key]}}
            </template>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['ss:storeApply:query']"
          >详情</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['ss:storeApply:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['ss:storeApply: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-drawer
      :with-header="false"
      :visible.sync="showDetail"
      direction="rtl"
      size="1280px"
      destroy-on-close
    >
      <store-apply-detail :id="row.id"/>
    </el-drawer>


  </div>
</template>

<script>
import { listStoreApply, getStoreApply, delStoreApply, addStoreApply, updateStoreApply } from "@/api/ss/storeApply";
import { $showColumns } from '@/utils/mixins';
import StoreLink from '@/components/Business/Store/StoreLink.vue'
import UserLink from '@/components/Business/SmUser/UserLink.vue'
import StoreApplyDetail from '@/views/ss/storeApply/detail.vue'

// 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "StoreApply",
  components: { StoreApplyDetail,  UserLink, StoreLink },
  mixins: [$showColumns],
  dicts: ['store_apply_type', 'store_apply_status'],
  data() {
    return {
      showDetail: false,
      row: {},
      // 字段列表
      columns: [
        {key: 'id', visible: true, label: '申请ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: "120"},
        {key: 'storeName', visible: true, label: '店铺', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'applyType', visible: true, label: '申请类型', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'status', visible: true, label: '申请状态', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'userName', visible: true, label: '提交人', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'createTime', visible: true, label: '提交时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: "180"},
        {key: 'verifyName', visible: true, label: '审核人', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'verifyTime', visible: true, label: '审核时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: "180"},
        {key: 'verifyRemark', visible: true, label: '审核意见', minWidth: null, sortable: true, overflow: true, align: 'center', width: null},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺审核表格数据
      storeApplyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        id: null,
        applyType: null,
        status: null,
        storeId: null,
        userId: null,
        verifyId: null,
        verifyRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        applyType: [
          { required: true, message: "审核类型不能为空", trigger: "change" }
        ],
        storeId: [
          { required: true, message: "店铺id不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "提交审核的时间不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "提交人id不能为空", trigger: "blur" }
        ],
        newData: [
          { required: true, message: "新数据json不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询店铺审核列表 */
    getList() {
      this.loading = true;
      listStoreApply(this.queryParams).then(response => {
        this.storeApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyType: null,
        status: null,
        storeId: null,
        createTime: null,
        userId: null,
        verifyId: null,
        verifyTime: null,
        verifyRemark: null,
        oldData: null,
        newData: null
      };
      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 = "添加店铺审核";
    },
    handleView(row) {
      this.row = row;
      this.showDetail = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStoreApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStoreApply(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStoreApply(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除店铺审核编号为"' + ids + '"的数据项？').then(function() {
        return delStoreApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ss/storeApply/export', {
        ...this.queryParams
      }, `storeApply_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
