<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商铺名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="老板名称" prop="bossName">
        <el-input v-model="queryParams.bossName" placeholder="请输入老板名称" clearable size="small" style="width: 150px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="老板电话" prop="bossPhone">
        <el-input v-model="queryParams.bossPhone" placeholder="请输入老板电话" clearable size="small" style="width: 150px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditFlag">
        <el-select v-model="queryParams.auditFlag" placeholder="请选择审核状态" style="width: 100px" clearable size="small">
          <el-option v-for="dict in auditFlagOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"><el-button type="success" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['management:shop:edit']">修改</el-button></el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['management:shop:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商铺名称" align="center" prop="name" />
      <el-table-column label="老板名称" align="center" prop="bossName" />
      <el-table-column label="老板电话" align="center" prop="bossPhone" />
      <el-table-column label="地址" align="center" prop="addres" />
      <el-table-column label="审核状态" align="center" prop="auditFlag" :formatter="auditFlagFormat" />
      <el-table-column label="申请时间" align="center" prop="createTime" width="180" />
      <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-edit" @click="handleUpdate(scope.row)" v-hasPermi="['management:shop:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['management:shop: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="70%" :close-on-click-modal="false" append-to-body>
      <el-steps :active="parseInt(form.auditFlag )+2" simple style="margin-bottom: 20px;">
        <el-step title="待审核" icon="el-icon-edit"></el-step>
        <el-step title="审核资料" icon="el-icon-s-check"></el-step>
        <el-step title="审核通过" icon="el-icon-success" v-if="form.auditFlag != 2"></el-step>
        <el-step title="审核不通过" icon="el-icon-error" v-if="form.auditFlag == 2" style="color: red;"></el-step>
      </el-steps>
      <el-alert v-if="form.auditFlag == 2" title="审核不通过" type="error" :description="form.msg" show-icon effect="dark" style="margin-bottom: 20px;"></el-alert>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商铺名称" prop="name"><el-input v-model="form.name" placeholder="请输入商铺名称" /></el-form-item>
        <el-form-item label="老板名称" prop="bossName"><el-input v-model="form.bossName" placeholder="请输入老板名称" /></el-form-item>
        <el-form-item label="老板电话" prop="bossPhone"><el-input v-model="form.bossPhone" placeholder="请输入老板电话" /></el-form-item>
        <el-form-item label="营业执照"><el-image v-if="form.pic" class="lsw_square" :fit="'contain'" :src="baseUrl + form.pic.split(',')[0]" :preview-src-list="[baseUrl + form.pic.split(',')[0]]"></el-image></el-form-item>
        <el-form-item label="商铺图片"><imageUpload v-model="form.showPictures" :limit="5" /></el-form-item>
        <el-form-item label="地址" prop="addres"><el-input v-model="form.addres" type="textarea" placeholder="请输入内容" /></el-form-item>
        <el-form-item label="简介"><el-input type="textarea" rows="6" v-model="form.briefContent"></el-input></el-form-item>
        <el-form-item label="审核状态" prop="auditFlag">
          <el-radio-group v-model="form.auditFlag">
            <el-radio-button v-for="dict in auditFlagOptions" :key="dict.dictValue" :label="dict.dictValue">{{ dict.dictLabel }}</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="驳回原因" v-if="form.auditFlag == 2">
          <el-input type="textarea" rows="3" v-model="form.msg" placeholder="请输入审核不通过原因"></el-input>
        </el-form-item>
        <el-form-item label="审核时间" prop="auditTime" v-if="form.auditFlag != 0"><el-input v-model="form.auditTime" disabled 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 { listShop, getShop, delShop, addShop, updateShop, exportShop } from '@/api/management/shop';

export default {
  name: 'Shop',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商铺管理表格数据
      shopList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 审核状态字典
      auditFlagOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        bossName: null,
        bossPhone: null,
        enableFlag: null,
        auditFlag: null,
        orderByColumn: 'createTime', //排序字段
        isAsc: 'desc' //排序方式
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: '商铺名称不能为空' }],
        bossName: [{ required: true, message: '老板名称不能为空' }],
        bossPhone: [{ required: true, message: '老板电话不能为空' }],
        auditFlag: [{ required: true, message: '审核状态不能为空' }]
      },
      startTime: null,
      endTime: null,
      activeName: 'first',
      baseUrl: process.env.VUE_APP_BASE_API
    };
  },
  created() {
    this.getList();
    this.getDicts('shop_audit').then(response => {
      this.auditFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询商铺管理列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 状态字典翻译
    enableFlagFormat(row, column) {
      return this.selectDictLabel(this.enableFlagOptions, row.enableFlag);
    },
    // 审核状态字典翻译
    auditFlagFormat(row, column) {
      return this.selectDictLabel(this.auditFlagOptions, row.auditFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.startTime = '';
      this.endTime = '';
      this.form = {
        shopId: null,
        name: null,
        addres: null,
        lat: null,
        lng: null,
        bossName: null,
        bossPhone: null,
        showPictures: null,
        labelText: null,
        businessHours: null,
        briefContent: null,
        content: null,
        enableFlag: '0',
        auditFlag: '0',
        auditAt: null,
        auditTime: null,
        createTime: 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.shopId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.startTime = '08:00';
      this.endTime = '23:00';
      this.open = true;
      this.title = '添加商铺管理';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const shopId = row.shopId || this.ids;
      getShop(shopId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改商铺管理';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (this.startTime && this.endTime) {
          this.form.businessHours = this.startTime + '~' + this.endTime;
        }
        if (valid) {
          if (this.form.shopId != null) {
            updateShop(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const shopIds = row.shopId || this.ids;
      this.$confirm('是否确认删除商铺管理编号为"' + shopIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delShop(shopIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有商铺管理数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLoading = true;
          return exportShop(queryParams);
        })
        .then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    }
  }
};
</script>
<style lang="scss">
.lsw_square {
  img{
    border-radius: 5px;
    width: 146px;
  }
}
</style>
