<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
<!--      <div v-if="crud.props.searchToggle">-->
<!--        &lt;!&ndash; 搜索 &ndash;&gt;-->
<!--        <el-select-->
<!--          v-model="query.device_type"-->
<!--          class="filter-item"-->
<!--          clearable-->
<!--          placeholder="设备类型"-->
<!--          size="small"-->
<!--          style="width: 450px"-->
<!--        >-->
<!--          <el-option v-for="item in device_types" :key="item.id" :label="item.label" :value="item.value" />-->
<!--        </el-select>-->
<!--        <rrOperation />-->
<!--      </div>-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :before-close="crud.cancelCU"
        :close-on-click-modal="false"
        :title="crud.status.title"
        :visible.sync="crud.status.cu > 0"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small">
          <el-form-item label="图片上传" prop="image_code">
            <el-upload
              :action="imagesUploadApi"
              :before-upload="beforeUpload_u"
              :file-list="fileList"
              :headers="headers"
              :limit="1"
              list-type="picture"
              :on-success="handleSuccess"
              class="upload-demo"
              multiple
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
          <el-form-item label="图标高度" prop="image_height">
            <el-input v-model="form.image_height" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="图标宽度" prop="image_width">
            <el-input v-model="form.image_width" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="配置名称" prop="configuration_code">
            <el-input v-model="form.configuration_code" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="绑定设备" prop="device_code">
            <el-select v-model="form.device_code" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.device_code"
                :label="item.device_name"
                :value="item.device_code">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否启用">
            <el-switch
              v-model="form.is_active"
              active-value="1"
              inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.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 label="图标" prop="image_name">
          <template slot-scope="{row}">
            <el-image
              :preview-src-list="[baseApi + '/file/图片/' + row.image_code]"
              :src=" baseApi + '/file/图片/' + row.image_code"
              class="el-avatar"
              fit="contain"
              lazy
            >
              <div slot="error">
                <i class="el-icon-document" />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="配置名称" prop="configuration_code" />
        <el-table-column label="图标编号" prop="image_code" />
        <el-table-column label="图标高度" prop="image_height" />
        <el-table-column label="图标宽度" prop="image_width" />
        <el-table-column label="绑定设备编号" prop="device_code" />
        <el-table-column label="是否启用" prop="is_active">
          <template slot-scope="{row}">
            <span v-if="row.is_active === '1'"><el-tag type="success">启用</el-tag></span>
            <span v-else><el-tag type="warning">未启用</el-tag></span>
          </template>
        </el-table-column>
        <el-table-column prop="create_name" label="创建人" />
        <el-table-column prop="create_time" label="创建时间" min-width="135" />
        <el-table-column
          v-permission="['admin','stageImage:edit','stageImage:del']"
          align="center"
          label="操作"
          width="150px"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import CRUD, { crud, form, header, presenter } from '@crud/crud'
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 { selectDeviceList } from '@/api/acs/device/device'
import crudTrajectoryConfiguration from '@/api/trajectory/configuration/trajectory_configuration'

const defaultForm = {
  configuration_uuid: null,
  configuration_code: null,
  image_code: null,
  image_name: null,
  image_height: null,
  image_width: null,
  device_code: null,
  is_active: '1',
  is_delete: null,
  create_by: null,
  create_time: null,
  update_by: null,
  update_time: null
}
export default {
  name: 'TrajectoryConfiguration',
  components: { pagination, crudOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '设备配置',
      url: 'api/trajectoryConfiguration',
      idField: 'configuration_uuid',
      sort: 'configuration_uuid,desc',
      crudMethod: { ...crudTrajectoryConfiguration },
      optShow: {
        add: true,
        edit: true,
        del: true
      }
    })
  },
  data() {
    return {
      value: [],
      permission: {
        add: ['admin', 'TrajectoryConfiguration:add'],
        edit: ['admin', 'TrajectoryConfiguration:edit'],
        del: ['admin', 'TrajectoryConfiguration:del']
      },
      headers: { 'Authorization': getToken() },
      rules: {
        configuration_code: [
          { required: true, message: '配置名称不能为空', trigger: 'blur' }
        ],
        image_code: [
          { required: true, message: '图标编号不能为空', trigger: 'blur' }
        ],
        image_name: [
          { required: true, message: '图标名称不能为空', trigger: 'blur' }
        ],
        image_height: [
          { required: true, message: '图标高度不能为空', trigger: 'blur' }
        ],
        image_width: [
          { required: true, message: '图标宽度不能为空', trigger: 'blur' }
        ],
        device_code: [
          { required: true, message: '绑定设备不能为空', trigger: 'blur' }
        ]
      },
      fileList: [],
      options: []
    }
  },
  computed: {
    ...mapGetters([
      'imagesUploadApi',
      'baseApi'
    ])
  },
  mounted() {
    // 获取所有设备
    selectDeviceList().then(data => {
      console.log(data)
      this.options = data
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterSubmit]() {
      this.fileList = []
    }, [CRUD.HOOK.beforeToAdd]() {
      this.fileList = []
    },
    [CRUD.HOOK.afterToEdit]() {
      let new_lst = []
      const image_code = this.form.image_code
      const image_name = this.baseApi + '/file/图片/' + image_code
      new_lst = [{ name: image_code, url: image_name }]
      this.fileList = new_lst
    },
    beforeUpload_u(file) {
      const testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = (testmsg === 'png' || testmsg === 'jpg' || testmsg === 'svg')

      let bool = false
      if (extension) {
        bool = true
      } else {
        bool = false
      }
      if (!extension) {
        this.$confirm(`上传文件只能是png/jpg/svg格式!`)
      }

      return bool
    },
    handleSuccess(response) {
      console.log(response)
      this.form.image_code = response.real_name
      this.form.image_name = response.path
    }
  }
}
</script>

<style scoped>

</style>
