<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div class="screen-container">
      <el-form ref="screenForm" :model="screenForm" label-width="70px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="退货原因" prop="reason">
              <el-input v-model="screenForm.reason" placeholder="请输入退货原因"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="当前状态" prop="status">
              <el-select v-model="screenForm.status" placeholder="请选择状态">
                <el-option label="已启用" :value="true"></el-option>
                <el-option label="已停用" :value="false"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="创建时间" prop="createDate" style="height: 33px;">
              <el-date-picker
                v-model="screenForm.createDate"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fl">
          <el-button size="small" type="primary" icon="el-icon-plus" @click="addOrEdit('add')">新增退货原因</el-button>
        </div>
      </div>

      <div class="table">
        <el-table ref="multipleTable" v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column align="center" label="退货原因" prop="reason" min-width="200"></el-table-column>
          <el-table-column align="center" label="创建时间" prop="createTime"></el-table-column>
          <el-table-column align="center" label="操作人" prop="createBy"></el-table-column>
          <el-table-column align="center" label="状态" class-name="status-col">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status ? 'success':'danger'">{{ scope.row.status ? '已启用':'已停用' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="140" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrEdit('edit', scope.row.id)">编辑</el-button>

              <template>
                <el-popconfirm v-if="scope.row.status" style="margin-left: 10px;" title="确定停用吗？" @onConfirm="changeStatus(scope.row.id, scope.row.reason, false)" >
                  <el-button slot="reference" type="text">停用</el-button>
                </el-popconfirm>
                <el-popconfirm v-else style="margin-left: 10px;" title="确定启用吗？" @onConfirm="changeStatus(scope.row.id, scope.row.reason, true)" >
                  <el-button slot="reference" type="text">启用</el-button>
                </el-popconfirm>
              </template>

              <el-popconfirm style="margin-left: 10px;" title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
                <el-button slot="reference" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal">
          </el-pagination>
        </div>
      </div>
    </div>

    <!-- 新增编辑 -->
    <el-dialog :title="addFormType === 'add' ? '新增退货原因':'编辑退货原因'" :visible.sync="addFormVisible" :show-close="false" width="50%" :close-on-click-modal="false">
      <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="left" label-width="80px">
        <el-form-item label="退货原因" prop="reason">
          <el-input v-model="addForm.reason" autocomplete="off" placeholder="请输入退货原因" maxlength="40" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            v-model="addForm.status">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddForm">取 消</el-button>
        <el-button type="primary" @click="submitAddForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {saveReason, deleteReason, getReasonList, getReasonDetail} from "@/api/order";

export default {
  data() {
    return {
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      screenForm: { // 筛选表单数据
        reason: '',
        status: '',
        createDate: '',
      },
      editId:  null,
      addFormType: 'add',
      addFormVisible: false,
      addForm: {
        reason: '',
        status: true,
      },
      addFormRules: {
        reason: [
          { required: true, message: '请输入退货原因', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.listLoading = true;

      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        reason: this.screenForm.reason,
        status: this.screenForm.status,
        startTime: this.screenForm.createDate ? this.screenForm.createDate[0] : '',
        endTime: this.screenForm.createDate ? this.screenForm.createDate[1] : '',
      };

      getReasonList(params).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },
    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1
      this.getList()
    },
    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields()
      this.currentPage = 1
      this.getList()
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    // 操作 - 删除
    handleDelete(id) {
      deleteReason({id: id}).then(() => {
        this.getList();
        this.$successMsg();
      })
    },

    // 操作 - 更改状态
    changeStatus(id, reason, status) {
      saveReason({
        id, reason, status
      }).then(res => {
        this.getList();
        this.$successMsg();
      })
    },

    // 新增编辑
    addOrEdit(type, id) {
      this.addFormType = type;
      this.addFormVisible = true;
      if(type == 'edit') {
        this.editId = id;
        getReasonDetail({id: id}).then(res => {
          this.addForm = {
            reason: res.data.reason,
            status: res.data.status
          }
        })
      }
    },

    // 取消 新增编辑
    cancelAddForm(){
      this.addFormVisible = false;
      this.$refs.addForm.resetFields();
    },

    // 提交 新增编辑
    submitAddForm() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          let params = {
            reason: this.addForm.reason,
            status: this.addForm.status
          }
          if(this.addFormType == 'edit') {
            params.id = this.editId;
            saveReason(params).then(res => {
              this.cancelAddForm();
              this.getList();
              this.$successMsg('编辑成功');
            })
          } else {
            saveReason(params).then(res => {
              this.cancelAddForm();
              this.getList();
              this.$successMsg('新增成功');
            })
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
