<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="satelliteName" label="卫星名称" align="center">
      </el-table-column>
      <el-table-column
        prop="imageSatelliteType"
        label="卫星标识"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="imageResolution" label="分辨率标识" align="center">
      </el-table-column>
      <el-table-column prop="imageSensorType" label="传感器" align="center">
      </el-table-column>
      <el-table-column prop="sensorName" label="传感器名称" align="center">
      </el-table-column>
      <el-table-column prop="price" label="价格" align="center">
      </el-table-column>
      <el-table-column prop="dailySort" label="排序" align="center">
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.status == 0">不可用</span>
          <span v-if="row.status == 1">可用</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, 0)"
              >设为不可用</el-button
            >
            <el-button
              v-if="row.status == 0"
              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="text-align: center"
      @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"
      @closed="checkList"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="卫星名称" prop="satelliteName">
          <el-input v-model="form.satelliteName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="卫星标识" prop="imageSatelliteType">
          <el-input
            v-model="form.imageSatelliteType"
            style="width: 80%"
          ></el-input>
        </el-form-item>
        <el-form-item label="分辨率标识" prop="imageResolution">
          <el-input
            v-model="form.imageResolution"
            style="width: 80%"
          ></el-input>
        </el-form-item>
        <el-form-item label="传感器" prop="imageSensorType">
          <el-input
            v-model="form.imageSensorType"
            style="width: 80%"
          ></el-input>
        </el-form-item>
        <el-form-item label="传感器名称" prop="sensorName">
          <el-input v-model="form.sensorName" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="dailySort">
          <el-input-number
            :min="0"
            :step="1"
            :precision="0"
            v-model="form.dailySort"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number
            :min="0"
            :step="0.01"
            :precision="2"
            v-model="form.price"
          ></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 {
  getSatelliteData,
  delimagePrice,
  addimagePrice,
  updateimagePrice,
} from "api/website/archiveDataManagement";
export default {
  data() {
    return {
      list: [{}],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
      },
      form: {
        satelliteName: undefined,
        imageSatelliteType: undefined,
        imageResolution: undefined,
        imageSensorType: undefined,
        sensorName: undefined,
        dailySort: 0,
        price: 0,
        status: 0,
      },
      rules: {
        satelliteName: [
          { required: true, message: "请输入卫星名称", trigger: "blur" },
        ],
        imageSatelliteType: [
          { required: true, message: "请输入卫星标识", trigger: "blur" },
        ],
        imageResolution: [
          { required: true, message: "请输入分辨率标识", trigger: "blur" },
        ],
        imageSensorType: [
          { required: true, message: "请输入传感器", trigger: "blur" },
        ],
        sensorName: [
          { required: true, message: "请输入传感器名称", trigger: "blur" },
        ],
        dailySort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        price: [{ required: true, message: "请输入价格", trigger: "blur" }],
      },
      showAddOrUpd: false,
      titleText: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    checkList() {
      this.getList();
    },
    getList() {
      getSatelliteData(this.listQuery).then((res) => {
        if (res.status == 200) {
          this.total = res.data.totalCount;
          this.list = res.data.data;
        }
      });
    },
    addItem() {
      this.showAddOrUpd = true;
      this.titleText = "添加卫星传感器";
      this.form = {
        satelliteName: undefined,
        imageSatelliteType: undefined,
        imageResolution: undefined,
        imageSensorType: undefined,
        sensorName: undefined,
        dailySort: 0,
        price: 0,
        status: 0,
      };
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    edit(row) {
      this.showAddOrUpd = true;
      this.titleText = "修改卫星传感器";
      this.form = row;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    changeSate(row, state) {
      let data = {
        id: row.id,
        status: state,
      };
      updateimagePrice(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) {
            //编辑
            updateimagePrice(this.form).then((res) => {
              if (res.status == 200) {
                this.$message.success("修改成功");
                this.getList();
                this.showAddOrUpd = false;
              } else {
                this.$message.warning(res.message);
              }
            });
          } else {
            //添加
            addimagePrice(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(() => {
          delimagePrice({ 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: "已取消",
          });
        });
    },
    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>
