<template>
  <div class="app-container">
    <!-- 检索区 -->
    <el-card shadow="never" class="search-card">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="服务名称" prop="serviceName">
          <el-input v-model="queryParams.serviceName" placeholder="请输入服务名称" clearable style="width: 200px" @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-card>

    <!-- 列表区 -->
    <el-card shadow="never" class="table-card">
      <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="['agri:goodsService:add']">添加</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="['agri:goodsService:remove']">批量删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="serviceList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="服务名称" align="center" prop="serviceName" min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="简介" align="center" prop="serviceIntro" min-width="160" :show-overflow-tooltip="true" />
        <el-table-column label="图标" align="center" width="90">
          <template slot-scope="scope">
            <image-preview :src="scope.row.serviceIcon" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="默认显示" align="center" prop="defaultShowFlag" width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.defaultShowFlag === '1'" type="success" size="small">是</el-tag>
            <el-tag v-else type="info" size="small">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="140">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['agri:goodsService:edit']">编辑</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['agri:goodsService: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-card>

    <!-- 添加/编辑弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务名称" maxlength="64" show-word-limit />
        </el-form-item>
        <el-form-item label="简介" prop="serviceIntro">
          <el-input v-model="form.serviceIntro" type="textarea" :rows="3" maxlength="500" show-word-limit placeholder="请输入简介" />
        </el-form-item>
        <el-form-item label="图标" prop="serviceIcon">
          <image-upload v-model="form.serviceIcon" :limit="1" :file-size="5" :file-type="['png', 'jpg', 'jpeg']" />
        </el-form-item>
        <el-form-item label="默认显示" prop="defaultShowFlag">
          <el-radio-group v-model="form.defaultShowFlag">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
          <div class="form-tip">「是」时商家发品默认勾选该项</div>
        </el-form-item>
        <el-form-item label="排序" prop="sortNo">
          <el-input-number v-model="form.sortNo" :min="0" :precision="0" controls-position="right" />
          <span class="form-tip-inline">数值越小越靠前</span>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoodsService, getGoodsService, addGoodsService, updateGoodsService, delGoodsService } from "@/api/agri/goodsService"

export default {
  name: "AgriGoodsService",
  data() {
    return {
      loading: true,
      ids: [],
      multiple: true,
      showSearch: true,
      total: 0,
      serviceList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceName: undefined
      },
      form: {},
      rules: {
        serviceName: [{ required: true, message: "请输入服务名称", trigger: "blur" }],
        serviceIntro: [{ required: true, message: "请输入简介", trigger: "blur" }],
        serviceIcon: [{ required: true, message: "请上传图标", trigger: "change" }],
        defaultShowFlag: [{ required: true, message: "请选择默认显示", trigger: "change" }],
        sortNo: [{ required: true, message: "请输入排序", trigger: "blur" }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询服务列表 */
    getList() {
      this.loading = true
      listGoodsService(this.queryParams).then(response => {
        this.serviceList = response.rows || []
        this.total = response.total || 0
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    /** 重置表单 */
    reset() {
      this.form = {
        serviceId: undefined,
        serviceName: undefined,
        serviceIntro: undefined,
        serviceIcon: undefined,
        defaultShowFlag: "1",
        sortNo: 0
      }
      this.resetForm("form")
    },
    cancel() {
      this.open = false
      this.reset()
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serviceId)
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加商品服务"
    },
    handleUpdate(row) {
      this.reset()
      getGoodsService(row.serviceId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "编辑商品服务"
      })
    },
    /** 提交表单 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return
        }
        const submitFn = this.form.serviceId != undefined ? updateGoodsService : addGoodsService
        submitFn(this.form).then(() => {
          this.$modal.msgSuccess(this.form.serviceId != undefined ? "修改成功" : "新增成功")
          this.open = false
          this.getList()
        })
      })
    },
    /** 删除 */
    handleDelete(row) {
      const serviceIds = row.serviceId || this.ids
      this.$modal.confirm("确认删除所选商品服务？已被商品使用的服务无法删除").then(() => {
        return delGoodsService(serviceIds)
      }).then(() => {
        this.$modal.msgSuccess("删除成功")
        this.getList()
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>
.search-card {
  margin-bottom: 10px;
}
.form-tip {
  color: #909399;
  font-size: 12px;
  margin-top: 4px;
}
.form-tip-inline {
  color: #909399;
  font-size: 12px;
  margin-left: 8px;
}
</style>
