<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="菜单" prop="menuNumber">
        <el-select v-model="queryParams.menuNumber" placeholder="请选择菜单" clearable size="small"
                   @change="changeMenu($event)">
          <el-option
            v-for="item in menus"
            :key="item.menuNumber"
            :label="item.menuName"
            :value="item.menuNumber"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="时段" prop="periodId">-->
      <!--        <el-select v-model="queryParams.periodId" placeholder="请选择时段" :disabled="true" clearable size="small">-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.consumption_period"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="消费模式" prop="patternId">-->
      <!--        <el-select v-model="queryParams.patternId" placeholder="请选择消费模式" :disabled="true" clearable size="small">-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.consumption_pattern"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="消费人员类型" prop="consumerType">-->
      <!--        <el-select v-model="queryParams.consumerType" placeholder="请选择消费人员类型" :disabled="true" multiple clearable size="small">-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.use_type"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <el-form-item label="菜品编号" prop="cuisineNumber">
        <el-input
          v-model="queryParams.cuisineNumber"
          placeholder="请输入菜品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜品名称" prop="cuisineName">
        <el-input
          v-model="queryParams.cuisineName"
          placeholder="请输入菜品名称"
          clearable
          size="small"
          @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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['canteen:register:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['canteen:register:edit']"
        >修改
        </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="['canteen:register:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['canteen:register:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="cuisineAdd"
          v-hasPermi="['canteen:register:imports']"
        >批量新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="registerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="菜单登记id" align="center" prop="registerId" v-if="false"/>
      <el-table-column label="菜单编号" align="center" prop="menuNumber"/>
      <!--      <el-table-column label="时段" align="center" prop="periodId">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.consumption_period" :value="scope.row.periodId"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="消费模式" align="center" prop="patternId">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.consumption_pattern" :value="scope.row.patternId"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="消费人员类型" align="center" prop="consumerType">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.consumption_period" :value="scope.row.consumerType"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="菜品编号" align="center" prop="cuisineNumber"/>
      <el-table-column label="菜品名称" align="center" prop="cuisineName"/>
      <el-table-column label="价格" align="center" prop="price"/>
      <!--      <el-table-column label="积分价格" align="center" prop="integral"/>-->
      <el-table-column label="已下单数量" align="center" prop="stock"/>
      <!--      <el-table-column label="现有库存" align="center" prop="surplus"/>-->
      <el-table-column label="最大份数限制" align="center" prop="maximum"/>
      <el-table-column label="机构代码" align="center" prop="jgid"/>
      <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="['register:register:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['register:register: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-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="菜单编号" prop="menuNumber">
          <el-select v-model="form.menuNumber" placeholder="请选择菜单">
            <el-option
              v-for="item in menus"
              :key="item.menuNumber"
              :label="item.menuName"
              :value="item.menuNumber"
            />
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="时段" prop="periodId" v-if="false">-->
        <!--          <el-select v-model="form.periodId" placeholder="请选择时段">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.consumption_period"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="parseInt(dict.value)"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="消费模式" prop="patternId" v-if="false">-->
        <!--          <el-select v-model="form.patternId" placeholder="请选择消费模式id">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.consumption_pattern"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="parseInt(dict.value)"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="消费人员类型" prop="consumerType" v-if="false">-->
        <!--          <el-select v-model="form.consumerType" placeholder="请选择消费人员类型">-->
        <!--            <el-option-->
        <!--              v-for="dict in dict.type.consumption_period"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="parseInt(dict.value)"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="菜品" prop="cuisineNumber">
          <el-select v-model="form.cuisineNumber" placeholder="请选择菜品" @change="changeCuisine($event)">
            <el-option
              v-for="item in options"
              :key="item.cuisineName"
              :label="item.cuisineName"
              :value="item.cuisineNumber"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜品名称" prop="cuisineName" v-if="false">
          <el-input v-model="form.cuisineName" placeholder="请输入菜品名称"/>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格"/>
        </el-form-item>
        <!--        <el-form-item label="积分价格" prop="integral">-->
        <!--          <el-input v-model="form.integral" placeholder="请输入积分价格"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="初始库存" prop="stock">-->
        <!--          <el-input v-model="form.stock" placeholder="请输入初始库存"/>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="现有库存" prop="surplus">-->
        <!--          <el-input v-model="form.surplus" placeholder="请输入现有库存"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="最大份数限1制" prop="maximum">
          <el-input v-model="form.maximum" placeholder="请输入份数限制"/>
        </el-form-item>
        <el-form-item label="机构代码" prop="jgid">
          <el-input v-model="form.jgid" disabled placeholder="请输入机构代码"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title_list" :visible.sync="open_list" width="1200px" append-to-body :before-close="modalClose">
      <div style="display: flex;align-items: center;justify-content: center;flex-direction:column">
        <div style="width: 90%;">
          <el-form :model="queryParam" ref="queryForm" :inline="true" v-show="showSearch">
            <el-form-item label="菜单" prop="menuNumber">
              <el-select v-model="queryParam.menuNumber" placeholder="请选择菜单" clearable size="small"
                         @change="changeRegisterAdd($event)">
                <el-option
                  v-for="item in menus"
                  :key="item.menuNumber"
                  :label="item.menuName"
                  :value="item.menuNumber"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <div style="width: 90%">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="registerAdd"
              >确定添加
              </el-button>
            </el-col>
          </el-row>
        </div>
        <br>
        <div style="width: 90%">
          <el-table border highlight-current-row :data="dataList" :height="tableHeight"
                    @selection-change="cuisineSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="菜品id" align="center" prop="cuisineId" v-if="false"/>
            <el-table-column label="菜品编号" align="center" prop="cuisineNumber" v-if="false"/>
            <el-table-column label="菜品名称" align="center" prop="cuisineName"/>
            <el-table-column label="菜品图品" align="center" prop="cuisinePictrue">
              <template slot-scope="scope">
                <img style="width:80px;height:80px;border:none;" :src="baseUrl + scope.row.cuisinePictrue">
              </template>
            </el-table-column>
            <el-table-column label="菜品价格" align="center" prop="cuisinePrice">
              <template slot-scope="{row}">
                <el-input style="width: 40%" v-model="row.cuisinePrice"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="启用标志" align="center" prop="qybz" v-if="false">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.qybz"/>
              </template>
            </el-table-column>
            <el-table-column label="是否删除" align="center" prop="isDelete" v-if="false"/>
            <el-table-column label="备注" align="center" prop="remark"/>
          </el-table>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRegister,
  getRegister,
  delRegister,
  addRegister,
  updateRegister,
  exportRegister,
  cuisine,
  menus,
  getData,
  addCuisineforMenu
} from '@/api/canteen/register'
import user from '@/store/modules/user'

