<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="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">
      <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>
              {{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)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleCancel(scope.row)"
            v-if="canCancel(scope.row)"
          >取消申请</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="1000px"
      destroy-on-close
    >
      <mch-store-apply-detail :id="row.id" @click-link="showDetail = false"/>
    </el-drawer>

  </div>
</template>

<script>
import { getStoreApply, delStoreApply, addStoreApply, updateStoreApply } from "@/api/ss/storeApply";
import { $showColumns, $view } 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'
import MchStoreApplyDetail from '@/views/mch/storeApply/detail.vue'
import { mchCancelStoreApply, mchListStoreApply } from '@/api/mch/storeApply'
import { StoreApplyStatus } from '@/utils/constants'

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

export default {
  name: "MchStoreApply",
  mixins: [$showColumns, $view],
  components: { MchStoreApplyDetail, StoreApplyDetail,  UserLink, StoreLink },
  dicts: ['store_apply_type', 'store_apply_status'],
  props: {
    query: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  computed: {
    canCancel() {
      return (row) => {
        return row.status === StoreApplyStatus.WAIT_AUDIT;
      }
    }
  },
  data() {
    return {
      showDetail: false,
      row: {},
      // 字段列表
      columns: [
        {key: 'id', visible: true, label: '申请编号', 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: '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: 20,
        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.queryParams = {
      ...this.queryParams,
      ...this.$route.query,
      ...this.query
    }
    this.getList();
  },
  methods: {
    handleCancel(row) {
      this.$confirm('确定要取消申请吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        mchCancelStoreApply(row.id).then(res => {
          if (res.code === 200) {
            this.$message.success("取消成功");
            this.getList();
          }
        })
      })
    },
    /** 当排序按钮被点击时触发 **/
    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;
      mchListStoreApply(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>
