<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="xmmc">
        <el-input v-model="queryParams.xmmc" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目状态" prop="xmzt">
        <el-select v-model="queryParams.xmzt" placeholder="请选择项目状态" clearable>
          <el-option v-for="dict in dict.type.ys_xmzt" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="区块" prop="qk">
        <el-input v-model="queryParams.qk" placeholder="请输入区块" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="结合单位" prop="jhdw">
        <el-input v-model="queryParams.jhdw" placeholder="请输入结合单位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <!-- <el-form-item label="甲方单位" prop="jfdw">
        <el-input v-model="queryParams.jfdw" placeholder="请输入甲方单位" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="x轴最小值" prop="xmin">
        <el-input v-model="queryParams.xmin" placeholder="请输入x轴最小值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="x轴最大值" prop="xmax">
        <el-input v-model="queryParams.xmax" placeholder="请输入x轴最大值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="y轴最小值" prop="ymin">
        <el-input v-model="queryParams.ymin" placeholder="请输入y轴最小值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="y轴最大值" prop="ymax">
        <el-input v-model="queryParams.ymax" placeholder="请输入y轴最大值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建人" prop="createdBy">
        <el-input v-model="queryParams.createdBy" placeholder="请输入创建人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable v-model="queryParams.createdTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="备用1" prop="ext1">
        <el-input v-model="queryParams.ext1" placeholder="请输入备用1" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="备用2" prop="ext2">
        <el-input v-model="queryParams.ext2" placeholder="请输入备用2" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="备用3" prop="ext3">
        <el-input v-model="queryParams.ext3" placeholder="请输入备用3" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目状态" prop="xmzt">
        <el-input v-model="queryParams.xmzt" placeholder="请输入项目状态" clearable @keyup.enter.native="handleQuery" />
      </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-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ysqqXmxx:ysqqXmxx:add']">新增</el-button>
        <!-- <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ysqqXmxx:ysqqXmxx:edit']">修改</el-button>
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ysqqXmxx:ysqqXmxx:remove']">删除</el-button> -->
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ysqqXmxx:ysqqXmxx:export']">导出</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      </el-col>
      <el- :span="1.5">
      </el-col>
      <el-col :span="1.5">
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->
    <el-table border v-loading="loading" :data="ysqqXmxxList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="项目名称" align="center" prop="xmmc" min-width="100" show-overflow-tooltip />
      <el-table-column label="项目日期" align="center" prop="xmrq" min-width="100" show-overflow-tooltip />
      <el-table-column label="区块" align="center" prop="qk" min-width="70" show-overflow-tooltip />
      <el-table-column label="项目简介" align="center" prop="xmjj" min-width="120" show-overflow-tooltip />
      <el-table-column label="结合单位" align="center" prop="jhdw" min-width="90" show-overflow-tooltip />
      <el-table-column label="甲方单位" align="center" prop="jfdw" min-width="90" show-overflow-tooltip />
      <el-table-column label="项目状态" align="center" prop="xmzt" min-width="90" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ys_xmzt" :value="scope.row.xmzt" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="x轴最小值" align="center" prop="xmin" min-width="90" show-overflow-tooltip />
      <el-table-column label="x轴最大值" align="center" prop="xmax" min-width="90" show-overflow-tooltip />
      <el-table-column label="y轴最小值" align="center" prop="ymin" min-width="90" show-overflow-tooltip />
      <el-table-column label="y轴最大值" align="center" prop="ymax" min-width="90" show-overflow-tooltip /> -->
      <!-- <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
