<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('mendian.名称')" prop="posName">
        <el-input
          v-model="queryParams.posName"
          :placeholder="$t('mendian.请输入名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('mendian.起送价')" prop="posPrice">
        <el-input
          v-model="queryParams.posPrice"
          :placeholder="$t('mendian.请输入起送价')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('mendian.所在地区')" prop="area">
        <el-input
          v-model="queryParams.area"
          :placeholder="$t('mendian.请输入所在地区')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('mendian.下架')" prop="offShelf">
        <el-select
          v-model="queryParams.offShelf"
          :placeholder="$t('mendian.下架')"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_store_off"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('mendian.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('mendian.重置')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['chanting:store:export']"
        >{{$t('mendian.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column :label="$t('mendian.名称')" align="center" prop="posName" />
      <el-table-column :label="$t('mendian.店封面')" align="center" prop="image">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('mendian.起送价')" align="center" prop="posPrice" />
      <el-table-column :label="$t('mendian.所在地区')" align="center" prop="area" />
      <el-table-column :label="$t('mendian.类型')" align="center" prop="type">
        <template slot-scope="scope">
          <div v-if="scope.row.type==0">{{generateTitle('餐饮')}}</div>
          <div v-if="scope.row.type==1">{{generateTitle('商超')}}</div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('mendian.卫生许可')" align="center" prop="hygienePermit" />
	  <el-table-column :label="$t('mendian.所属用户')" align="center" prop="userName" />
    <el-table-column :label="$t('mendian.下架')" align="center" prop="offShelf" >
      <template slot-scope="scope">
        <el-switch
          v-model="scope.row.offShelf"
          active-value="1"
          inactive-value="0"
          @change="handleStatusChange(scope.row)"
        ></el-switch>
      </template>
    </el-table-column>
      <el-table-column :label="$t('mendian.操作')" 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="['chanting:store:edit']"
          >{{$t('mendian.详请')}}</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['chanting:store: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"
    />

    <!-- 添加或修改posstore对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'160px':'160px'">
        <el-form-item :label="$t('mendian.名称')" prop="posName">
          <el-input v-model="form.posName" :disabled="true" :placeholder="$t('mendian.请输入名称')" />
        </el-form-item>
        <el-form-item :label="$t('mendian.店封面')" prop="image">
          <!-- <el-image
                style="width: 100px; height: 100px"
                :src="form.image"
                :fit="fit"></el-image> -->
          <image-preview :src="form.image" width="100px" height="100px"/>
        </el-form-item>
        <el-form-item :label="$t('mendian.起送价')" prop="posPrice">
          <el-input v-model="form.posPrice" :disabled="true"  :placeholder="$t('mendian.请输入起送价')" />
        </el-form-item>
        <el-form-item :label="$t('mendian.所在地区')" prop="area">
          <el-input v-model="form.area" :disabled="true"  :placeholder="$t('mendian.请输入所在地区')" />
        </el-form-item>
        <el-form-item :label="$t('mendian.详细地址')" prop="address">
          <el-input v-model="form.address" type="textarea" :disabled="true"  :placeholder="$t('mendian.请输入内容')" />
        </el-form-item>
        <el-form-item :label="$t('mendian.简介')" prop="briefIntroduction">
          <el-input v-model="form.briefIntroduction" type="textarea" :disabled="true"  :placeholder="$t('mendian.请输入内容')" />
        </el-form-item>
        <el-form-item :label="$t('mendian.类型')" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio
                disabled
                v-for="dict in dict.type.sys_pos_type"
                :key="dict.value"
                :label="parseInt(dict.value)"
              >{{generateTitle(dict.label)}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item :label="$t('mendian.卫生许可')" prop="hygienePermit">
            <el-input v-model="form.hygienePermit" :disabled="true" :placeholder="$t('mendian.请输入卫生许可')" />
          </el-form-item>
          <el-form-item :label="$t('infoussanhuser.附件')" prop="hygieneLicence">
            <image-upload v-model="form.hygieneLicence" :isShowTip="false"/>
            <!-- <image-preview style="margin-right: 20px;" v-for="item in form.BankPhoto" :src="item" width="100px" height="100px"/> -->
          </el-form-item>
          <el-form-item :label="$t('mendian.营业时间')" prop="businessHours">
            <el-input v-model="form.businessHours" :disabled="true" :placeholder="$t('mendian.请输入营业时间')" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="cancel">{{$t('mendian.确定')}}</el-button>
          <el-button @click="cancel">{{$t('mendian.取消')}}</el-button>
        </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStore, getStore, delStore, addStore, updateStore ,changeOffShelf} from "@/api/chanting/store";
import {generateTitle} from '@/utils/i18.js'
export default {
  name: "Store",
  dicts: ['sys_pos_type','sys_store_off'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // posstore表格数据
      storeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        posName: null,
        posPrice: null,
        area: null,
        longitude: null,
        latitude: null,
        type: 1,
        sort: null,
        offShelf:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        posName: [
          { required: true, message: this.$t('mendian.名称不能为空'), trigger: "blur" }
        ],
        image: [
          { required: true, message: this.$t('mendian.店封面不能为空'), trigger: "blur" }
        ],
        area: [
          { required: true, message: this.$t('mendian.所在地区不能为空'), trigger: "blur" }
        ],
        address: [
          { required: true, message: this.$t('mendian.详细地址不能为空'), trigger: "blur" }
        ],
        longitude: [
          { required: true, message: this.$t('mendian.经度不能为空'), trigger: "blur" }
        ],
        latitude: [
          { required: true, message: this.$t('mendian.纬度不能为空'), trigger: "blur" }
        ],
        type: [
          { required: true, message: this.$t('mendian.类型不能为空'), trigger: "change" }
        ],
        hygienePermit: [
          { required: true, message: this.$t('mendian.卫生许可不能为空'), trigger: "blur" }
        ],
        sort: [
          { required: true, message: this.$t('mendian.分类不能为空'), trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    generateTitle,
    /** 查询posstore列表 */
    getList() {
      this.loading = true;
      listStore(this.queryParams).then(response => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 下架修改
    handleStatusChange(row) {
      let text = row.offShelf === "0" ? "上架" : "下架";
      this.$modal.confirm('确认要"' + text + '""' + row.posName + '"门店吗？').then(function() {
        return changeOffShelf(row.id, row.offShelf);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.offShelf = row.offShelf === "0" ? "1" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        posName: null,
        image: null,
        posPrice: null,
        area: null,
        address: null,
        longitude: null,
        latitude: null,
        briefIntroduction: null,
        type: null,
        hygienePermit: null,
        businessHours: null,
        sort: null,
        offShelf:null,
        hygieneLicence: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 = this.$t('mendian.添加门店');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStore(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('mendian.查看门店');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStore(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('mendian.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addStore(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('mendian.新增成功'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('mendian.是否确认删除编号为') + ids + this.$t('mendian.的数据项')).then(function() {
        return delStore(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('mendian.删除成功'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('chanting/store/export', {
        ...this.queryParams
      }, `store_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
