<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.query"
        placeholder="请输入检索词"
        style="width: 60%;"
        class="filter-item"
        @keyup.enter.native="fetchData()"
      />
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="fetchData">
        检索
      </el-button>
      <el-button
        class="filter-item"
        style="margin: 0 10px 20px 0; float: right;"
        type="success"
        icon="el-icon-circle-plus-outline"
        @click="handleCreateOrUpdate('create')"
      >
        新建
      </el-button>
    </div>

    <el-table
      key="0"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @row-click="showDialog"
    >
      <el-table-column type="index" label="序号" align="center" fixed width="60px" />
      <el-table-column label="名称" fixed align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="自定义ID" align="center" fixed width="80px">
        <template slot-scope="{row}">
          <span>{{ row.id2 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="食谱封面图" align="center" width="200px">
        <template slot-scope="{row}">
          <el-image style="width: 180px; height: 100px" :src="row.coverPic" fit="contain" />
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center">
        <template slot-scope="{row}">
          <span>{{ row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="180px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding" fixed="right" width="360">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleCreateOrUpdate('edit', row)">
            更新
          </el-button>
          <el-button type="primary" size="mini" @click="manageSteps(row)">
            管理步骤
          </el-button>
          <el-button type="primary" size="mini" @click="manageFoods(row)">
            管理食物
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row, $index)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="params" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="ID" prop="id">
          <el-input v-model="params.id2" placeholder="请输入自定义ID" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="params.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="params.description" placeholder="请输入描述" type="textarea" :row="2" />
        </el-form-item>
        <el-form-item label="营养素图片" prop="coverPic">
          <single-image :value="params.coverPic" :show-preview="true" @success="updateFile" style="width: 100%" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="营养素计算" :visible.sync="contentDialogVisible">
      <span v-html="recipeContentHtml" />
    </el-dialog>
  </div>
</template>

<script>
import { getRecipes, createRecipe, updateRecipe, removeRecipe, getRecipeReport } from '@/api/recipe'
import Pagination from '@/components/Pagination'
import SingleImage from '@/components/Upload/SingleImage'

export default {
  name: 'Index',
  components: { Pagination, SingleImage },
  data() {
    return {
      listQuery: {
        query: '',
        pageNum: 1,
        pageSize: 20
      },
      listLoading: false,
      list: [],
      total: 0,
      dialogFormVisible: false,
      textMap: { 'create': '新建', 'edit': '更新' },
      dialogStatus: '',
      rules: {},
      params: {},
      recipeContentHtml: '',
      contentDialogVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getRecipes(this.listQuery).then(res => {
        this.list = res.data.list
        this.listLoading = false
      }).catch(res => {
        this.$message.error('获取数据失败')
        this.listLoading = false
      })
    },
    handleCreateOrUpdate(dialogStatus, row) {
      this.params = dialogStatus === 'create' ? {} : Object.assign({}, row)
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = true

    },
    manageSteps(row) {
      this.$router.push({ path: `recipe/${row.id}/step` })
    },
    manageFoods(row) {
      this.$router.push({ path: `recipe/${row.id}/food` })
    },
    handleDelete(row) {
      removeRecipe(row.id).then(res => {
        this.$message.success('提交成功')
        this.fetchData()
      }).catch(res => {
        this.$message.error(res.data.message)
        this.fetchData()
      })
    },
    createData() {
      createRecipe(this.params).then(res => {
        this.$message.success('提交成功')
        this.dialogFormVisible = false
        this.fetchData()
      }).catch(res => {
        this.$message.error(res.data.message)
        this.fetchData()
      })
    },
    updateData() {
      updateRecipe(this.params.id, this.params).then(res => {
        this.$message.success('提交成功')
        this.dialogFormVisible = false
        this.fetchData()
      }).catch(res => {
        this.$message.error(res.data.message)
        this.fetchData()
      })
    },
    updateFile(data) {
      this.$set(this.params, 'coverPic', data.fileUrl)
    },
    showDialog(row) {
      getRecipeReport(row.id).then(res => {
        this.contentDialogVisible = true
        this.recipeContentHtml = res.data
      }).catch(res => {
        this.$message.error(res.data.message)
      })
    }
  }
}
</script>

<style scoped>

</style>
