<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键字搜索:" >
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态:" >
        <el-select
        style="width: 100%"
        v-model="queryParams.auditStatus"
        class="m-2"
        placeholder="请选择"
        size="large"
      >
        <el-option key="" label="全部" value="" />
        <el-option key="0" label="审核中" value="0" />
        <el-option key="1" label="审核通过" value="1" />
        <el-option key="2" label="审核驳回" value="2" />
      </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:layers:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <!-- <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:layers:remove']"
          >删除</el-button
        > -->
        <el-button
        type="warning"
        plain
        icon="Download"
        @click="handleExport"
        v-hasPermi="['system:user:export']"
     >导出</el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="layersList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="邮箱" show-overflow-tooltip align="center" prop="email" />
      <el-table-column label="单位" show-overflow-tooltip align="c enter" prop="company" />
      <el-table-column label="职务" show-overflow-tooltip align="center" prop="position" />
      <el-table-column label="需求内容" align="center" prop="trialScope" >
        <template #default="scope">
          <el-tag

          class="mx-1"
          style="margin-top: 2px;"
          effect="dark"
        >
         <span   >
          <el-tooltip
          class="box-item"
          effect="dark"
          :content="scope.row.trialScope"
          placement="top-start"
        >
        {{scope.row.trialScope}}
        </el-tooltip>


        </span>

        </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="auditStatus" >
        <template #default="scope">
          <el-tag


          :type="scope.row.auditStatus=='0'?'warning':scope.row.auditStatus=='1'?'success':'danger'"
          class="mx-1"
          effect="dark"

        >
        {{scope.row.auditStatus=='0'?'审核中':scope.row.auditStatus=='1'?'审核通过':'审核驳回'}}

        </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" show-overflow-tooltip align="center" prop="remark" />
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        width="200"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">

          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >审核</el-button
          >
          <!-- <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:layers:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改部门对话框 -->

    <el-dialog title="申请信息审核" v-model="open" width="600px" append-to-body>
      <el-form ref="deptRef" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item prop="name" label="姓名">
              <el-input
                v-model="form.name"
                type="text"
                size="large"
                auto-complete="off"
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>

            <el-form-item prop="mobile" label="手机号">
              <el-input
                v-model="form.mobile"
                type="text"
                size="large"
                auto-complete="off"
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>
            <el-form-item prop="company" label="公司">
              <el-input
                v-model="form.company"
                type="text"
                size="large"
                auto-complete="off"
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>
            <el-form-item prop="position" label="职务">
              <el-input
                v-model="form.position"
                type="text"
                size="large"
                auto-complete="off"
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>
            <el-form-item prop="email" label="邮箱">
              <el-input
                v-model="form.email"
                type="text"
                size="large"
                auto-complete="off"
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>
            <el-form-item prop="trialScope" label="需求内容">
              <el-select
                style="width: 100%"
                v-model="form.trialScope"
                class="m-2"
                multiple

                placeholder="请选择"
                size="large"
              >
              <el-option key="管线设计" label="管线设计" value="管线设计" />
              <el-option key="运营合作" label="运营合作" value="运营合作" />
              <el-option key="设备合作" label="设备合作" value="设备合作" />
              <el-option key="项目排查" label="项目排查" value="项目排查" />
              <el-option key="其他" label="其他" value="其他" />
              </el-select>
            </el-form-item>
            <el-form-item label="备注">
              <el-input
                v-model="form.remark"
                type="textarea"
                size="large"
                auto-complete="off"
                placeholder="请输入"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm('agree')">审核通过</el-button>
          <el-button type="danger" @click="submitForm('reject')">审核驳回</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Layers">
import {
  list,
  agree,
  delectById,
  reject
} from "@/api/system/trial";

const { proxy } = getCurrentInstance();
const data = reactive({
  // 按钮loading
  buttonLoading: false,
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 图层管理表格数据
  layersList: [],
  // 弹出层标题
  title: "",
  // 是否显示弹出层
  open: false,
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    layerName: undefined,
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {
    layerName: [
      { required: true, message: "图层名称不能为空", trigger: "blur" },
    ],
    layerUrl: [
      { required: true, message: "图层地址不能为空", trigger: "blur" },
    ],
    tiffUrl: [
      { required: true, message: "航拍图地址不能为空", trigger: "blur" },
    ],
    ascUrl: [{ required: true, message: "高成文件不能为空", trigger: "blur" }],

    fileLoad: [
      { required: true, message: "文件夹名称不能为空", trigger: "blur" },
    ],
  },
});

const {
  open,
  showSearch,
  queryParams,
  form,
  ids,
  rules,
  title,
  layersList,
  total,
  multiple,
  single,
  loading,
  buttonLoading,
} = toRefs(data);
const userSelect = ref("图层分配用户");
const openUser = ref(false);

const generateData = () => {
  const data = [];
  return new Promise((resolve, reject) => {
    listUser().then((res) => {
      console.log(res);

      const states = res.rows;

      states.forEach((item, index) => {
        data.push({
          label: item.nickName,
          key: item.userId,
          initial: item.nickName,
        });
      });
      console.log(data);
      resolve(data);
    });
  });
};

const userData = ref([]);
const userQuery = ref([]);

const filterMethod = (query, item) => {
  return item.initial.toLowerCase().includes(query.toLowerCase());
};
// 表单重置
function reset() {
  form.value = {
    name: "",
    mobile: "",
    email: "",
    company: "",
    position: "",
    trialScope: "",
    remark: "",
  };
  proxy.resetForm("deptRef");
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加图层管理";
}
//用户分配
function handleOpenUser(row) {
  authUserGet(row.id).then(res=>{
  form.value.id = row.id;
  userQuery.value = res.data;
  openUser.value = true;
  })

}
function handleCheck(row) {
  proxy.$modal
    .confirm("是否确认将当前tif生成图片JPG")
    .then(() => {
      tifConvert(row.id).then((res) => {
        getList();
        proxy.$modal.msgSuccess("操作成功");
      });
    })
    .then(() => {})
    .catch(() => {})
    .finally(() => {});
}

/** 提交按钮 */
function submitForm(type) {
  proxy.$refs["deptRef"].validate((valid) => {
    if (valid) {
      let data = JSON.parse(JSON.stringify(form.value))
      buttonLoading.value = true;
      if(data){
        data.trialScope = data.trialScope.toString()
      }
      if (type=='agree') {
        agree(data)
          .then((response) => {
            proxy.$modal.msgSuccess("审核成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        reject(data)
          .then((response) => {
            proxy.$modal.msgSuccess("驳回成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  openUser.value = false;
  reset();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.trialId);
  console.log(ids.value)
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}
/** 查询图层管理列表 */
function getList() {
  //  loading.value = false;
  loading.value = true;
  list({params:{
    keyword: queryParams.value.keyword
  },...queryParams.value}).then((response) => {
    layersList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  queryParams.value.auditStatus =''
  handleQuery();
}

function handleExport(){
  proxy.download("/system/trial/export", {
    ...queryParams.value,
  },`试用申请_${new Date().getTime()}.xlsx`);
}
/** 删除按钮操作 */
function handleDelete(row) {
  const idsData = row.trialId || ids.value;
  proxy.$modal
    .confirm('是否删除数据？')
    .then(() => {
      loading.value = true;
      return delectById(idsData);
    })
    .then(() => {
      loading.value = false;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}
function handleUpdate(row) {
  loading.value = true;
  reset();
  form.value = {...row};
  form.value.trialScope=form.value.trialScope.split(',')
  const id = row.id || ids.value;
  open.value = true;
  loading.value = false;

}
getList();

</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: end;
}
</style>
