<template>
  <div>
    <el-card>
      <el-form
        :model="searchForm"
        label-width="90px"
        @submit.prevent="fnGetDataTransfer"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="过户类型" prop="ASSIGNED_TYPE">
              <el-select v-model="searchForm.ASSIGNED_TYPE">
                <el-option
                  v-for="item in relatedClassificationList"
                  :key="item.BIANMA"
                  :label="item.name"
                  :value="item.name"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="过户方式" prop="ASSIGNED_WAY">
              <el-select v-model="searchForm.ASSIGNED_WAY">
                <el-option
                  v-for="item in assignedWayList"
                  :key="item.BIANMA"
                  :label="item.NAME"
                  :value="item.NAME"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="过户车型" prop="ASSIGNED_VEHICLE_MODEL">
              <el-select v-model="searchForm.ASSIGNED_VEHICLE_MODEL">
                <el-option
                  v-for="item in assignedVehicleModel"
                  :key="item.BIANMA"
                  :label="item.NAME"
                  :value="item.NAME"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="原车牌号" prop="RAW_PLATE_NUMBER">
              <el-input
                v-model="searchForm.RAW_PLATE_NUMBER"
                placeholder="请填写原车牌号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="现车牌号" prop="NOW_PLATE_NUMBER">
              <el-input
                v-model="searchForm.NOW_PLATE_NUMBER"
                placeholder="请填写现车牌号"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="现车主" prop="NOW_OWNERS">
              <el-input
                v-model="searchForm.NOW_OWNERS"
                placeholder="请填写现车主业主"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="登记人" prop="REGISTRANT">
              <el-input
                v-model="searchForm.REGISTRANT"
                placeholder="请填写登记人"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="过户时间" prop="ASSIGNED_DATE">
              <el-date-picker
                v-model="searchForm.ASSIGNED_DATE"
                value-format="YYYY-MM-DD"
                format="YYYY-MM-DD"
                type="date"
                placeholder="请选择过户时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label-width="20px">
              <el-button type="primary" native-type="submit">搜索</el-button>
              <el-button native-type="reset" @click="fnResetPaginationTransfer">
                重置
              </el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="10px" class="end">
              <el-button>导出</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <layout-card>
      <layout-table
        v-model:pagination="pagination"
        :data="list"
        @get-data="fnGetDataTransfer"
      >
        <el-table-column label="序号" width="70">
          <template #default="{ $index }">
            {{ serialNumber(pagination, $index) }}
          </template>
        </el-table-column>

        <el-table-column prop="RECORD_NUMBER" label="档案号" />
        <el-table-column prop="ASSIGNED_TYPE" label="过户类型" />
        <el-table-column prop="RAW_PLATE_NUMBER" label="原车牌号" />
        <el-table-column prop="NOW_PLATE_NUMBER" label="现车牌号" />
        <el-table-column
          prop="RAW_OWNERS"
          label="原车主业主"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column
          prop="NOW_OWNERS"
          label="现车主业主"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column
          prop="ASSIGNED_DATE"
          label="过户时间"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column
          prop="REGISTRANT"
          label="登记人"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column
          prop="CREATTIME"
          label="创建时间"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column
          prop="OPERATTIME"
          label="修改时间"
          width="140"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="150">
          <template #default="{ row }">
            <el-button
              type="primary"
              text
              link
              @click="
                router.push({
                  path: '/mechanical_files/assigned_manage/view',
                  query: { ASSIGNED_ID: row.ASSIGNED_ID },
                })
              "
            >
              查看
            </el-button>
            <el-button
              v-if="buttonJurisdiction.del"
              type="primary"
              text
              link
              @click="fnDelete(row.ASSIGNED_ID)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
        <template #button>
          <el-button
            v-if="buttonJurisdiction.add"
            type="primary"
            @click="
              router.push({ path: '/mechanical_files/assigned_manage/add' })
            "
          >
            新增
          </el-button>
        </template>
      </layout-table>
    </layout-card>
  </div>
</template>

<script setup>
import { serialNumber } from "@/assets/js/utils.js";
import useListData from "@/assets/js/useListData.js";
import {
  layoutFnGetAssignedWay,
  layoutFnGetAssignedVehicleModel
} from "@/assets/js/assigned_manage.js";
import {
  getAssignedList,
  setAssignedManageDelete,
  layoutFnGetAssignedType,
} from "@/request/assigned_manage.js";
import { useRouter } from "vue-router";
import useButtonJurisdiction from "@/assets/js/useButtonJurisdiction.js";
import { debounce } from "throttle-debounce";
import { ElMessage, ElMessageBox } from "element-plus";
import { onMounted, ref } from "vue";

const router = useRouter();
const { list, pagination, searchForm, fnGetData, fnResetPagination } =
  useListData(getAssignedList, {});

const assignedVehicleModel = await layoutFnGetAssignedVehicleModel();

const relatedClassificationList = ref(null);

onMounted(async () => {
  const data = await layoutFnGetAssignedType();
  relatedClassificationList.value = JSON.parse(data.value.zTreeNodes);
});
const assignedWayList = await layoutFnGetAssignedWay();
const fnGetDataTransfer = () => {
  fnGetData({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnResetPaginationTransfer = () => {
  searchForm.value = {};
  fnResetPagination({
    DEPTIDS: searchForm.value.DEPTIDS?.join(","),
  });
};
const fnDelete = debounce(
  1000,
  async (ASSIGNED_ID) => {
    await ElMessageBox.confirm("确定要删除吗？", { type: "warning" });
    await setAssignedManageDelete({ ASSIGNED_ID })
    ElMessage.success("删除成功");
  },
  { atBegin: true }
);

const buttonJurisdiction = await useButtonJurisdiction("assigned");
</script>
<style scoped></style>