export default {
  name: 'Register',
  dicts: ['consumption_period', 'consumption_pattern', 'consumption_period', 'use_type'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜单登记表格数据
      registerList: [],
      // 弹出层标题
      title: '',
      title_list: '批量新增',
      // 是否显示弹出层
      open: false,
      open_list: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menuNumber: null,
        periodId: null,
        patternId: null,
        consumerType: [],
        cuisineNumber: null,
        cuisineName: null,
        jgid: null
      },
      // 表单参数
      form: { maximum: 1 },
      // 表单校验
      rules: {},
      options: [],
      menus: [],
      inputVal: '',
      dataList: [],
      queryParam: {
        currentPage: 1,
        pageSize: 20,
        menuNumber: null,
        periodId: null,
        patternId: null,
        consumerType: null
      },
      queryJgxx: {
        jgid: null
      },
      cuisine_ids: [],
      register_adds: {
        menuNumber: null,
        periodId: null,
        patternId: null,
        consumerType: null,
        cuisines: null
      }
    }
  },
  created() {
    this.getCuisineList()
    this.getMenuList()
    this.getList()
  },
  methods: {
    changeMenu(val) {
      if (val == '') {
        // this.queryParams = {
        //   periodId: null,
        //   patternId: null,
        //   consumerType: null
        // }
      } else {
        this.menus.map(item => {
          if (item.menuNumber == val) {
            this.queryParams = {
              menuNumber: item.menuNumber + ''
            }
          }
        })
      }
      this.getList()
    },
    getMenuList() {
      this.queryJgxx.jgid = user.state.organ
      menus(this.queryJgxx).then((response) => {
        this.menus = response.rows
      })
      // let temp = [];
      // this.$route.query.consumer.split(',').forEach(item =>{
      //   temp.push(parseInt(item));
      // })
      this.queryParams = {
        menuNumber: this.$route.query.number
        // periodId: this.$route.query.time,
        // patternId: this.$route.query.pattern,
        // consumerType: temp
      }
    },
    changeCuisine(val) {
      this.options.map(item => {
        if (item.cuisineNumber == val) {
          this.form.cuisineName = item.cuisineName
          this.form.price = item.cuisinePrice
        }
      })
    },
    getCuisineList() {
      this.queryJgxx.jgid = user.state.organ
      cuisine(this.queryJgxx).then((response) => {
        this.options = response.rows
      })
    },
    /** 查询菜单登记列表 */
    getList() {
      this.queryParams.jgid = user.state.organ
      this.loading = true
      listRegister(this.queryParams).then(response => {
        this.registerList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        registerId: null,
        menuNumber: null,
        // periodId: null,
        // patternId: null,
        // consumerType: null,
        cuisineNumber: null,
        cuisineName: null,
        price: null,
        integral: null,
        stock: null,
        surplus: null,
        jgid: null,
        isDelete: 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.registerId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      // this.form.periodId = parseInt(this.queryParams.periodId);
      // this.form.patternId = parseInt(this.queryParams.patternId);
      // this.form.consumerType = parseInt(this.queryParams.consumerType);
      this.form.menuNumber = this.queryParams.menuNumber
      this.open = true
      this.title = '添加菜单登记'
      this.form.jgid = user.state.organ
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const registerId = row.registerId || this.ids
      getRegister(registerId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改菜单登记'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.registerId != null) {
            updateRegister(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRegister(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const registerIds = row.registerId || this.ids
      this.$modal.confirm('是否确认删除菜单登记编号为"' + registerIds + '"的数据项？').then(function() {
        return delRegister(registerIds)
      }).then(response => {
        this.getList()
        this.$modal.msgSuccess(response.data)
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$modal.confirm('是否确认导出所有菜单登记数据项？').then(() => {
        this.exportLoading = true
        return exportRegister(queryParams)
      }).then(response => {
        this.$download.name(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    modalClose() {
      this.open_list = false
    },
    cuisineAdd() {
      this.queryParam.menuNumber = this.queryParams.menuNumber
      // this.queryParam.periodId = this.queryParams.periodId;
      // this.queryParam.patternId = this.queryParams.patternId;
      // this.queryParam.consumerType = this.queryParams.consumerType;
      this.getData()
      this.open_list = true
    },
    getData() {
      this.queryParam.jgid = user.state.organ
      getData(this.queryParam).then((response) => {
        this.dataList = response.rows
      })
    },
    cuisineSelectionChange(selection) {
      this.cuisine_ids = selection
    },
    registerAdd() {
      this.register_adds = {
        menuNumber: this.queryParam.menuNumber,
        // periodId: this.queryParam.periodId,
        // patternId: this.queryParam.patternId,
        // consumerType: this.queryParam.consumerType,
        cuisines: this.cuisine_ids
      }
      addCuisineforMenu(this.register_adds).then(response => {
        this.modalClose()
        this.getList()
        this.$modal.msgSuccess('添加成功')
      })
    },
    changeRegisterAdd(val) {
      if (val == '') {
        this.queryParam = {
          // periodId: null,
          // patternId: null,
          // consumerType: null
        }
      } else {
        this.menus.map(item => {
          if (item.menuNumber == val) {
            this.queryParam = {
              menuNumber: item.menuNumber + ''
              // periodId: item.periodId + "",
              // patternId: item.patternId + "",
              // consumerType: item.consumerType + ""
            }
          }
        })
      }
      this.getData()
    }
  },
  computed: {
    tableHeight() {
      return document.body.clientHeight - 300
    }
  },
  watch: {
    open_list: function(val) {
      // alert(1)
      // alert(val)
      // this.$emit('update:open_list', val);
    },
    $route: {
      handler() {
        menus().then((response) => {
          this.menus = response.rows
        })
        this.queryParams = {
          menuNumber: this.$route.query.number
        }
        this.getList()
      }

    },
    deep: true
  }
}
</script>