</el-table-column>
<el-table-column label="备用1" align="center" prop="ext1" />
<el-table-column label="备用2" align="center" prop="ext2" />
<el-table-column label="备用3" align="center" prop="ext3" /> -->
      <!-- <el-table-column label="项目状态" align="center" prop="xmzt" /> -->
      <el-table-column label="操作" min-width="260" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 判断是否为查看者 -->
          <template v-if="isViewer(scope.row.xmjs)">
            <!-- 查看者：只能查看 -->
            <el-button size="mini" type="text" icon="el-icon-odometer" @click="handleJxxInfo(scope.row)">井信息</el-button>
            <el-button size="mini" type="text" icon="el-icon-user" @click="handleXmcyInfo(scope.row)">项目成员</el-button>
            <el-button size="mini" type="text" icon="el-icon-files"
              @click="handleSegyInfo(scope.row)">segy信息</el-button>
            <el-button size="mini" type="text" icon="el-icon-location"
              @click="handleYsgcInfo(scope.row)">导航图</el-button>
          </template>
          <!-- 管理者和协作者：根据项目状态显示不同操作 -->
          <template v-else>
            <!-- 未提交状态：显示所有操作按钮 -->
            <template v-if="scope.row.xmzt === '未提交'">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['ysqqXmxx:ysqqXmxx:edit']">修改</el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['ysqqXmxx:ysqqXmxx:remove']">删除</el-button>
              <el-button size="mini" type="text" icon="el-icon-upload2" @click="handleSubmit(scope.row)"
                v-hasPermi="['ysqqXmxx:ysqqXmxx:submit']">提交</el-button>
              <el-button size="mini" type="text" icon="el-icon-odometer" @click="handleJxxInfo(scope.row)">井信息</el-button>
              <el-button size="mini" type="text" icon="el-icon-user" @click="handleXmcyInfo(scope.row)">项目成员</el-button>
              <el-button size="mini" type="text" icon="el-icon-files"
                @click="handleSegyInfo(scope.row)">segy信息</el-button>
              <el-button size="mini" type="text" icon="el-icon-location"
                @click="handleYsgcInfo(scope.row)">导航图</el-button>
            </template>
            <!-- 待验收状态：显示查看、井信息、segy信息、撤回和导航图按钮 -->
            <template v-else-if="scope.row.xmzt === '待验收'">
              <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
              <el-button size="mini" type="text" icon="el-icon-odometer" @click="handleJxxInfo(scope.row)">井信息</el-button>
              <el-button size="mini" type="text" icon="el-icon-user" @click="handleXmcyInfo(scope.row)">项目成员</el-button>
              <el-button size="mini" type="text" icon="el-icon-files"
                @click="handleSegyInfo(scope.row)">segy信息</el-button>
              <el-button size="mini" type="text" icon="el-icon-back" @click="handleWithdraw(scope.row)"
                v-hasPermi="['ysqqXmxx:ysqqXmxx:submit']">撤回</el-button>
              <el-button size="mini" type="text" icon="el-icon-location"
                @click="handleYsgcInfo(scope.row)">导航图</el-button>
            </template>
            <!-- 已验收和已归档状态：只显示查看按钮 -->
            <template v-else>
              <el-button size="mini" type="text" icon="el-icon-odometer" @click="handleJxxInfo(scope.row)">井信息</el-button>
              <el-button size="mini" type="text" icon="el-icon-user" @click="handleXmcyInfo(scope.row)">项目成员</el-button>
              <el-button size="mini" type="text" icon="el-icon-files"
                @click="handleSegyInfo(scope.row)">segy信息</el-button>
              <el-button size="mini" type="text" icon="el-icon-location"
                @click="handleYsgcInfo(scope.row)">导航图</el-button>
            </template>
          </template>
        </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="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="xmmc">
              <el-input v-model="form.xmmc" placeholder="请输入项目名称" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区块" prop="qk">
              <el-input v-model="form.qk" placeholder="请输入区块" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目日期" prop="xmrq">
              <el-date-picker clearable v-model="form.xmrq" type="date" value-format="yyyy-MM-dd" placeholder="请选择时间"
                style="width: 100%;" :disabled="isReadOnly">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结合单位" prop="jhdw">
              <el-input v-model="form.jhdw" placeholder="请输入结合单位" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="甲方单位" prop="jfdw">
              <el-input v-model="form.jfdw" placeholder="请输入甲方单位" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目简介" prop="xmjj">
              <el-input v-model="form.xmjj" type="textarea" placeholder="请输入内容" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="x轴最小值" prop="xmin">
              <el-input v-model="form.xmin" placeholder="请输入x轴最小值" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="x轴最大值" prop="xmax">
              <el-input v-model="form.xmax" placeholder="请输入x轴最大值" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="y轴最小值" prop="ymin">
              <el-input v-model="form.ymin" placeholder="请输入y轴最小值" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="y轴最大值" prop="ymax">
              <el-input v-model="form.ymax" placeholder="请输入y轴最大值" :disabled="isReadOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable v-model="form.createdTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备用1" prop="ext1">
          <el-input v-model="form.ext1" placeholder="请输入备用1" />
        </el-form-item>
        <el-form-item label="备用2" prop="ext2">
          <el-input v-model="form.ext2" placeholder="请输入备用2" />
        </el-form-item>
        <el-form-item label="备用3" prop="ext3">
          <el-input v-model="form.ext3" placeholder="请输入备用3" />
        </el-form-item> -->
        <!-- <el-form-item label="项目状态" prop="xmzt">
          <el-select v-model="form.xmzt" placeholder="请选择项目状态" clearable>
            <el-option v-for="dict in dict.type.ys_xmzt" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!isReadOnly" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">{{ isReadOnly ? '关 闭' : '取 消' }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listYsqqXmxx, getYsqqXmxx, delYsqqXmxx, addYsqqXmxx, updateYsqqXmxx, startConvert, withdraw } from "@/api/ysqqXmxx/ysqqXmxx"

