<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="储配站" prop="stationId">
        <el-select v-model="queryParams.stationId" placeholder="请选择储配站">
          <el-option
            v-for="item in stationList"
            :key="item.stationId"
            :label="item.stationName"
            :value="item.stationId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="气瓶条码" prop="bottleCode">
        <el-input
          v-model="queryParams.bottleCode"
          placeholder="请输入气瓶条码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="自有编号" prop="myselfNum">
        <el-input
          v-model="queryParams.myselfNum"
          placeholder="请输入自有编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 186px"
        />
      </el-form-item>

      <el-form-item label="气瓶状态" prop="bottleStatus">
        <el-select v-model="queryParams.bottleStatus" placeholder="请选择气瓶状态" clearable size="small">
          <el-option label="正常" value="1" />
          <el-option label="逾期未检" value="2" />
          <el-option label="报废" value="3" />
        </el-select>
      </el-form-item>

      <el-form-item label="制造年月" prop="produceDate">
        <el-date-picker
          v-model="produceDate"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="timeChange"
          size="mini"
        >
        </el-date-picker>
      </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"
        >新增气瓶</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-upload2"
          size="mini"
          style="color: #1890ff"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          plain
          icon="el-icon-download"
          size="mini"
		      :loading="exportLoading"
          @click="handleExport"
          style="color: #1890ff"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList">
      <el-table-column label="储配站" align="center" prop="stationName" width="120" :show-overflow-tooltip="true"/>
      <el-table-column label="气瓶条码" align="center" prop="bottleCode" />
      <el-table-column label="自有编号" align="center" prop="myselfNum" />
      <el-table-column label="制造单位" align="center" prop="produceUnit" />
      <el-table-column label="气瓶状态" align="center" prop="bottleStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.bottleStatus == '1'">正常</span>
          <span v-if="scope.row.bottleStatus == '2'">逾期未检</span>
          <span v-if="scope.row.bottleStatus == '3'">报废</span>
        </template>
      </el-table-column>
      <el-table-column label="充装介质" align="center" prop="chargeMedium" />
      <el-table-column label="在站/离站" align="center" prop="currentStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.currentStatus == '0'">在站</span>
          <span v-if="scope.row.currentStatus == '1'">离站</span>
        </template>
      </el-table-column>
      <el-table-column label="空满类型" align="center" prop="emptyType">
        <template slot-scope="scope">
          <span v-if="scope.row.emptyType == '0'">空瓶</span>
          <span v-if="scope.row.emptyType == '1'">满瓶</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="showDetail(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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"
    />

    <!-- 添加或修改气瓶信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <div style="color: #1890ff;margin-bottom: 15px">气瓶基本信息</div>
        <el-row>
          <el-col :span="8">
            <el-form-item label="储配站" prop="stationId">
              <el-select v-model="form.stationId" placeholder="请选择储配站">
                <el-option
                  v-for="item in stationList"
                  :key="item.stationId"
                  :label="item.stationName"
                  :value="item.stationId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="气瓶条码" prop="bottleCode">
              <el-input v-model="form.bottleCode" placeholder="请输入气瓶条码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="自有编号" prop="myselfNum">
              <el-input v-model="form.myselfNum" placeholder="请输入自有编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="在站/离站" prop="currentStatus">
              <el-select v-model="form.currentStatus" placeholder="请选择在站/离站">
                <el-option label="在站" value="0"/>
                <el-option label="离站" value="1"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="气瓶状态" prop="bottleStatus">
              <el-select v-model="form.bottleStatus" placeholder="请选择气瓶状态">
                <el-option label="正常" value="1"/>
                <el-option label="逾期未检" value="2"/>
                <el-option label="报废" value="3"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="空满状态" prop="emptyType">
              <el-select v-model="form.emptyType" placeholder="请选择空满状态">
                <el-option label="空瓶" value="0"/>
                <el-option label="满瓶" value="1"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="登记日期" prop="registerDate">
              <el-date-picker clearable size="small"
                              v-model="form.registerDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              style="width: 100%"
                              placeholder="选择登记日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="下检日期" prop="nextCheckDate">
              <el-date-picker clearable size="small"
                              v-model="form.nextCheckDate"
                              type="date"
                              style="width: 100%"
                              value-format="yyyy-MM-dd"
                              placeholder="选择下检日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="报废日期" prop="scrapDate">
              <el-date-picker clearable size="small"
                              v-model="form.scrapDate"
                              type="date"
                              style="width: 100%"
                              value-format="yyyy-MM-dd"
                              placeholder="选择报废日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="使用登记代码" prop="useRegisterCode">
              <el-input v-model="form.useRegisterCode" placeholder="请输入使用登记代码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="充装介质" prop="chargeMedium">
              <el-input v-model="form.chargeMedium" placeholder="请输入充装介质" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="经纬度坐标" prop="longitude">
          <el-col :span="9">
            <el-input v-model="form.longitude" placeholder="请输入经度" />
          </el-col>
          <el-col :span="9" style="margin-left: 10px">
            <el-input v-model="form.latitude" placeholder="请输入纬度"/>
          </el-col>
          <el-col :span="3" style="margin-left: 30px">
            <el-button type="primary" plain @click="MapdialogFun">选择经纬度</el-button>
          </el-col>
        </el-form-item>

        <div style="color: #1890ff;margin-bottom: 15px ">气瓶规格参数</div>

        <el-row>
          <el-col :span="8">
            <el-form-item label="制造单位" prop="produceUnit">
              <el-input v-model="form.produceUnit" placeholder="请输入制造单位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="制造年月" prop="produceDate">
              <el-date-picker clearable size="small"
                              v-model="form.produceDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              style="width: 100%"
                              placeholder="选择制造年月">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出厂编号" prop="produceCode">
              <el-input v-model="form.produceCode" placeholder="请输入出厂编号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="规格Kg/MPa" prop="bottleSpecs">
              <el-input v-model="form.bottleSpecs" placeholder="请输入气瓶规格" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="气瓶容积L" prop="bottleCapacity">
              <el-input v-model="form.bottleCapacity" placeholder="请输入气瓶容积" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="公称压力Mpa" prop="ratedPresure">
              <el-input v-model="form.ratedPresure" placeholder="请输入公称压力" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="壁厚" prop="wallThickness">
              <el-input v-model="form.wallThickness" placeholder="请输入壁厚" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="皮重" prop="tare">
              <el-input v-model="form.tare" placeholder="请输入皮重" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--地理位置选择-->
    <LocationChoice ref="locationChoice"/>

    <!-- 导入数据 -->
    <ImportData ref="importData"/>

    <!-- 详情 -->
    <DetailInfo ref="detailInfo"/>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo } from "@/api/gasBottleTrack/gasBottleManage";
