<!--
  @description: 协议录入
  @Author:dijia
  @routerPath:/protocolNnterList
  @date：2021/6/9
-->
<template>
  <div>
    <div class="shopmall-page-title">
      {{ roleCode === 'shopSupplier' ? '协议查看' : '协议录入' }}
    </div>
    <div class="search-content clearfix">
      <section class="fl">
        <el-form ref="formitInfo" :model="formitInfo" :inline="true">
          <el-form-item label="协议名称" prop="protocolName">
            <el-input v-model.trim="formitInfo.protocolName" placeholder="" />
          </el-form-item>
          <el-form-item label="协议说明" prop="remark">
            <el-input v-model.trim="formitInfo.remark" placeholder="" />
          </el-form-item>
          <el-form-item label="是否使用" prop="useStatus">
            <el-select v-model="formitInfo.useStatus" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </section>
      <div class="fr">
        <el-button type="purple" @click.native="onSearch">
          搜索
        </el-button>
        <el-button type="gray" @click.native="resetList">
          重置
        </el-button>
      </div>
    </div>
    <div v-if="roleCode !== 'shopSupplier'" class="shopmall-page-add">
      <el-button type="purple" @click="addProtocolBtn">
        新增
      </el-button>
    </div>
    <!-- 表格区域 -->
    <div class="shopmall-view">
      <el-table
        ref="multipleTable"
        v-loading="loading"
        element-loading-text="加载中"
        :data="tableData"
        tooltip-effect="dark"
        :border="false"
        :stripe="false"
        style="width: 100%"
      >
        <el-table-column
          type="index"
          label="序号"
          width="60"
          align="center"
        />
        <el-table-column
          prop="protocolName"
          label="协议名称"
          width="230"
          align="left"
        >
          <template slot-scope="scope">
            <span style="white-space: normal;">
              {{ scope.row.protocolName }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          label="协议说明"
          align="left"
          width="250"
        />
        <el-table-column
          prop="startTime"
          label="开始日期"
          align="center"
          width="160"
        >
          <template slot-scope="scope">
            {{ scope.row.startTime }}
          </template>
        </el-table-column>
        <el-table-column
          prop="endTime"
          label="结束日期"
          align="center"
          width="160"
        >
          <template slot-scope="scope">
            {{ scope.row.endTime }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否使用"
          align="center"
          width="100"
        >
          <template slot-scope="scope">
            <span v-show="scope.row.useStatus=='01'" style="color:#00AACA">
              启用
            </span>
            <span v-show="scope.row.useStatus=='02'" style="color:#F77C46;margin-left:0;">
              禁用
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-link type="primary" @click="handleGetDetails(scope.row.id,'view')">
              [查看]
            </el-link>
            <el-link v-if="roleCode !== 'shopSupplier'" type="primary" @click="handleGetDetails(scope.row.id,'edit')">
              [编辑]
            </el-link>
            <el-link v-if="roleCode !== 'shopSupplier'&& scope.row.useStatus=='02'" type="primary" @click="handleChangeStatus(scope.row.id,'01')">
              [启用]
            </el-link>
            <el-link v-if="roleCode !== 'shopSupplier'&& scope.row.useStatus=='01'" style="color:#F77C46" @click="handleChangeStatus(scope.row.id,'02')">
              [禁用]
            </el-link>
            <el-link v-if="roleCode !== 'shopSupplier'" type="primary" @click="handleDeleteProtocol(scope.row.id)">
              [删除]
            </el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页组件-->
    <pagination :pagination-data="paginationData" shopmall-pagination="shopmall-pagination" @change="initData" />
    <!--新增协议-->
    <protocolNnterDialog ref="protocolDialog" @callback="callback" />
  </div>
</template>
<script>
// api
import { getProtocolList, updateProtocolStatus, getProtocoDetails, deleteprotocol } from '@/api/shoppingmall/products'
// 组件
import protocolNnterDialog from './protocolNnter-dialog.vue'
import pagination from '@comp/common/pagination/pagination'// 分页
/* 公用方法*/
import { filterObj } from '@/assets/js/utils'// 常用方法
export default {
  name: 'ProtocolNnterList',
  components: {
    protocolNnterDialog,
    pagination
  },
  props: {},
  data() {
    return {
      loading: false,
      // 查询列表params对象
      formitInfo: {
        protocolName: null, // 协议名称
        remark: null, // 协议内容
        useStatus: null, // 是否使用
        pageNo: 1,
        pageSize: 10
      },
      // select使用状态数组 01 启用 02 禁用
      options: [
        {
          value: '00',
          label: '请选择',
          disabled: true
        },
        {
          value: '01',
          label: '启用'
        },
        {
          value: '02',
          label: '禁用'
        }
      ],
      // 协议列表数据数组
      tableData: [],
      // 分页数据
      paginationData: {
        pageNo: 1,
        pageSize: 10,
        total: 0
      },
      roleCode: '' // 当前登录人的code 供应商: shopSupplier
    }
  },
  mounted() {
    this.roleCode = this.$store.getters.info.currentRol.roleCode
    this.initData()
  },
  methods: {
    // 搜索
    onSearch() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      // 获取列表信息
      this.initData()
    },
    // 重置
    resetList() {
      this.$refs.formitInfo.resetFields()
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      // 获取列表信息
      this.initData()
    },
    // 获取所有的协议信息
    initData() {
      this.loading = true
      const params = this.getQueryParams()// 查询条件
      getProtocolList(params).then(res => {
        if (res.header.ret === '0') {
          this.loading = false
          this.tableData = res.data.records
          this.paginationData.total = res.data.total
        }
      })
        .catch(err => {
          this.loading = false
          throw new Error('查看协议列表失败' + err)
        })
    },
    // 查询条件
    getQueryParams() {
      const param = Object.assign({}, this.formitInfo)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      return filterObj(param)
    },
    // 修改协议状态
    handleChangeStatus(id, status) {
      this.$confirm('确认要修改协议状态吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        updateProtocolStatus(id, status).then(res => {
          if (res.header.ret === '0') {
            this.$message({
              message: ' 修改状态成功！',
              type: 'success'
            })
            this.initData()
          }
        })
      })
    },
    // 点击新增按钮
    addProtocolBtn() {
      this.$refs.protocolDialog.open({
        visible: true, // dialog显示状态
        type: 'add'
      })
    },
    // 查看协议信息
    handleGetDetails(id, code) {
      getProtocoDetails(id).then(res => {
        if (res.header.ret === '0') {
          this.$refs.protocolDialog.open({
            visible: true,
            advertData: res.data,
            type: code // 类型 add 新增/view 查看/edit 编辑
          })
        }
      })
    },
    // 删除协议
    handleDeleteProtocol(id) {
      this.$confirm('确认要删除协议吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        confirmButtonClass: 'confirmBtn',
        cancelButtonClass: 'cancelBtn',
        type: 'warning'
      }).then(() => {
        deleteprotocol(id).then(res => {
          if (res.header.ret === '0') {
            this.$message({
              message: ' 删除成功！',
              type: 'success'
            })
            this.initData()
          }
        })
      })
    },
    // dialog回调函数
    callback() {
      this.initData()
    }
  }
}
</script>
<style scoped lang="less">
.shopmall-page-add {
  width: 100%;
  margin-top:  12px;
  padding-left: 12px;
  display: flex;
  align-items: center;
}
</style>
