<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
    <el-form-item label="图层" prop="algorithmName">
         <el-select
          class="m-2 select"
          v-model="queryParams.layersId"
          placeholder="Select"
        >
          <el-option
            v-for="item in layerslist"
            :key="item.name"
            :label="item.layerName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="算法名称" prop="algorithmName">
        <el-input
          v-model="queryParams.algorithmName"
          placeholder="请输入算法名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:cost:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"

          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:cost:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"

          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:cost:remove']"
        >删除</el-button>
      </el-col>

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

    <el-table v-loading="loading" :data="costList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="算法名称" align="center" prop="algorithmName" />
      <el-table-column label="项目名称" show-overflow-tooltip align="center" prop="itemName" />
      <el-table-column label="项目描述" show-overflow-tooltip align="center" prop="itemDescription" />
      <el-table-column label="单位" width="150" align="center" prop="unit" >
         <template #default="scope">
           {{getDict(scope.row.unit)}}
         </template>
      </el-table-column>
      <el-table-column label="单价" align="center"  width="150" prop="price" />
       <el-table-column label="排序" align="center" width="150" prop="orderNum" />
      <el-table-column
            label="操作"
            align="center"
            width="200"
            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="title" v-model="open" width="500px" append-to-body>
      <el-form ref="deptRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="算法名称" prop="algorithmName">
          <el-input v-model="form.algorithmName" placeholder="请输入算法名称" />
        </el-form-item>
        <el-form-item label="项目名称" prop="itemName">
          <el-input v-model="form.itemName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目描述" prop="itemDescription">
          <el-input v-model="form.itemDescription" placeholder="请输入项目描述" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
            <el-select style="width:100%" v-model="form.unit" placeholder="请选择">
                        <el-option
                           v-for="dict in cost_unit"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.value"
                        ></el-option>
           </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
          <el-form-item label="排序" prop="orderNum">
          <el-input-number v-model="form.orderNum"  controls-position="right" :min="0" placeholder="请输入排序" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>








  </div>
</template>

<script setup name="Layers">
import { listCost, getCost, delCost, addCost, updateCost } from "@/api/system/cost";

import {
  listLayers,

} from "@/api/system/layers";
const { proxy } = getCurrentInstance();
const {  cost_unit } = proxy.useDict("cost_unit");
const data = reactive({
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 造价配置表格数据
      costList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        algorithmName: undefined,
        itemName: undefined,
        itemDescription: undefined,
        unit: undefined,
        price: undefined,
        layersId: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

        algorithmName: [
          { required: true, message: "算法名称不能为空", trigger: "blur" }
        ],
        itemName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        itemDescription: [
          { required: true, message: "项目描述不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
         orderNum: [
          { required: true, message: "排序", trigger: "blur" }
        ],

      }
});

const {
  open,
  showSearch,
  queryParams,
  form,
  ids,
  rules,
  title,
  costList,
  total,
  multiple,
  single,
  loading,
  buttonLoading,
} = toRefs(data);
// 表单重置
function reset() {
  form.value = {
    id: undefined,
    algorithmName: undefined,
    itemName: undefined,
    itemDescription: undefined,
    unit: undefined,
    price: undefined,
    orderNum:undefined
  };
  proxy.resetForm("deptRef");
}
function getDict(val){

 return  cost_unit.value.filter((e)=>{
     return e.value ==val

  })[0]?.label
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加配置";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["deptRef"].validate((valid) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {


        updateCost(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      } else {
        addCost(form.value)
          .then((response) => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          })
          .finally(() => {
            buttonLoading.value = false;
          });
      }
    }
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
// 多选框选中数据
function handleSelectionChange(selection) {

  ids.value = selection.map((item) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
  console.log(ids.value)
}
/** 查询图层管理列表 */
function getList() {
  //  loading.value = false;
    loading.value = true;
    costList.value=[]
    listCost(queryParams.value).then(response => {
        costList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryForm");
  handleQuery();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const idvalue = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除当前的数据项？')
    .then(() => {
      loading.value = true;
      return delCost(idvalue);
    })
    .then(() => {
      loading.value = false;
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {})
    .finally(() => {
      loading.value = false;
    });
}
function handleUpdate(row) {
  loading.value = true;
  reset();
  const id = row.id || ids.value;
  getCost(id).then((response) => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改配置";
  });
}
const layerslist = ref([])
function getLayers(){
  layerslist.value.push({
    id:'',
    layerName:'基础造价图层'
  })
  listLayers().then((res) => {
   layerslist.value =[...layerslist.value,...res.rows]
  })
}
getList();
getLayers();
</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: end;
}
</style>