import LocationChoice from "./LocationChoice";
import { gasStorageStationList } from "@/api/gasBottleTrack/gasHolderStationManage";
import ImportData from "./ImportData";
import DetailInfo from "./DetailInfo";
export default {
  name: "Info",
  components: {
    LocationChoice,
    ImportData,
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 气瓶信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationId: null,
        bottleCode: null,
        myselfNum: null,
        produceUnit: null,
        produceDate: null,
        produceCode: null,
        bottleStatus: null,
        lastChargeDate: null,
        longitude: null,
        latitude: null,
        chargeMedium: null,
        bottleSpecs: null,
        ratedPresure: null,
        bottleCapacity: null,
        wallThickness: null,
        tare: null,
        useRegisterCode: null,
        registerDate: null,
        lastCheckDate: null,
        nextCheckDate: null,
        scrapDate: null,
        currentStatus: null,
        emptyType: null,
        isDel: null,
        produceBeginTime: null,
        produceEndTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stationId: [
          { required: true, message: "请选择储配站", trigger: "change" }
        ],
        bottleCode: [
          { required: true, message: "请输入气瓶条码", trigger: "blur" }
        ],
        myselfNum: [
          { required: true, message: "请输入自有编号", trigger: "blur" }
        ],
        currentStatus: [
          { required: true, message: "请选择在站/离站", trigger: "change" }
        ],
        bottleStatus: [
          { required: true, message: "请选择气瓶状态", trigger: "change" }
        ],
        emptyType: [
          { required: true, message: "请选择空满状态", trigger: "change" }
        ],
        longitude: [
          { required: true, message: "请输入经纬度", trigger: "blur" },
        ],
      },
      stationList: [],
      produceDate: []
    };
  },
  created() {
    this.getList();
    this.getStationInfo();
  },
  methods: {
    /** 查询气瓶信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bottleId: null,
        stationId: null,
        bottleCode: null,
        myselfNum: null,
        produceUnit: null,
        produceDate: null,
        produceCode: null,
        bottleStatus: "1",
        lastChargeDate: null,
        longitude: null,
        latitude: null,
        chargeMedium: null,
        bottleSpecs: null,
        ratedPresure: null,
        bottleCapacity: null,
        wallThickness: null,
        tare: null,
        useRegisterCode: null,
        registerDate: null,
        lastCheckDate: null,
        nextCheckDate: null,
        scrapDate: null,
        currentStatus: "0",
        emptyType: null,
        createTime: null,
        updateTime: null,
        isDel: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.produceBeginTime =  null;
      this.queryParams.produceEndTime =  null;
      this.produceDate = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bottleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getStationInfo();
      this.open = true;
      this.title = "添加气瓶信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getStationInfo();
      const bottleId = row.bottleId || this.ids
      getInfo(bottleId).then(response => {
        this.form = response.data;
        if(this.form.longitude){
          this.$refs.locationChoice.devicePos = [this.form.longitude,this.form.latitude];
        }
        this.open = true;
        this.title = "修改气瓶信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bottleId != null) {
            updateInfo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              if (response!=''){
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }else {
                this.$message({
                  message: '气瓶条码已存在 请重新填写气瓶条码',
                  type: 'warning'
                });
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bottleIds = row.bottleId || this.ids;
      this.$confirm('是否确认删除气瓶条码为"' + row.bottleCode + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(bottleIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有气瓶信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    MapdialogFun() {
      this.$refs.locationChoice.dialogTableVisible = true;
    },
    getStationInfo(){
      gasStorageStationList().then(res =>{
        if(res.code == 200){
          this.stationList = res.data;
        }
      })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.importData.upload.title = "气瓶导入";
      this.$refs.importData.upload.open = true;
    },
    timeChange(value){
      this.queryParams.produceBeginTime =  value[0];
      this.queryParams.produceEndTime =  value[1];
    },
    showDetail(row){
      const bottleId = row.bottleId || this.ids
      getInfo(bottleId).then(response => {
        this.$refs.detailInfo.form = response.data;
        const devicePos = [response.data.longitude, response.data.latitude];
        this.$refs.detailInfo.detailOpen = true;
        this.$refs.detailInfo.initMap(devicePos);
      });
    },
  }
};
</script>
