<!-- 模板管理 -->
<template>
  <div class="stock-order">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryData()">
      <el-form-item label="模板名称">
        <el-input v-model="dataForm.name" placeholder="模板名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dataForm.date"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="queryData()">查询</el-button>
        <el-button v-if="isAuth('wh:templatemg:save')" type="primary" @click="addOrUpdateHandle(0, false)">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center">
      </el-table-column>
      <el-table-column
        prop="templateId"
        v-if="false"
        header-align="center"
        align="center"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="templateCode"
        header-align="center"
        align="center"
        min-width="120"
        label="编码">
      </el-table-column>
      <el-table-column
        prop="templateName"
        header-align="center"
        align="center"
        min-width="120"
        label="模板名称">
      </el-table-column>
      <el-table-column
        prop="usedNumber"
        header-align="center"
        align="center"
        label="使用次数">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="productId"
        header-align="center"
        align="center"
        :formatter="formatProduct"
        label="是否产品模板">
      </el-table-column>
      <el-table-column
        prop="creatorName"
        header-align="center"
        align="center"
        label="创建人">
      </el-table-column>
      <el-table-column
        prop="notes"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('wh:templatemg:info')" type="text" size="small" @click="addOrUpdateHandle(scope.row.templateId, true)">查看</el-button>
          <el-button v-if="isAuth('wh:templatemg:editor') && scope.row.state === '0'" style="color: red" type="text" size="small" @click="update(scope.row.templateId, 1)">停用</el-button>
          <el-button v-if="isAuth('wh:templatemg:editor') && scope.row.state === '1'" type="text" size="small" @click="update(scope.row.templateId, 0)">启用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!--    模板出库-->
    <template-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></template-add>
  </div>
</template>

<script>
  import templateAdd from './template-add'
  import { getTemplateList, updateTemplate } from '@/api/warehouse'
  export default {
    name: 'stock-order',
    components: {templateAdd},
    data () {
      return {
        addOrUpdateVisible: false,
        outboundVisible: false,
        dataForm: {
          name: '',
          date: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: []
      }
    },
    created () {
      this.queryData()
    },
    methods: {
      // 查询
      queryData () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      async getDataList () {
        this.dataListLoading = true
        let params = {
          current: this.pageIndex,
          size: this.pageSize,
          beginTime: this.dataForm.date[0],
          endTime: this.dataForm.date[1],
          templateName: this.dataForm.name
        }
        await getTemplateList(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 添加模板
      addOrUpdateHandle (id, disable) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, disable)
        })
      },
      // 是否产品模板
      formatProduct (row) {
        return row.productId ? '是' : '否'
      },
      // 停用
      async update (templateId, state) {
        let postData = {
          state: state,
          templateId: templateId,
          updaterId: this.$store.state.user.id
        }
        await updateTemplate(postData).then(({data}) => {
          if (data && data.code === '200') {
            this.getDataList()
          } else {
            this.$message.error(data.msg)
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
