<!-- eslint-disable no-empty -->
<template>
  <div class="app-container">
    <!-- <div class="head-container">
      <el-row :gutter="20">
        <el-col :span="3">
          <el-input v-model="query.conTypeName" size="small" clearable placeholder="请输入耗材类型名称" style="width: 100%;"
            class="filter-item" @keyup.enter.native="getTableData" />
        </el-col>
        <el-col :span="3">
          <el-input v-model="query.conTypeCode" size="small" clearable placeholder="请输入耗材类型代码" style="width: 100%;"
            class="filter-item" @keyup.enter.native="getTableData" />
        </el-col>
        <el-col :span="3">
          <el-select v-model="query.status" placeholder="请选择商品规格" style="width: 100%;">
            <el-option :label="item.label" :value="item.value" v-for="item in typeEnums" :key="item.label" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="getTableData">查询</el-button>
          <el-button @click="resetHandle">重置</el-button>
        </el-col>
      </el-row>
    </div> -->
    <div class="head-container">
      <el-row>
        <el-col>
          <el-button type="primary" icon="el-icon-plus" @click="clickdialogframe('add')">添加</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="head-container" id="table_drag">
      <el-table ref="table" :data="tableData.data" v-loading="tableData.loading" row-key="id">
        <el-table-column prop="id" label="ID" />
        <el-table-column label="耗材类型名称" prop="conTypeName" />
        <!-- <el-table-column label="耗材类型代码" prop="conTypeCode" />
        <el-table-column label="创建时间" prop="createTime">
          <template v-slot="scope">
            {{ dayjs(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss') }}
          </template>
</el-table-column>
<el-table-column label="店铺ID" prop="shopId">
  <template v-slot="scope">
              <el-link type="primary" @click="$router.push({ name: 'information' })">{{ scope.row.shopId }}</el-link>
            </template>
</el-table-column> -->
        <el-table-column label="状态" prop="status">
          <template v-slot="scope">
            <!-- <el-tag type="primary" v-if="scope.row.status == '1'">正常</el-tag>
            <el-tag type="danger" v-if="scope.row.status == '0'">禁用</el-tag> -->
            <el-switch active-value="0"  inactive-value="1"
              v-model="scope.row.status" @change="changeswitch(scope.row)" />

          </template>
        </el-table-column>
        <!-- <el-table-column label="排序" prop="sort" sortable /> 
        <el-table-column label="更新时间" prop="updateTime">
          <template v-slot="scope">
            {{ dayjs(scope.row.createdAt).format('YYYY-MM-DD HH:mm:ss') }}
          </template>
        </el-table-column>-->
        <el-table-column label="操作" width="200">
          <template v-slot="scope">
            <!-- <el-button type="text" icon="el-icon-rank">排序</el-button> -->
            <el-button type="text" icon="el-icon-edit" @click="clickdialogframe('edit', scope.row)">编辑</el-button>
            <!-- <el-popconfirm title="确定删除吗？" @confirm="delTableHandle([scope.row.id])">
              <el-button type="text" icon="el-icon-delete" style="margin-left: 20px !important;"
                slot="reference">删除</el-button>
            </el-popconfirm> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="head-container">
      <el-pagination :total="tableData.total" :current-page="tableData.page + 1" :page-size="tableData.size"
        layout="total, sizes, prev, pager, next, jumper" @current-change="paginationChange"
        @size-change="e => { tableData.size = e; tableData.page = 0; getTableData() }" />
    </div>
    <el-dialog :title="dialogtitle" :visible.sync="dialogshow">
      <el-form ref="refruleForm" :model="ruleForm" :rules="rules">
        <el-form-item label="耗材类型名称" prop="conTypeName">
          <el-input v-model="ruleForm.conTypeName" @input="trimInput($event, 'conTypeName')" />
        </el-form-item>
        <!-- <el-form-item label="耗材类型代码" prop="conTypeCode">
          <el-input v-model="ruleForm.conTypeCode" />
        </el-form-item> -->
        <el-form-item label="是否禁用" prop="status">
          <el-switch v-model="ruleForm.status" />
        </el-form-item>
        <el-form-item style="display: flex;justify-content: flex-end;">
          <el-button @click="dialogshow = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('refruleForm')">确 定</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import Sortable from 'sortablejs'
import dayjs from 'dayjs'
import settings from '@/settings'
import { upProSort } from '@/api/shop'
import { gettbConsType, posttbConsType, puttbConsType } from '@/api/consumable'
export default {
  data() {
    return {
      dayjs,
      query: {
        conTypeCode: '',
        conTypeName: '',
        status: ''
      },
      categorys: [],
      typeEnums: [{
        label: '正常',
        value: '1'
      }, {
        label: '禁用',
        value: '0'
      }],
      dialogshow: false, //弹框显示
      dialogtitle: '', //文字显示
      tableData: {
        data: [],
        page: 0,
        size: 30,
        loading: false,
        total: 0
      },
      ruleForm: {
        conTypeCode: '',
        conTypeName: '',
        shopId: localStorage.getItem('shopId'),
        status: false
      },
      rules: {
        conTypeName: [
          { required: true, message: '请输入耗材类型名称', trigger: 'blur' }
        ],
        conTypeCode: [
          { required: true, message: '请输入耗材类型代码', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '选择状态', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getTableData()
    this.$nextTick(() => {
      // this.tableDrag()
    })
  },
  methods: {
    trimInput(val, key) {
      console.log(val)
      this.ruleForm[key] = val.replace(/\s+/g, '')
    },
    //表格拖拽
    tableDrag() {
      const el = document.querySelector('#table_drag .el-table__body-wrapper tbody')
      new Sortable(el, {
        animation: 150,
        onEnd: async e => {
          // console.log('拖拽结束===', e);
          if (e.oldIndex == e.newIndex) return
          let oid = this.tableData.data[e.oldIndex].id
          let nid = this.tableData.data[e.newIndex].id
          let ids = this.tableData.data.map(item => item.id)
          try {
            await upProSort({
              strId: oid,
              endId: nid,
              ids: ids
            })
            await this.getTableData()
          } catch (error) {
            console.log(error)
          }
        }
      })
    },
    // 重置查询
    resetHandle() {
      this.query.conTypeCode = ''
      this.query.conTypeName = ''
      this.query.status = ''
      this.tableData.page = 0
      this.getTableData()
    },
    // 分页回调
    paginationChange(e) {
      this.tableData.page = e - 1
      this.getTableData()
    },
    // 获取商品列表
    async getTableData() {
      this.tableData.loading = true
      try {
        const res = await gettbConsType({
          page: this.tableData.page,
          size: this.tableData.size,
          conTypeCode: this.query.conTypeCode,
          conTypeName: this.query.conTypeName,
          status: this.query.status,
          shopId: localStorage.getItem('shopId')
        })
        this.tableData.loading = false
        this.tableData.data = res.content
        this.tableData.total = res.totalElements
      } catch (error) {
        console.log(error)
      }
    },
    // 弹框修改值  添加  编辑
    clickdialogframe(type, item) {
      // eslint-disable-next-line eqeqeq
      if (type == 'add') { // 添加
        this.dialogtitle = '添加'
        this.$nextTick(() => {
          this.$refs.refruleForm.resetFields()
        })
      } else {
        this.dialogtitle = '编辑'
        this.ruleForm.id = item.id
        this.ruleForm.conTypeCode = item.conTypeCode
        this.ruleForm.conTypeName = item.conTypeName
        this.ruleForm.status = item.status == 0 ? true : false
      }
      this.dialogshow = true
    },
    async changeswitch(item) {
      console.log(item)
      await puttbConsType({
        id: item.id,
        conTypeCode: item.conTypeCode,
        conTypeName: item.conTypeName,
        shopId: item.shopId,
        status: item.status
      })
      this.getTableData()
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.dialogtitle == '编辑') {
            await puttbConsType({
              id: this.ruleForm.id,
              conTypeCode: this.ruleForm.conTypeCode,
              conTypeName: this.ruleForm.conTypeName,
              shopId: this.ruleForm.shopId,
              status: this.ruleForm.status ? '0' : '1'
            })
            this.dialogshow = false

            this.getTableData()
          } else {
            await posttbConsType({
              // conTypeCode: this.ruleForm.conTypeCode,
              conTypeName: this.ruleForm.conTypeName,
              shopId: this.ruleForm.shopId,
              status: this.ruleForm.status ? '0' : '1'
            })
          }
          this.dialogshow = false
          this.$refs[formName].resetFields()
          this.getTableData()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 删除商品
    async delTableHandle(ids) {
      try {
        await tbProductDelete(ids)
        this.getTableData()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="scss">
.handle {
  font-size: 18px;
  color: #999;

  &:hover {
    cursor: grab;
  }
}

.shop_info {
  display: flex;

  .info {
    flex: 1;
    padding-left: 8px;
    display: flex;
    flex-direction: column;

    .tag_wrap {
      display: flex;
    }
  }
}
</style>
