<template>
  <div class="main">
    <el-row>
      <el-button type="primary" @click="addItem">添加分辨率</el-button>
    </el-row>
    <el-table border :data="list" style="margin-top: 20px; width: 100%">
      <el-table-column prop="resolutionName" label="分辨率名称" align="center">
      </el-table-column>
      <el-table-column prop="resolution" label="分辨率标识" align="center">
      </el-table-column>
      <el-table-column prop="sort" label="排序" align="center">
      </el-table-column>

      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.status == 1">上架</span>
          <span v-if="row.status == 2">下架</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-row>
            <el-button type="text" @click="edit(row)">编辑</el-button>
            <el-button
              v-if="row.status == 1"
              type="text"
              @click="changeSate(row, 2)"
              >下架</el-button
            >
            <el-button
              v-if="row.status == 2"
              type="text"
              @click="changeSate(row, 1)"
              >上架</el-button
            >

            <el-button type="text" @click="delItem(row)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="listQuery.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      :title="titleText"
      :visible.sync="showAddOrUpd"
      width="30%"
      @close="showAddOrUpd = false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="分辨率名称" prop="resolutionName">
          <el-input v-model="form.resolutionName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="分辨率标识" prop="resolution">
          <el-input v-model="form.resolution" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            :min="0"
            :step="1"
            :precision="0"
            v-model="form.sort"
          ></el-input-number>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddOrUpd = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdOk('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getResolutionData,
  delResolution,
  addResolution,
  updateResolution,
} from "api/website/archiveDataManagement";
export default {
  data() {
    return {
      list: [{}],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
      },
      form: {
        resolutionName: undefined,
        resolution: undefined,
        sort: 0,
        status: 2,
      },
      rules: {
        resolutionName: [
          { required: true, message: "请输入分辨率名称", trigger: "blur" },
        ],
        resolution: [
          { required: true, message: "请输入分辨率标识", trigger: "blur" },
        ],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
      },
      showAddOrUpd: false,
      titleText: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    addItem() {
      this.showAddOrUpd = true;
      this.titleText = "添加分辨率";
      this.form = {
        resolutionName: undefined,
        resolution: undefined,
        sort: 0,
        status: 2,
      };
    },
    edit(row) {
      this.showAddOrUpd = true;
      this.titleText = "修改分辨率";
      this.form = row;
    },
    changeSate(row, state) {
      let data = {
        id: row.id,
        status: state,
      };
      updateResolution(data).then((res) => {
        if (res.status == 200) {
          this.$message.success("修改成功");
          this.getList();
        } else {
          this.$message.warning(res.message);
        }
      });
    },
    addOrUpdOk(formname) {
      this.$refs[formname].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            //编辑
            updateResolution(this.form).then((res) => {
              if (res.status == 200) {
                this.$message.success("修改成功");
                this.getList();
                this.showAddOrUpd = false;
              } else {
                this.$message.warning(res.message);
              }
            });
          } else {
            //添加
            addResolution(this.form).then((res) => {
              if (res.status == 200) {
                this.$message.success("添加成功");
                this.listQuery.page = 1;
                this.getList();
                this.showAddOrUpd = false;
              } else {
                this.$message.warning(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    delItem(row) {
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delResolution({ id: Number(row.id) }).then((res) => {
            if (res.status == 200) {
              this.$message.success("删除成功");
              this.getList();
            } else {
              this.$message.warning(res.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    getList() {
      getResolutionData(this.listQuery).then((res) => {
        if (res.status == 200) {
          this.total = res.data.totalCount;
          this.list = res.data.data;
        }
      });
    },
    handleSizeChange(val) {
      this.listQuery.page = 1;
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
  },
};
</script>
<style lang="scss" scoped>
.main {
  box-sizing: border-box;
  padding: 36px;
}
</style>
