<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" >
      <el-form-item label="餐品名称" prop="name">
        <el-input
          v-model.trim="queryParams.name"
          placeholder="请输入餐品名称"
          clearable
          size="small"
          style="width: 140px"
          @keyup.enter.native="handleQuery"

        />
      </el-form-item>
      <el-form-item label="餐别" >
         <el-select v-model="queryParams.type" style="width: 160px" placeholder="请选择餐别" clearable size="small">
          <el-option :label="item.value"  :value="item.value" v-for="(item,index) in mTypeList" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="上下架" >
         <el-select v-model="queryParams.status" style="width: 160px" placeholder="请选择上下架" clearable size="small">
          <el-option :label="item.name"  :value="item.value" v-for="(item,index) in statusList" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="推荐菜品" >
         <el-select v-model="queryParams.isRecommend" style="width: 160px" placeholder="请选择推荐菜品" clearable size="small">
          <el-option :label="item.name"  :value="item.value" v-for="(item,index) in isRecommendList" :key="index" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button plain  size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <br>
      <el-form-item>
        <el-upload
          ref="upload"
          class="upload"
          action="#"
          :limit = 1
          :on-change="handleChange"
          :auto-upload="true"
          :file-list="fileList"
          accept=".xlsx,.xls"
          style="display:inline-block;margin:0  10px 0 0px;vertical-align: top;"
          >
          <el-button
            plain
            size="mini"
            :loading="uploadLoading"
          >上传菜品</el-button>
        </el-upload>
        <el-button
          plain
          size="mini"
          @click="handleExport"
        >菜品模板下载</el-button>
        <!-- <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport2"
        >菜品导出</el-button> -->
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="adminList" >
      <el-table-column label="序号" align="center" prop="" show-overflow-tooltip >
        <template slot-scope="scope">
         {{scope.$index + 1}}
        </template>
      </el-table-column>
      <el-table-column label="餐别" align="center" prop="type" show-overflow-tooltip >
        <template slot-scope="scope">
          <span>{{scope.row.type}}</span>
        </template>
      </el-table-column>
      <el-table-column label="餐品名称" align="center" prop="name" show-overflow-tooltip />
      <el-table-column label="价格（元）" align="center" prop="price" show-overflow-tooltip />
      <el-table-column label="上下架状态" align="center" prop="status" show-overflow-tooltip >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="statusUpdate(scope.row)"
            >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="推荐状态" align="center" prop="isRecommend" show-overflow-tooltip >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isRecommend"
             :active-value="1"
            :inactive-value="0"
            @change="statusUpdate(scope.row)"
            >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope" >
          <el-button
            type="text"
            icon="el-icon-edit"
            size="small"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
           <!-- <el-button
            :disabled="scope.row.status == 1"
            type="text"
            icon="el-icon-edit"
            size="small"
            @click="statusUpdate(scope.row.id,1)"
          >下架</el-button>
           <el-button
            :disabled="scope.row.status == 0"
            type="text"
            icon="el-icon-edit"
            size="small"
            @click="statusUpdate(scope.row.id,0)"
          >上架</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <!-- 对话框 -->
    <el-dialog   :title="title" :visible.sync="open" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false"  >
       <el-form ref="form" :model="form"  label-width="80px" >
        <el-row>
          <el-col :span="12">
            <el-form-item label="餐别：" prop="type">
              <el-select v-model="form.type" style="width: 140px" placeholder="请选择餐别" clearable size="small" >
                <el-option :label="item.value"  :value="item.value" v-for="(item,index) in mTypeList" :key="index" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜品名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入菜品名称"  style="width: 140px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格" prop="price">
              <el-input v-model="form.price" placeholder="请输入价格" oninput="value=value.replace(/^\D*(\d{0,9}(?:\.\d{0,2})?).*$/g,'$1')" max-length="5" style="width: 140px" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推荐菜品" prop="isRecommend">
              <el-select v-model="form.isRecommend" style="width: 140px" placeholder="请选择推荐菜品" clearable size="small">
                <el-option :label="item.name"  :value="item.value" v-for="(item,index) in isRecommendList" :key="index" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!loading">确 定</el-button>
        <el-button :loading="loading" type="primary" @click="submitForm" v-else>提交中...</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { getDishes, updataDishes, batchByFileDishes, dreMenuExport } from "@/api/recharge/cateringManagement";
