<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="serviceType">
        <el-select v-model="queryParams.serviceType" placeholder="请选择服务类型" clearable>
          <el-option
            v-for="dict in dict.type.convenience_service"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约热线" prop="bookPhone">
        <el-input
          v-model="queryParams.bookPhone"
          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="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:converienceService:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:converienceService:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="converienceServiceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="服务类型" align="center" prop="serviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.convenience_service" :value="scope.row.serviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="预约热线" align="center" prop="bookPhone" />
      <el-table-column label="轮播图" align="center" prop="serviceBanner" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.serviceBanner" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <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="['business:converienceService:edit']"
          >修改</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" :rules="rules" label-width="80px">
        <el-form-item label="服务类型" prop="serviceType">
          <el-select v-model="form.serviceType" placeholder="请选择服务类型" :disabled="true">
            <el-option
              v-for="dict in dict.type.convenience_service"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="预约热线" prop="bookPhone">
          <el-input v-model="form.bookPhone" placeholder="请输入预约热线" />
        </el-form-item>
        <el-form-item label="轮播图" prop="serviceBanner">
          <image-upload v-model="form.serviceBanner" :limit="5"/>
          <!-- 调试信息：显示当前轮播图数据 -->
          <div style="margin-top: 10px; font-size: 12px; color: #999;">
            <div>当前轮播图数量: {{ getBannerCount() }}</div>
            <div v-if="form.serviceBanner">数据预览: {{ form.serviceBanner }}</div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listConverienceService, getConverienceService, updateConverienceService } from "@/api/business/converienceService";

export default {
  name: "ConverienceService",
  dicts: ['convenience_service'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 便民服务表格数据
      converienceServiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceType: undefined,
        description: undefined,
        bookPhone: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        description: [
          { required: true, message: "描述不能为空", trigger: "blur" }
        ],
        bookPhone: [
          { required: true, message: "预约热线不能为空", trigger: "blur" }
        ],
        serviceBanner: [
          { 
            required: true, 
            message: "轮播图不能为空", 
            trigger: "blur",
            validator: (rule, value, callback) => {
              if (!value || (typeof value === 'string' && value.trim() === '') || (Array.isArray(value) && value.length === 0)) {
                callback(new Error('轮播图不能为空'));
              } else {
                callback();
              }
            }
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询便民服务列表 */
    getList() {
      this.loading = true;
      listConverienceService(this.queryParams).then(response => {
        this.converienceServiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        serviceType: undefined,
        description: undefined,
        bookPhone: undefined,
        serviceBanner: undefined
      };
      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
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getConverienceService(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改便民服务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          updateConverienceService(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/converienceService/export', {
        ...this.queryParams
      }, `converienceService_${new Date().getTime()}.xlsx`)
    },
    // 获取轮播图数量
    getBannerCount() {
      if (this.form.serviceBanner) {
        if (Array.isArray(this.form.serviceBanner)) {
          return this.form.serviceBanner.length;
        } else if (typeof this.form.serviceBanner === 'string') {
          return 1; // 如果是一个字符串，则认为只有一个
        }
      }
      return 0;
    }
  }
};
</script>
