<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('mendianfood.门店id')" prop="mdid">
        <el-input
          v-model="queryParams.mdid"
          :placeholder="$t('mendianfood.请输入门店id')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('mendianfood.商品名')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('mendianfood.请输入商品名')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('mendianfood.商品价格')" prop="price">
        <el-input
          v-model="queryParams.price"
          :placeholder="$t('mendianfood.请输入商品价格')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('systemuser.审核状态')" prop="toExamine">
        <el-select
          v-model="queryParams.toExamine"
          :placeholder="$t('systemuser.审核状态')"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_user_audit"
            :key="dict.value"
            :label="generateTitle(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('mendianfood.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('mendianfood.重置')}}</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:food:export']"
        >{{$t('mendianfood.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="foodList" @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('mendianfood.门店id')" align="center" prop="mdid" />
      <el-table-column :label="$t('mendianfood.商品名')" align="center" prop="name" />
      <el-table-column :label="$t('mendianfood.商品配图')" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('mendianfood.商品价格')" align="center" prop="price" />
      <el-table-column :label="$t('mendianfood.商品介绍')" align="center" prop="introduce" />
     <el-table-column :label="$t('systemuser.审核')" align="center" key="toExamine" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.toExamine"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
      <el-table-column :label="$t('mendianfood.操作')" 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:food:edit']"
          >{{$t('mendianfood.详情')}}</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['chanting:food: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"
    />

    <!-- 添加或修改food对话框 -->
    <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="分类id" prop="flId">
          <el-input v-model="form.flId" placeholder="请输入分类id" />
        </el-form-item>
        <el-form-item label="门店id" prop="mdid">
          <el-input v-model="form.mdid" placeholder="请输入门店id" />
        </el-form-item> -->
        <el-form-item :label="$t('mendianfood.商品名')" prop="name">
          <el-input v-model="form.name" :disabled="true" :placeholder="$t('mendianfood.请输入商品名')" />
        </el-form-item>
        <el-form-item :label="$t('mendianfood.商品配图')" prop="image">
             <image-preview :src="form.image" width="100px" height="100px"/>
        </el-form-item>
        <el-form-item :label="$t('mendianfood.商品价格')" prop="price">
          <el-input v-model="form.price" :disabled="true" :placeholder="$t('mendianfood.请输入商品价格')" />
        </el-form-item>
        <el-form-item :label="$t('mendianfood.商品介绍')" prop="introduce">
          <el-input v-model="form.introduce" :disabled="true" :placeholder="$t('mendianfood.请输入商品介绍')" />
        </el-form-item>
<!--        <el-form-item :label="$t('mendianfood.排序')" prop="sort">
          <el-input v-model="form.sort" :placeholder="$t('mendianfood.请输入排序')" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('mendianfood.确定')}}</el-button>
        <el-button @click="cancel">{{$t('mendianfood.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFood, getFood, delFood, addFood, updateFood ,changerecommend} from "@/api/chanting/food";
import {generateTitle} from '@/utils/i18.js'
export default {
  name: "Food",
  dicts: ['sys_pos_type','sys_user_audit'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // food表格数据
      foodList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flId: null,
        mdid: null,
        name: null,
        image: null,
        price: null,
        introduce: null,
        recommend: null,
        sort: null,
        toExamine: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mdid: [
          { required: true, message: "门店id不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "商品名不能为空", trigger: "blur" }
        ],
        image: [
          { required: true, message: "商品配图不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "商品价格不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    generateTitle,
    /** 查询food列表 */
    getList() {
      this.loading = true;
      listFood(this.queryParams).then(response => {
        this.foodList = response.rows;
        console.log('1111111111',this.foodList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        flId: null,
        mdid: null,
        name: null,
        image: null,
        price: null,
        introduce: null,
        recommend: null,
        sort: null,
        toExamine: 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 = "添加food";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFood(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('mendianfood.详情');
      });
    },
    // 推荐状态修改
    handleStatusChange(row) {
      console.log('===',row)
      let text = row.toExamine === '1' ? this.$t('systemuser.审核上架') : this.$t('systemuser.下架');
      var tisyuy = '';
      if(row.toExamine=='1'){
        tisyuy = this.$t('message.language')=='Ngôn ngữ'?'Xác nhận cần  xét duyệt cho lên kệ  sản phẩm'+row.name+'?':this.$t('mendianfood.确认要')+'"' + text + '""' + row.name + '"'+this.$t('mendianfood.商品吗');
      }else{
        tisyuy = this.$t('message.language')=='Ngôn ngữ'?'Xác nhận cần xuống kệ  sản phẩm'+row.name+'?':this.$t('mendianfood.确认要')+'"' + text + '""' + row.name + '"'+this.$t('mendianfood.商品吗');
      }
      this.$modal.confirm(tisyuy).then(function() {
        var zhuangt = {
          id:row.id,
          toExamine:row.toExamine=='1'?'1':'0'
        }
        return changerecommend(zhuangt);
      }).then(() => {
        this.$modal.msgSuccess(text + this.$t('mendianfood.成功'));
      }).catch(function() {
        row.toExamine = row.toExamine === '0' ? '1' : '0';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFood(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('mendianfood.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addFood(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除food编号为"' + ids + '"的数据项？').then(function() {
        return delFood(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('chanting/food/export', {
        ...this.queryParams
      }, `food_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