export default {
  name: "YsqqXmxx",
  dicts: ['ys_xmzt', 'xm_js'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 验收前期-项目信息表格数据
      ysqqXmxxList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        xmmc: null,
        qk: null,
        xmjj: null,
        jhdw: null,
        jfdw: null,
        xmin: null,
        xmax: null,
        ymin: null,
        ymax: null,
        createdBy: null,
        createdTime: null,
        ext1: null,
        ext2: null,
        ext3: null,
        xmzt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xmmc: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        xmrq: [
          { required: true, message: "项目日期不能为空", trigger: "blur" }
        ],
        xmin: [
          { required: true, message: "x轴最小值不能为空", trigger: "blur" }
        ],
        xmax: [
          { required: true, message: "x轴最大值不能为空", trigger: "blur" }
        ],
        ymin: [
          { required: true, message: "y轴最小值不能为空", trigger: "blur" }
        ],
        ymax: [
          { required: true, message: "y轴最大值不能为空", trigger: "blur" }
        ],



      },
      // 是否只读模式
      isReadOnly: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询验收前期-项目信息列表 */
    getList() {
      this.loading = true
      listYsqqXmxx(this.queryParams).then(response => {
        this.ysqqXmxxList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xmmc: null,
        qk: null,
        xmjj: null,
        jhdw: null,
        jfdw: null,
        xmin: null,
        xmax: null,
        ymin: null,
        ymax: null,
        createdBy: null,
        createdTime: null,
        updateBy: null,
        updateTime: null,
        ext1: null,
        ext2: null,
        ext3: null,
        xmzt: '未提交' // 新增时默认为未提交状态
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加验收前期-项目信息"
      // 设置为编辑模式
      this.isReadOnly = false
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getYsqqXmxx(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改验收前期-项目信息"
        // 设置为编辑模式
        this.isReadOnly = false
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateYsqqXmxx(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addYsqqXmxx(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除验收前期-项目信息编号为"' + ids + '"的数据项？').then(function () {
        return delYsqqXmxx(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ysqqXmxx/ysqqXmxx/export', {
        ...this.queryParams
      }, `ysqqXmxx_${new Date().getTime()}.xlsx`)
    },
    /** 井信息按钮操作 */
    handleJxxInfo(row) {
      this.$router.push({
        path: '/ysqqXmxxJxx/jxx/index',
        query: {
          zbid: row.id,
          status: row.xmzt // 传递项目状态
        }
      })
    },
    /** 项目成员按钮操作 */
    handleXmcyInfo(row) {
      this.$router.push({
        path: '/ysqqXmxxXmcy/xmcy/index',
        query: {
          zbid: row.id,
          xmjs: row.xmjs // 传递当前用户在该项目中的角色
        }
      })
    },
    /** segy信息按钮操作 */
    handleSegyInfo(row) {
      this.$router.push({
        path: '/ysqqXmxxSegy/segy/index',
        query: {
          zbid: row.id,
          status: row.xmzt // 传递项目状态
        }
      })
    },
    /** 导航图按钮操作 */
    handleYsgcInfo(row) {
      this.$router.push({
        path: '/ysqqXmxx/ysgc/index',
        query: {
          zbid: row.id,
          status: row.xmzt // 传递项目状态
        }
      })
    },
    /** 提交按钮操作 */
    handleSubmit(row) {
      this.$modal.confirm('是否确认提交项目"' + row.xmmc + '"？').then(() => {
        return startConvert(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("提交成功")
      }).catch(() => { })
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset()
      const id = row.id || this.ids
      getYsqqXmxx(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "查看验收前期-项目信息"
        // 设置为只读模式
        this.isReadOnly = true
      })
    },
    /** 撤回按钮操作 */
    handleWithdraw(row) {
      this.$modal.confirm('是否确认撤回项目"' + row.xmmc + '"？').then(() => {
        return withdraw(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("撤回成功")
      }).catch(() => { })
    },
    /** 判断是否为查看者 */
    isViewer(xmjs) {
      if (!xmjs || !this.dict || !this.dict.type || !this.dict.type.xm_js) {
        return false
      }
      // 查找字典中标签包含"查看者"的角色
      const role = this.dict.type.xm_js.find(item => item.value === xmjs)
      if (role && role.label.includes('查看者')) {
        return true
      }
      return false
    }
  }
}
</script>