// import {  listRecharge } from "@/api/system/product";
export default {
  name: "admin",
  data() {
    return {
      // 上传遮罩层
      uploadLoading: false,
      exportLoading:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "菜单详情",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        type: undefined,
        name:undefined,
        startTime:undefined,
        endTime:undefined,
        status:undefined,
        isRecommend:undefined,
      },
      //下拉条件
      mTypeList:[
        {value:"早餐"},
        {value:"午餐"},
        {value:"晚餐"},
        // {value:"宵夜"},
      ],
      //下拉条件
      statusList:[
        {name:"上架",value:0},
        {name:"下架",value:1},
      ],
      //下拉条件
      isRecommendList:[
        {name:"推荐",value:1},
        {name:"不推荐",value:0},
      ],
      //表单
      form:{
        name:undefined,
        type:undefined,
        price:undefined,
      },
      file:undefined,//上传文件
      limit:100,
      num:0,
      fileList:[],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      if(this.dateRange.length>0){
        this.queryParams.startTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      }
      getDishes(this.queryParams).then(response => {
          this.adminList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ids: undefined,
        name: undefined,
        type: undefined,
        status:undefined,
        isRecommend:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /**修改上下架状态 */
    statusUpdate(row){
      updataDishes(row).then(res=>{
        this.msgSuccess("状态变更成功")
        this.getList()
      })

    },
    /** 修改订单信息按钮 */
    handleUpdate(row){
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true
    },
    /**  取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm(row) {
      if(this.form.price>1000){
        this.msgError("最大菜品单价不能超过1000元")
      }else{
        updataDishes(this.form).then(res=>{
          this.open = false
          this.getList()
          this.msgSuccess("修改成功");
        })
      }

    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.startTime = undefined;
      this.queryParams.endTime = undefined;
      this.queryParams.type = undefined;
      this.queryParams.name = undefined;
      this.queryParams.status = undefined;
      this.queryParams.isRecommend = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 上传按钮操作 */
    handleChange(param){
      this.limit = 1
      this.num++
      if(this.num>1){
        this.num = 0
        return false
      }else{
        // this.uploadLoading = true
        this.file = new FormData()// FormData 对象
        this.file.append('file', param.raw)

        batchByFileDishes(this.file).then(response => {
          this.getList()
          this.msgSuccess("上传成功");
          this.uploadLoading = false
          this.$refs.upload.clearFiles()
        }).catch(err =>{
          this.getList()
          this.$message.error('上传失败')
          this.uploadLoading = false
        })
      }
    },
    /** 模板导出 */
    handleExport() {
      var a = document.createElement("a"); //创建一个<a></a>标签
      a.href = "https://smartpark.caih.com/dxfile/3164eb4f-d4f5-4f28-a201-61c602ad2cdb.xls"; // 给a标签的href属性值加上地址，注意，这里是绝对路径，不用加 点.
      a.download = "点餐服务菜品上传模板" ; //设置下载文件文件名，这里加上.xlsx指定文件类型，pdf文件就指定.fpd即可
      a.style.display = "none"; // 障眼法藏起来a标签
      document.body.appendChild(a); // 将a标签追加到文档对象中
      a.click(); // 模拟点击了a标签，会触发a标签的href的读取，浏览器就会自动下载了
      a.remove();
    },
    /** 导出按钮操作 */
    // handleExport2() {
    //   let queryParams = {...this.queryParams};
    //   delete queryParams.current
    //   delete queryParams.size
    //   this.exportLoading = true;
    //   dreMenuExport(queryParams).then(response =>{
    //     this.exportLoading = false;
    //     localStorage.setItem('fileExportId', response.data)
    //     this.$router.push({path:'/recharge/fileExport'})
    //     // setTimeout(()=>{
    //     //   listRecharge({current:1,size:1,id:response.id}).then(res=>{
    //     //     var a = document.createElement("a"); //创建一个<a></a>标签
    //     //     a.href = res.data.records[0].url; // 给a标签的href属性值加上地址，注意，这里是绝对路径，不用加 点.
    //     //     a.download = "配餐记录" + getNowTime(); //设置下载文件文件名，这里加上.xlsx指定文件类型，pdf文件就指定.fpd即可
    //     //     a.style.display = "none"; // 障眼法藏起来a标签
    //     //     document.body.appendChild(a); // 将a标签追加到文档对象中
    //     //     a.click(); // 模拟点击了a标签，会触发a标签的href的读取，浏览器就会自动下载了
    //     //     a.remove();
    //     //     this.exportLoading = false;
    //     //   })
    //     // },1000)
    //   })
    // },
  }
};
</script>
<style scoped>
.row-bg .grid-content{
  margin:10px 0;
}
.grid-content.title{
  font-size: 16px;
  font-weight: 700;
}
</style>
