<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <!-- <label class="el-form-item-label">编号</label>
        <el-input v-model="query.id" clearable placeholder="编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" /> -->
        <label class="el-form-item-label">名称</label>
        <el-input v-model="query.name" clearable placeholder="名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <!-- <label class="el-form-item-label">类型</label>
        <el-select v-model="query.type" clearable placeholder="请选择类型" class="filter-item" style="width: 185px" @keyup.enter.native="crud.toQuery">
          <el-option
            v-for="item in dict.food_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select> -->
        <label class="el-form-item-label">日期</label>
        <date-range-picker v-model="query.createTime" class="date-item" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <!-- <el-form-item label="编号" prop="code">
            <el-input v-model="form.code" style="width: 370px;" />
          </el-form-item> -->
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="类型">
            <el-radio v-for="item in dict.food_type" :key="item.id" v-model="form.type" :label="item.value">{{ item.label }}</el-radio>
          </el-form-item>
          <el-form-item label="单价" prop="price">
            <el-input v-model="form.price" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="图片">
            <el-upload
              :on-success="handleSuccess"
              :on-error="handleError"
              :headers="headers"
              :file-list="fileList"
              :action="imagesUploadApi"
              :limit="1"
              list-type="picture-card"
            >
              <i class="el-icon-plus" />
            </el-upload>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.desc" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="编号" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="type.cateName" label="类型">
          <!-- <template slot-scope="scope">
            {{ dict.label.food_type[scope.row.type] }}
          </template> -->
        </el-table-column>
        <el-table-column prop="price" label="单价(元)">
          <template slot-scope="scope">
            {{ scope.row.price.toString().indexOf(".") &lt; 0 ?scope.row.price.toString() + '.00':scope.row.price.toString().substring(scope.row.price.toString().indexOf(".")+1,scope.row.price.toString().length).length>1?scope.row.price.toString():scope.row.price.toString() + '0' }}
            <!--            {{scope.row.price.toString().indexOf(".")>0?scope.row.price.toString() + '.00': scope.row.price.toString().substring(scope.row.price.toString().indexOf("."),scope.row.price.toString().length).length>=2?scope.row.price.toString():scope.row.price.toString() + '0'}}-->
          </template>
        </el-table-column>
        <el-table-column prop="picture" label="图片">
          <template slot-scope="{row}">
            <el-image
              :src="row.type && row.type.url && row.type.url.url"
              :preview-src-list="[row.type && row.type.url && row.type.url.url]"
              fit="contain"
              lazy
              class="el-avatar"
            >
              <div slot="error">
                <i class="el-icon-picture" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="createBy" label="创建者" /> -->
        <el-table-column prop="createTime" label="创建日期" />
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmFood from '@/api/dmFood'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { id: null, code: null, name: null, type: null, price: null, picture: null, url: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'DmFood',
  components: { pagination, crudOperation, rrOperation, DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['food_type'],
  cruds() {
    return CRUD({ title: 'food', url: 'api/dmFood', idField: 'id', sort: 'createTime,desc', crudMethod: { ...crudDmFood }, optShow: { del: false, edit: false, add: false, download: true, reset: true }})
  },
  data() {
    return {
      headers: {
        'Authorization': getToken()
      },
      fileList: [],
      permission: {
        add: ['admin', 'dmFood:add'],
        edit: ['admin', 'dmFood:edit'],
        del: ['admin', 'dmFood:del']
      },
      rules: {
        code: [
          { required: true, message: '编号不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '单价不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'code', display_name: '编号' },
        { key: 'name', display_name: '名称' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    handleSuccess(jsonData, field) {
      console.log(jsonData)
      this.form.url = jsonData.id
      this.form.picture = jsonData.type + '/' + jsonData.realName
    },
    handleError(e, file, fileList) {
      const msg = JSON.parse(e.message)
      this.$notify({
        title: msg.message,
        type: 'error',
        duration: 0
      })
    }
  }
}
</script>

<style scoped>

</style>
