<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板名称">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入模板名称"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后修改人" label-width="100px">
        <el-input
          v-model="queryParams.updateBy"
          placeholder="请输入最后修改人"
          clearable
          style="width: 240px;"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['ticketMr:InventoryTemplate:add']"
            >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="dataList" border>
      <el-table-column label="序号" align="center" type="index" width="60"></el-table-column>
      <el-table-column label="模板名称" align="center" prop="name" />
      <el-table-column label="创建人" align="center" prop="createBy"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
      <el-table-column label="最后修改人" align="center" prop="updateBy"></el-table-column>
      <el-table-column label="最后修改时间" align="center" prop="updateTime"></el-table-column>
      <el-table-column label="备注" align="center" prop="remark"></el-table-column>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="openDetails(scope.row)"
            v-if="checkPermi(['ticketMr:InventoryTemplate:details'])"
          >详情</el-button>
          <el-button
              size="mini"
              :type="scope.row.status != 1 ? 'text':'text'"
              style="margin-left: 10px;"
              @click="handleRelease(scope.row)"
              v-if="checkPermi(['ticketMr:InventoryTemplate:release'])"
            >{{ scope.row.status == 1 ? '取消发布' : '发布' }}</el-button>
          <el-button
              size="mini"
              type="text"
              style="margin-left: 10px;"
              @click="handleUpdate(scope.row)"
              v-if="checkPermi(['ticketMr:InventoryTemplate:edit'])"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              style="margin-left: 10px;"
              v-if="checkPermi(['ticketMr:InventoryTemplate:delete'])"
              @click="handleDelete(scope.row,scope.index)"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 新增/编辑弹框 -->
    <add-and-edit
      ref="addAndEdit"
      @getList="getList"
    />

  </div>
</template>

<script>

import { pageList, deleteById, releaseById } from '@/api/ticketMr/InventoryTemplate'
import addAndEdit from "./dialog/addAndEdit";
import { checkPermi } from "@/utils/permission"
export default {
  name: "agreement",
  dicts: [],
  components: { addAndEdit },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      dataList: null,
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      statusList: [
        {id: 1, name: '未发布', value: 0},
        {id: 2, name: '发布', value: 1},
        {id: 3, name: '下架', value: 2},
      ],
      visibleStatus: false,
      newObj: {},
      visibleType: ''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkPermi,
    /** 查询列表 */
    getList() {
      this.loading = true;
      pageList(this.addDateRange(this.queryParams, this.dateRange))
      .then(response => {
          this.dataList = response.data.rows;
          this.dataList.forEach(item =>{
            item.switchValue = item.status;
          })
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs["addAndEdit"].openDialog("添加库存模板", null, "ADD");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs["addAndEdit"].openDialog("编辑库存模板", row, "EDIT");
    },
    /** 详情按钮操作 */
    openDetails(row) {
      this.$refs["addAndEdit"].openDialog("库存模板详情", row, "DETAILS");
    },

    // 修改是否启用
    changeStatus(row) {
      console.log(row)
    },
    // 发布
    handleRelease(row) {
      let srt = row.status == 1 ? `取消发布后,该模板将在已绑定的排期中失效,确认要取消发布"${row.name}"吗？`: `是否确认发布"${row.name} "？`
      this.$modal.confirm(srt).then(function() {
        return releaseById({
          id:row.id,
          status: row.status == 1 ? 0 : 1
        });
      }).then((res) => {
        if(res.code == 200) {
          if(row.status == 0) {
            this.$confirm('模板发布成功后,可前往排期中进行绑定!','模板发布成功!',{
              type: 'success',
              confirmButtonText: '前往排期设置',
              cancelButtonText: '取消',
              center: true
            }).then(()=> {
              //this.getList();
              this.$router.push('/ticketMr/schedulingConfiguration')
            }).catch(() => {
              this.getList();
            });
          }else {
            this.$modal.msgSuccess("取消发布成功");
            this.getList();
          }
        }
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除"' + row.name + '"？').then(function() {
        return deleteById({id:row.id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 查看按钮操作 */
    seeCenter(obj, type) {
      this.visibleStatus = true
      this.visibleType = type;
      this.newObj = obj;
    }
  }
};
</script>
